How to do operation in {CELL}??

1 view (last 30 days)
troy
troy on 18 May 2013
Assume i have 8x8 blocks of image. Here my code:
[L L]=size(img);
L2=C/8;
imgblk=cell(L2,L2);
for i=1:L2
for j=1:L2
imgblk{i,j}=img( (i-1)*4+1:i*4 , (j-1)*4+1:j*4);
end
end
Now, how to apply coding below into imgblk{i,j} since imgblk{i,j} is in 'cell'??
A (i,j) = [Ʃ(fi,j - fi,j+1 ) - Ʃ(fi,j-1 - fi,j )]
and
B (i,j) = [Ʃ(fi,j - fi,j+1 ) - Ʃ(fi,j+1 - fi,j+2 )]
Also, how to do in horizontal first, then vertical? Thanks for those who help me =)
  3 Comments
Walter Roberson
Walter Roberson on 18 May 2013
fi,j is f subscript i,j . But that doesn't tell us what f is or what the sum is over.
troy
troy on 19 May 2013
Sorry.Actually we denote the luminance value in the image f of size MxN or LxL, at the particular location (i,j) with f(i,j), i and j being the row and column index, respectively.
Calculation sum is for every pixel f(i,j) in the odd-numbered rows of the image two variables :A (i,j) and B (i,j)
In my case i denote f(i,j)=I2(i,j)
I have done the code.But get error.
I2 = imread('.....');
I2 = rgb2gray (I2);
N=8;
[L L]=size(img);
L2=C/8;
imgblk=cell(L2,L2);
for i=1:L2
for j=1:L2
imgblk{i,j}=img( (i-1)*4+1:i*4 , (j-1)*4+1:j*4);
A (i,j) = abs(sum(I2(i,j) - I2(i,j+1) ) - sum(I2(i,j-1) - I2(i,j) ))
B (i,j) = abs(sum(I2(i,j) - I2(i,j+1) ) - sum(I2(i,j+1) - I2(i,j+2) ))
end
end
and i got this error
>>Undefined function or method 'minus' for input arguments of type 'cell'.
Actually A (i,j) = [Ʃ(f(i,j) - f(i,j+1) ) - Ʃ(f(i,j-1) - f(i,j) )] and B (i,j) = [Ʃ(f(i,j) - f(i,j+1) ) - Ʃ(f(i,j+1) - f(i,j+2) )] .
where i€ (i, i+2, i+4) and i is odd-numbered row.

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 19 May 2013
Edited: Image Analyst on 19 May 2013
I don't understand this code at all. Some questions
  1. What is C?
  2. What is L2 for?
  3. What is imgblk intended to be used for? Why is it necessary? Why are you messing around with cells anyway?
  4. Do you know that sum(I2(i,j) - I2(i,j+1)) will clip to zero if I2 is uint8? Cast them to double before subtracting to avoid that.
  5. What are A and B supposed to represent?
  5 Comments
Image Analyst
Image Analyst on 20 May 2013
I didn't say you can't use it. I said you don't use it.
Here's the first blockproc 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 = [];
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(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);
Here's the second blockproc demo:
% Demo of blockproc in two different ways.
% One uses an anonymous function to return a block of pixels
% the same size as the sliding window block.
% The other uses a custom written function to return a
% single value for each sliding window position.
function blockproc_demo()
try
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
% Read in standard MATLAB demo image.
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage, []);
caption = sprintf('Original Image\n%d by %d pixels', ...
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')
% Block process the image.
windowSize = 3;
% Each 3x3 block will get replaced by one value.
% Output image will be smaller by a factor of windowSize.
myFilterHandle = @myFilter;
blockyImage = blockproc(grayImage,[windowSize windowSize], myFilterHandle);
[rowsP, columnsP, numberOfColorChannelsP] = size(blockyImage);
% Display the processed image.
% It is smaller, but the display routine imshow() replicates
% the image so that it looks bigger than it really is.
subplot(2, 2, 2);
imshow(blockyImage, []);
caption = sprintf('Image Processed in %d by %d Blocks\n%d by %d pixels\nCustom Box Filter', ...
windowSize, windowSize, rowsP, columnsP);
title(caption, 'FontSize', fontSize);
% Now let's do it an alternate way where we use an anonymous function.
% We'll take the standard deviation in the blocks.
windowSize = 8;
myFilterHandle2 = @(block_struct) ...
std2(block_struct.data) * ones(size(block_struct.data));
blockyImageSD = blockproc(grayImage, [windowSize windowSize], myFilterHandle2);
[rowsSD, columnsSD, numberOfColorChannelsSD] = size(blockyImageSD);
subplot(2, 2, 4);
imshow(blockyImageSD, []);
caption = sprintf('Image Processed in %d by %d Blocks\n%d by %d pixels\nAnonymous Standard Deviation Filter', ...
windowSize, windowSize, rowsSD, columnsSD);
title(caption, 'FontSize', fontSize);
% Note: the image size of blockyImageSD is 256x256, NOT smaller.
% That's because we're returning an 8x8 array instead of a scalar.
uiwait(msgbox('Done with demo'));
catch ME
errorMessage = sprintf('Error in blockproc_demo():\n\nError Message:\n%s', ME.message);
uiwait(warndlg(errorMessage));
end
return;
% Takes one 3x3 block of image data and multiplies it
% element by element by the kernel and
% returns a single value.
function singleValue = myFilter(blockStruct)
try
% Assign default value.
% Will be used near sides of image (due to boundary effects),
% or in the case of errors, etc.
singleValue = 0;
% Create a 2D filter.
kernel = [0 0.2 0; 0.2 0.2 0.2; 0 0.2 0];
% kernel = ones(blockStruct.blockSize); % Box filter.
% Make sure filter size matches image block size.
if any(blockStruct.blockSize ~= size(kernel))
% If any of the dimensions don't match.
% You'll get here near the edges,
% if the image is not a multiple of the block size.
% warndlg('block size does not match kernel size');
return;
end
% Size matches if we get here, so we're okay.
% Extract our block out of the structure.
array3x3 = blockStruct.data;
% Do the filtering. Multiply by kernel and sum.
singleValue = sum(sum(double(array3x3) .* kernel));
catch ME
% Some kind of problem...
errorMessage = sprintf('Error in myFilter():\n\nError Message:\n%s', ME.message);
% uiwait(warndlg(errorMessage));
fprintf(1, '%s\n', errorMessage);
end
return;
troy
troy on 20 May 2013
thanks..I will try to understand it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!