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.
1 view (last 30 days)
Show older comments
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
Accepted Answer
Image Analyst
on 6 Apr 2016
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
Image Analyst
on 10 Apr 2016
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().
More Answers (1)
Kuifeng
on 5 Apr 2016
%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
Walter Roberson
on 6 Apr 2016
After the code you show there in your Question, add
values = sum( (tv(:,1) - cv(:,1)).^2 );
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!