extracting rgb from each block of an image in matlab

we divided an image of size 800*600 into 5 blocks using function "mat2cell" and i got output as "Columns 1 through 3
[60x80 uint8] [60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8] [60x80 uint8]
Columns 4 through 5
[60x80 uint8] [60x80 uint8] "
[60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8]
[60x80 uint8] [60x80 uint8]
now i want to extract rgb from each block of the image.. plz help..

Answers (2)

What was the original size of the matrix? From your output, it looks like the matrix had a third dimension of 1. For an image to be RGB, the third dimension must be 3, i.e.
[m n p] = size(IMAGE)
%p must be 3 for RGB
So I'm not sure how it's possible to do what you are trying to do.
See this demo and you will know how to do it:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% For demo purposes for Deepak, let's resize it to be a multiple of 25;
rgbImage = imresize(rgbImage, [100 150]);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage)
blockSize = 25;
ca = mat2cell(rgbImage,blockSize*ones(1,size(rgbImage,1)/blockSize),blockSize*ones(1,size(rgbImage,2)/blockSize),3);
plotIndex = 1;
for r = 1 : size(ca, 1)
for c = 1 : size(ca, 2)
fprintf('c=%d, r=%d\n', c, r);
subplot(4, 6, plotIndex);
imshow(ca{r,c});
plotIndex = plotIndex + 1
end
end
% Display the original image in the upper left.
subplot(4, 6, 1);
imshow(rgbImage);
title('Original Image');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

Categories

Find more on MATLAB Mobile in Help Center and File Exchange

Asked:

on 31 Mar 2012

Community Treasure Hunt

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

Start Hunting!