How to read RGB values from each block after running this code?

1 次查看(过去 30 天)
I have written a code for segment an image into 12 blocks.
clear; clc;
i=imread('./train/aa.png');
i2=imresize(i,[400,300]);
[rows columns numberOfColorBands] = size(i2);
%divide image up into blocks
blockSizeR = 100; %Rows in block
blockSizeC = 100; %Columns in block
%Figure out the size of each block in rows.
wholeBlockRows = floor(rows / blockSizeR);
blockVectorR = [blockSizeR * ones(1, wholeBlockRows)];
% Figure out the size of each block in columns.
wholeBlockCols = floor(columns / blockSizeC);
blockVectorC = [blockSizeC * ones(1, wholeBlockCols)];
% Create the cell array, ca.
% in the array contains a blockSizeR by blockSizeC by 3 color array.
% This line is where the image is actually divided up into blocks.
if numberOfColorBands > 1
% It's a color image.
ca = mat2cell(i2, blockVectorR, blockVectorC, numberOfColorBands);
else
ca = mat2cell(i2, blockVectorR, blockVectorC);
end
% Now display all the blocks.
plotIndex = 1;
numPlotsR = size(ca, 1);
numPlotsC = size(ca, 2);
for r = 1 : numPlotsR
for c = 1 : numPlotsC
fprintf('plotindex = %d, c=%d, r=%d\n', plotIndex, c, r);
% Specify the location for display of the image.
subplot(numPlotsR, numPlotsC, plotIndex);
% Extract the numerical array out of the cell
% just for tutorial purposes.
rgbBlock = ca{r,c};
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
[rowsB columnsB numberOfColorBandsB] = size(rgbBlock);
% Make the caption the block number.
caption = sprintf('Block #%d of %d\n%d rows by %d columns', ...
plotIndex, numPlotsR*numPlotsC, rowsB, columnsB);
title(caption);
drawnow;
% Increment the subplot to the next location.
plotIndex = plotIndex + 1;
end
end
Now,I want to read the RGB values from each block and calculate their mean and standard deviation and make a 72 element feature vector that would be feeded in to the feature vector storage. Please help. I need to submit the project. Thank you very much.

采纳的回答

Sulaymon Eshkabilov
Here are the calculations missing in your code to compute mean, std of R, G, B, etc.
...
% just for tutorial purposes.
rgbBlock = ca{r,c};
R{plotIndex}=rgbBlock(:,:,1); % Red
B{plotIndex}=rgbBlock(:,:,2); % Green
B{plotIndex}=rgbBlock(:,:,3); % Blue
R_Mean(plotIndex) = mean2(rgbBlock(:,:,1)); % Mean of Red
G_Mean(plotIndex) = mean2(rgbBlock(:,:,2)); % Mean of Green
B_Mean(plotIndex) = mean2(rgbBlock(:,:,3)); % Mean of Blue
STD_R(plotIndex)=std(double(rgbBlock(:,:,1)), 0, 'all');
STD_G(plotIndex)=std(double(rgbBlock(:,:,2)), 0, 'all');
STD_B(plotIndex)=std(double(rgbBlock(:,:,3)), 0, 'all');
imshow(rgbBlock); % Could call imshow(ca{r,c}) if you wanted to.
...
Good luck.
  5 个评论
Sulaymon Eshkabilov
@ARNABJIT CHOUDHURY You can use Figure(), as Image Anslys suggested. You may also keep track of all displayed images with:
figure(ii) % ii =1, 2, 3 ...
Good Luck

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by