Count grid boxes inside an object in an image.

2 views (last 30 days)
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

Accepted Answer

Image Analyst
Image Analyst on 11 May 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 Comments
Image Analyst
Image Analyst on 14 May 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 on 14 May 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

Sign in to comment.

More Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!