Image segmentation/blocking and get value from each block

First image is my input image i want to make 1)first divided(segment) my image into M*N block (like Second pic) 2) the i want to calculate(percentage of black and white color) from each block 3) then each block separate based on a threshold value of black or while percentage
i am new in image processing please give me a details explain, how can i solve my problem
Thanks in advance

 Accepted Answer

Have you done step 1 yet? It look like it.
Then threshold the image with a fixed threshold that is just above the gray level of the chassis.
binaryImage = grayImage > threshold;
See the interactive thresholding utility/tool in my my File Exchange Also run the Image Segmentation Tutorial there.
Next you need to scan the blocks in your image. Assuming you have the rows and columns of the red lines, do something like this:
for xBlockIndex = 1 : length(columns)-1
col1 = columns(xBlockIndex);
col2 = columns(xBlockIndex+ 1) - 1;
for yBlockIndex = 1 : length(rows)-1
row1 = rows(yBlockIndex);
row2 = rows(yBlockIndex+1)-1;
% Extract sub image
subImage = grayImage(row1:row2, col1:col2);
% Extract mask sub image
subImageMask = binaryImage(row1:row2, col1:col2);
% Compute mean for pixel above the threshold.
meanGrayLevels(yBlockIndex, xBlockIndex) = mean(subImage(subImageMask));
end
end

10 Comments

sazzad's "Answer" moved here:
Dear Image Analyst,
First all of thanks for your response. i am new in this field, i have not enough knowledge to understand your coding, however in my last few weeks i tried a lot to understand the basic of image processing although my research field in different but i need some image operation.i run both your both tutorial "Thresholding an image" and "Image Segmentation Tutorial ("BlobsDemo")" by using my image as a input. That i understood after running both, in Thresholding program => when i choose floating point[[( i can't select integer image because of some error that i don't understand like this Error using ==> imageDisplayParseInputs>validateCData at 291 If input is logical (binary), it must be two-dimensional.) )]]] i can select different band and can change the threshold value according to my demand. i can get binayimage and maskimage
when i run segmentation it generate pseudo-color image and 12 coin in one image but i don't understand how to use this output in my work.
i want to input my image and divided it into 12x8 block, then for each block want to determine the ration =(black color/white color), if the ratio is greater then 1 than my output will be 0 and if less than 1 my output will be 1 for that block like this at the end i want to get my output as a 1011100...... Could you please explain me more details what i need to do to get my output, please consider me as a elementary student in this field.
I don't know. Post your image. Maybe your image is color and the program didn't check for that. Maybe I've made changes since I uploaded blobs demo. Do you see these lines in your code starting around line 58:
% Check to make sure that it is grayscale.
[rows, columns, numberOfColorBands] = size(originalImage);
if numberOfColorBands > 1
promptMessage = sprintf('Your image file has %d color bands.\nThis demo was designed for grayscale images.\nDo you want me to convert it to grayscale for you so you can continue?', numberOfColorBands);
button = questdlg(promptMessage, 'Continue', 'Convert and Continue', 'Cancel', 'Convert and Continue');
if strcmp(button, 'Cancel')
fprintf(1, 'Finished running BlobsDemo.m.\n');
return;
end
% Do the conversion using standard book formula
originalImage = rgb2gray(originalImage);
end
i post my image in this link,
I need my output in form of 10101100 ...... binary value for each block , in my image i need to consider 8(row)x 12(column) ( output will be 96 binary bits after analyzing all the blocks(8x12 block)
my image run in "blobsdemo" but but i don't understand what to do with this please kindly explain details.
Dear Image Analyst,
I am looking forward to receive a solution of my problem from you. hope you will give me a good direction to solve my problem.
Thanks in advance
You can't just run blobsdemo blindly. It will find every blob. But you have some "missing" blobs at certain locations so it won't find those. What you have to do is to scan the image and at each location, take a little subwindow around the disk and check it's mean intensity. Some cells are brighter than others and it's certainly not binary, though you can threshold if you want to make it so. If all you want to do is to see if a certain number of pixels are lit up at each location then you don't need to call regionprops like blobsdemo does.
close all;
clear all;
clc;
I=imread('BinaryImage.png');
size(I);
c=mat2cell(I,[28 28 28 28 28 28 28 29],[28 28 28 28 28 28 28 28 28 28 28 24]);
for i=1:8
for j=1:12;
a=c{i,j};
n=numel(a);
d=nnz(a);
p=n/2-100;
if d>p
w{i,j}=1;
else w{i,j}=0;
end
end
end
See attached code to produce this:
The green boxes are around where I thought the cell was lit up enough. You can adapt it however you wish.
sazzad, did you see my solution? What do you think of it?
Dear Image Analyst,
Thanks a lot for your effort, you really do a great job for me, i am trying to make my code based on your coding.
Thanks again.
Thnx a lot sir. THANX A LOT.

Sign in to comment.

More Answers (1)

Dear Rsearcher
I have a rice panicle iamge. Then I separate to number of block, which size is same as the size of grain. This much I completed.Code attached. Now I want to detect the defected grain (blackish) in each block by applying some kind of thresholding operation. Then show the blocks which contain the defected grain. Then count these blocks. kindly help me.
.panicle1.jpg

Community Treasure Hunt

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

Start Hunting!