i had converted image into blocks. I converted blocks to linear array. Next i calculated mean values for first 8 arrays as given below. now i want to calculate values by using the formula: sum(linear array(:,1)​​-mean(:,1​)​).^2. please send code.

for i=1:4:nr-3
for j=1:4:nc-3
block=I(i:i+3,j:j+3);
%convert 4X4 into 16X1 column vector
tv(:,col)=reshape(block,16,1);
col=col+1;
count=count+1;
column=column+4;
end
row=row+4;
end
%find mean of 8 arrays
me=1;
nn=8;
nnn=1;
for count=1:nr+nc
cv(:,r) = mean(tv(:,me:nn*nnn), 2);
r=r+1;
nnn=nnn+1;
me=me+8;
end

2 Comments

Which variable corresponds to the "linear array" of your formula?
thank you for your reply sir. variable tv for linear array and variable cv for mean sir..

Sign in to comment.

 Accepted Answer

sri, simply use blockproc():
% Uses blockproc() to get mean, median, and standard deviation of image blocks.
% Demo code to divide the image up into 4 pixel by 4 pixel blocks
% and replace each pixel in the block by the mean,
% of all the gray levels of the pixels in the block.
%
clc;
clearvars;
close all;
workspace;
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('cameraman.tif'));
if ~exist(folder, 'dir')
% If that folder does not exist, don't use a folder
% and hope it can find the image on the search path.
folder = [];
end
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
subplot(1, 2, 1);
imshow(grayImage, []);
axis on;
caption = sprintf('Original Grayscale Image\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
set(gcf,'name','Image Analysis Demo','numbertitle','off')
% Define the function that we will apply to each block.
% First in this demo we will take the mean gray value in the block
% and create an pixel where all pixels have the mean value.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:));
% Block process the image to replace every pixel in the
% 4 pixel by 4 pixel block by the mean of the pixels in the block.
% The image is 256 pixels across which will give 256/4 = 64 blocks.
% Image will be the 1/4 the size of the original.
blockSize = [4 4];
blockMeanImage = blockproc(grayImage, blockSize, meanFilterFunction);
[rows, columns] = size(blockMeanImage);
% Display the block mean image.
subplot(1, 2, 2);
imshow(blockMeanImage, []);
axis on;
caption = sprintf('Block Mean Image\n64 blocks. Input block size = 4\n%d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
% Get the first 8 mean values going across columns
first8Means = blockMeanImage(1, 1:8)
message = sprintf('%.2f\n ', first8Means);
message = sprintf('The first 8 means = \n%s\n', message)
uiwait(helpdlg(message));

10 Comments

thank you sir.. can u say the code to convert 64*64 into again 256*256. if you know, please send the code sir, thank you..
And i have another doubt sir.. is your code compress the original image 256*256 to 64*64. please reply sir..
sri: If you process an image and move a 4 row-by-4 column block/tile along in jumps of 4 pixels, and compute the mean in each 4x4 block, then the output image will have only 1/4 as many elements along the row and column direction. So I really didn't compress anything, it's just that the output is smaller because your block moves along in jumps of the block size. If you want it to move by only one pixel, then it could be the same size as the original, and then you'd use imfilter() or conv2(). But you say you want to move in blocks/jumps so your output image will be smaller.
sir.. can you please explain the program statement given below:
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)); thank you sir..
blockproc() divides the data up into block, and it passes information about that block as a structure, passing it to the user supplied function. The structure that is passed has a field named "data" which is the contents of the section of the array it is to act upon. theBlockStructure.data is therefore the data to be acted upon, and theBlockStructure.data(:) reorganizes that data as a column vector. Then mean2() takes the mean of the column vector. mean() could have been used instead.
sir. can you send the reverse process of above. that is, code to convert 64*64 block image into 256*256 block image. please help me sir.
Do you mean like
image256 = imresize(image64, [256, 256], 'nearest');
Or actually you can do it with the function like this:
meanFilterFunction = @(theBlockStructure) mean(theBlockStructure.data(:)) * ones(8);
So that the filter function returns an 8 by 8 block (with all the same gray level). See my attached blockproc() demos for that and more ways to use blockproc().

Sign in to comment.

More Answers (1)

%After you have the array, you can try the function rms
error = (rms(array))^2; %as descripted, sometimes rms is directly used as error

4 Comments

It is recommended that you do not use "error" as the name of a variable, as error() is a critical MATLAB function.
sir.. how to apply formula in code.. you don't bother error..
After the code you show there in your Question, add
values = sum( (tv(:,1) - cv(:,1)).^2 );

Sign in to comment.

Asked:

on 5 Apr 2016

Commented:

on 18 Apr 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!