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
MATLAB: 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.
compressionImage Processing Toolbox
Related Solutions
See this demo. It will be very easy for you to adapt it. Basically you do this:
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));blockSize = [32 32];blockyImage = blockproc(grayImage, blockSize, meanFilterFunction);
Here's the full demo:
% Demo code to divide the image up into 16 pixel by 16 pixel blocks
% and replace each pixel in the block by the median, mean, or standard
% deviation 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 = fullfile(matlabroot, '\toolbox\images\imdemos');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 = [];endbaseFileName = '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(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', '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 median gray value in the block
% and create an equal size block where all pixels have the median value.
% Image will be the same size since we are using ones() and so for each block
% there will be a block of 8 by 8 output pixels.
medianFilterFunction = @(theBlockStructure) median(theBlockStructure.data(:)) * ones(size(theBlockStructure.data), class(theBlockStructure.data));% Block process the image to replace every pixel in the
% 8 pixel by 8 pixel block by the median of the pixels in the block.
blockSize = [8 8];% Quirk: must cast grayImage to single or double for it to work with median().
% blockyImage8 = blockproc(grayImage, blockSize, medianFilterFunction); % Doesn't work.
blockyImage8 = blockproc(single(grayImage), blockSize, medianFilterFunction); % Works.
[rows columns] = size(blockyImage8);% Display the block median image.
subplot(2, 2, 2);imshow(blockyImage8, []);caption = sprintf('Block Median Image\n32 blocks. Input block size = 8, output block size = 8\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);% 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.
% Note that the size of the output block (2 by 2) does not need to be the size of the input block!
% Image will be the 128 x 128 since we are using ones(2, 2) and so for each of the 64 blocks across
% there will be a block of 2 by 2 output pixels, giving an output size of 64*2 = 128.
% We will still have 64 blocks across but each block will only be 2 output pixels across,
% even though we moved in steps of 4 pixels across the input image.
meanFilterFunction = @(theBlockStructure) mean2(theBlockStructure.data(:)) * ones(2,2, class(theBlockStructure.data));blockSize = [4 4];blockyImage64 = blockproc(grayImage, blockSize, meanFilterFunction);[rows columns] = size(blockyImage64);% Display the block mean image.
subplot(2, 2, 3);imshow(blockyImage64, []);caption = sprintf('Block Mean Image\n64 blocks. Input block size = 4, output block size = 2\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);% Block process the image to replace every pixel in the % 8 pixel by 8 pixel block by the standard deviation
% of the pixels in the block.
% Image will be smaller since we are not using ones() and so for each block
% there will be just one output pixel, not a block of 8 by 8 output pixels.
blockSize = [8 8];StDevFilterFunction = @(theBlockStructure) std(double(theBlockStructure.data(:)));blockyImageSD = blockproc(grayImage, blockSize, StDevFilterFunction);[rows columns] = size(blockyImageSD);% Display the block standard deviation filtered image.
subplot(2, 2, 4);imshow(blockyImageSD, []);title('Standard Deviation Filtered Image', 'FontSize', fontSize);caption = sprintf('Block Standard Deviation Filtered Image\n32 blocks. Input block size = 8, output block size = 1\n%d rows by %d columns', rows, columns);title(caption, 'FontSize', fontSize);
It's just 3 lines. Just mask the original image:
binaryImage = grayImage > 150;maskedImage = zeros(size(grayImage)); % Initialize.
maskedImage(binaryImage) = grayImage(binaryImage);
See the full demo below:
% Demo by ImageAnalyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'coins.png';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 2, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);subplot(2, 2, 2); bar(pixelCount);grid on;title('Histogram of original image', 'FontSize', fontSize);xlim([0 grayLevels(end)]); % Scale x axis manually.
yl = ylim();line([150 150], [0 yl(2)], 'Color', 'r');% Threshold at 150
binaryImage = grayImage > 150;% Display the original gray scale image.subplot(2, 2, 3);imshow(binaryImage, []);title('Binary Image', 'FontSize', fontSize);% Mask the image
maskedImage = zeros(size(grayImage)); % Initialize.% Assign values from grayImage that are > 150.
maskedImage(binaryImage) = grayImage(binaryImage);% Display the original gray scale image.subplot(2, 2, 4);imshow(maskedImage, []);title('Masked Image', 'FontSize', fontSize);
Best Answer