Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Blocks extraction and handling from image

Subject: Blocks extraction and handling from image

From: Engineer

Date: 17 Apr, 2013 11:08:07

Message: 1 of 2

Hello,
I have divided my image following the process with mat2cell, as I saw here:
http://www.mathworks.com/matlabcentral/answers/46406

Now, I'd like to handle each block as a single image.

im = imread(binaryImage); %dimensions 1444x806
blockRows = 26;
blockColumns = 38;
wholeim = floor(rows / blockRows);
blockVectorR = [blockRows * ones(1, wholeim), rem(rows, blockRows)];
wholeBlockCols = floor(columns / blockColumns);
blockVectorC = [blockColumns * ones(1, wholeBlockCols), rem(columns, blockColumns)];
cellblocks = mat2cell (binaryimage, blockVectorRows, blockVectorColumns);
numPlotsR = size(cellblocks, 1);
numPlotsC = size(cellblocks, 2);
for r = 1 : numPlotsR
     for c = 1 : numPlotsC
                rgbBlock = ca{r,c};
end

I've tried several combinations, but rgbBlock results always just one matrix.
I've tried cell2mat, it gives me back the whole image

How can I exctract, numbering it, each matrix from each cell of cellblocks in the loop, in order to handle cellblocks as images (I have to provide white pixel concentration in each block, and apply regionprops etc.)?

thanks for your help!

Subject: Blocks extraction and handling from image

From: Engineer

Date: 19 Apr, 2013 14:51:08

Message: 2 of 2

Ok, I figured it out after few trials..I reply to myself!
The code is rough, but could be usefull to somenoe else. Cheers
%%%%%%%
im = imread(binaryimage);

%%in my case, image is 1444x806
% blockRows = 26; % Rows in block.
% blockColumns = 38; % Columns in block.

%% divide the image with blocks size 26x38 >> divide the image in (806/26)31 rows x (1440/38)38 colmns
Mcell = mat2cell(im,[26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26],[38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38]);
%% for every cell,count how many black and how many white pixels >> two arrays cointaining the numbers
a=zeros(31,38);
b=zeros(31,38);
c=zeros(31,38);
d=zeros(31,38);
for i=1:31
    for j=1:38
       index = find(Mcell{i,j}==0);
      a(i,j)=numel(index); % how many black pixels==0
     b(i,j)=numel(Mcell{i,j})-a(i,j); %how many whyte pixels==1
    c(i,j)=((numel(Mcell{i,j})-a(i,j))./988).*100; %percentage of white pixels
          end
end
%imshow(im),impixelinfo
%% display the grid
im(26:26:end,:,:) = 70; %# Change every tenth row to black
im(:,38:38:end,:) = 70; %# Change every tenth column to black
imshow(im); %# Display the image

%% operations in c array of percentages
for i=1:31
    for j=1:38
       index2=find(c(i,j)<100 & c(i,j)>16); % condition to satisfy
       d(i,j)=numel(index2); % matrix with =1 when satisfied
    end
end
c(~d)=0;
[row,col] = find(c>0); %indexes in which c>0
f=[row col]; %elemnts that identify cells to analyse

for x=1:numel(row)
    figure
    imshow(Mcell{f(x,1),f(x,2)})
end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us