Count grid boxes inside an object in an image.

Hi,
I want to draw grid on an image and I want to count the number of grid boxes which cover an object in that image. Some regions of that object intersect the grid. How can I count those intersect grids as well as inside of that object.?
Experts kindly get me the solution.
Regards Dams

 Respuesta aceptada

Image Analyst
Image Analyst el 11 de Mayo de 2013
Damodara:
If you draw C lines along the columns (vertically), this will split the image into (C+1) vertical strips. If you draw R lines along the rows (horizontally), this will split the image into (R+1) horizontal strips. You will then have (C+1)*(R+1) boxes in the grid. You will have C*R intersections, where the vertical and horizontal lines intersect.
If you have a binary image then you need to make a mask of the lines and multiply it or assign the binary image to be false there. Then call bwlabel to get the number of parts you chopped your object into.
[labeledImage, numberOfPieces] = bwlabel(binaryImage);
Of course that depends on where you drew your lines and will have a problem if any of the lines overlap a 1 pixel wide part. Please run this demo I made specially for you:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Synthesize an image.
grayImage = peaks(400);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage)
% Display the original gray scale image.
fh = figure;
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Binarize the image by thresholding it.
binaryImage = grayImage > 2.1;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Make mask
columnSplitters = 16:32:columns;
rowSplitters = 16:32:rows;
splitBinaryImage = binaryImage; % Initialize.
splitBinaryImage(:, columnSplitters) = false;
splitBinaryImage(rowSplitters, :) = false;
% Display the image.
subplot(2, 2, 3);
imshow(splitBinaryImage, []);
title('Split Binary Image', 'FontSize', fontSize);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage, numberOfBlobs] = bwlabel(splitBinaryImage, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
subplot(2, 2, 4);
imshow(coloredLabelsImage);
caption = sprintf('The number of pieces = %d', numberOfBlobs)
title(caption, 'FontSize', fontSize);
message = sprintf('Done with Image Analyst demo.\nThe number of pieces = %d', numberOfBlobs);
uiwait(helpdlg(message));
close(fh); % Close down the figure

12 comentarios

Image Analyst
Image Analyst el 11 de Mayo de 2013
P.S. Note that all the slicing lines are not shown in the lower left image due to subsampling (shrinking) of the image.
Damodara
Damodara el 12 de Mayo de 2013
Editada: Image Analyst el 12 de Mayo de 2013
Thanks but here if you reduce row and column splitters to one it will not give any count. And even mask lines take some pixels so count will be wrong if you consider more lines. My code as here. Kindly check this and how can I count these pieces.? Please help me.
myFolder =uigetdir;
%....GET INPUT DIRCTORY......
filePattern = fullfile(myFolder, '*.png');
pngFiles = dir(filePattern);
for i = 1:length(pngFiles)
baseFileName = pngFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
binaryImage(:,:,i)=imread(fullFileName);
imagesc(binaryImage);
colormap(gray)
[nX,nY,nZ]=size(binaryImage);
nSeg=max(nX,nY);
set(gca,'xtick',linspace(0,nY,nSeg+1),'xticklabel',[],...
'xgrid','on','xcolor','K',...
'ytick',linspace(0,nX,nSeg+1),'ytickLabel',[],...
'ygrid','on','ycolor','K',...
'gridLineStyle','-','linewidth',1)
end
Regards Dams
Image Analyst
Image Analyst el 12 de Mayo de 2013
But, unlike my code, your code doesn't do anything. It just reads in images, and even then it requires that they all be the same size. Did you forget to adapt my code and paste it in? And I warned you about the line taking up a pixel and thus would need to be adapted some if you expect to have one pixel wide regions. And of course, you need to specify exactly what rows and columns you want to split the image on - recall I said it depends on where you draw your lines. I assume you know that, and know that I just made some arbitrary decisions on that, but it looks like I better make that crystal clear.
Damodara
Damodara el 12 de Mayo de 2013
Yes I have tried your code. Its good what I expected. But again I am facing bit problem how can I get one pixel regions from your code.?Suppose an object has 756 pixels in an image. If I split that object to one pixel regions then it has to give 756. Is there any way to split without considering mask lines because it takes a pixel to draw a line as like mentioned in above code just draw grid and how many regions are there .?
Regards Dams
Image Analyst
Image Analyst el 12 de Mayo de 2013
I thought you wanted grid boxes over the object - that's what you said. If you want individual pixels, then the image is already in that form. Each element is a single pixel.
If you have an object, you can get the individual pixel coordinates using the PixelList measurement returned by regionprops(). You can get the pixel values using the PixelValues measurement returned by regionprops().
Damodara
Damodara el 12 de Mayo de 2013
Editada: Damodara el 12 de Mayo de 2013
Yes, I wanted grid boxes over the object and count number of grid boxes which cover only that object. Even I wanted to increase grid size from one pixel to size of that image that is where I stuck up. I can increase grid size in loop but how to count which covers only that object. ?
Image Analyst
Image Analyst el 12 de Mayo de 2013
I don't understand what you are asking. Time for you to upload a screenshot.
Damodara
Damodara el 13 de Mayo de 2013
Hi, How can I upload my screenshot here. ?
Regards Dams
Image Analyst
Image Analyst el 13 de Mayo de 2013
Activate your window, type alt-printscreen, go to http://snag.gy and type control-v. Then tell me the URL that you see.
Damodara
Damodara el 14 de Mayo de 2013
Hi,
How can count these grid boxes which cover that object. I wanted to count for different grid size. http://snag.gy/VOThM.jpg
Regards Dams
Image Analyst
Image Analyst el 14 de Mayo de 2013
How is that different than what I did? And your boxes are not one pixel wide. The grid lines are, but the boxes are not -they're wider than that, just like I had. What's the use of chopping your object up like this anyway?
Damodara
Damodara el 14 de Mayo de 2013
Hi,
Here grid size is not one pixel. Have tried my code which i posted.?I have given an example. I want to use it for fractal dimension. Regards Dams

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Convert Image Type en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by