Height and width of non uniform image.
Show older comments
Is there any idea how to calculate height and width of region of image? I want to measure the height and width of selected region as shown in this image.
Answers (3)
Walter Roberson
on 19 Jan 2012
0 votes
regionprops() ?
1 Comment
Walter Roberson
on 19 Jan 2012
Yup. Label the image, regionprops() the labeled image, look at the bounding box.
Image Analyst
on 19 Jan 2012
0 votes
Images have to be rectangular. For the image
[rows columns numberOfColorChannels] = size(imageArray);
For the region within the image, you need to do as Walter says, get a binary image, call bwlabel, then call regionprops. See BlobsDemo http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862 if you need an image segmentation tutorial/demo.
14 Comments
Syahrul Niezam
on 19 Jan 2012
Walter Roberson
on 19 Jan 2012
Yes. regionprops() does not care how you generate the labeled matrix, only that it receives a labeled matrix. And at a pinch, even a binary matrix qualifies as a labeled matrix: everywhere set to 1 binary would be considered to be part of the same region.
David Young
on 19 Jan 2012
Not exactly. If the matrix is of class double, and is binary in the sense of containing only values 0 and 1, then yes, everywhere set to 1 is part of the same (possibly disconnected) region.
However, if the matrix is of class logical, then regionprops treats each connected region of 1s as separate, as if bwlabel(bw) had been passed instead of bw.
Walter Roberson
on 19 Jan 2012
Creeping Featuritis! ;-)
Image Analyst
on 19 Jan 2012
Yeah, I noticed that a couple of versions ago. If you have a binary image (say from thresholding) you can pass it right in to regionprops() without using bwlabel. I still use bwlabel though - force of habit I guess, or just to be super explicit. I think now though it actually probably passes the binary image to bwconncomp() since it seems like they're trying to promote that as the replacement for bwlabel().
Syahrul Niezam
on 19 Jan 2012
Walter Roberson
on 19 Jan 2012
What output do you get from segmenting? How can you tell the different segments apart?
Image Analyst
on 20 Jan 2012
The threshold method DOES give a binary image:
binaryImage = grayImage < thresholdValue;
% binaryImage is a logical image.
Syahrul Niezam
on 20 Jan 2012
Image Analyst
on 20 Jan 2012
So is it working now?
Syahrul Niezam
on 20 Jan 2012
Image Analyst
on 21 Jan 2012
I don't see the 4 regions. I see (1) white background, (2) gray blob, (3) red blob, (4) arrow #1, and (5) arrow #2. Which regions do you want to measure?
Syahrul Niezam
on 21 Jan 2012
Image Analyst
on 22 Jan 2012
Just use 1,2,3 instead of 180, 200, and 255 and you'll have a labeled image. a and b must be gray scale images.
Image Analyst
on 21 Jan 2012
Try it like this instead:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
rgbImage = imread('C:\Users\Syahrul\Documents\Temporary\szuzpc.jpg');
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
subplot(2, 3, 2);
imshow(redChannel, []);
title('Red Channel Image', 'FontSize', fontSize);
subplot(2, 3, 3);
imshow(greenChannel, []);
title('Green Channel Image', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(blueChannel, []);
title('Blue Channel Image', 'FontSize', fontSize);
% Combine channels to get just the red blob only.
redBlob = blueChannel <= 50 & redChannel > 50;
% Get rid of blobs less than 1000 in pixel area.
redBlob = bwareaopen(redBlob, 1000);
subplot(2, 3, 5);
imshow(redBlob, []);
% Label each blob with 8-connectivity, so we can make measurements of it
[labeledImage numberOfBlobs] = bwlabel(redBlob, 8);
% Apply a variety of pseudo-colors to the regions.
coloredLabelsImage = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');
% Display the pseudo-colored image.
imshow(coloredLabelsImage);
axis on;
title('Red Blob Alone', 'FontSize', fontSize);
% Get all the blob properties.
blobMeasurements = regionprops(labeledImage, 'area', 'BoundingBox');
numberOfBlobs = size(blobMeasurements, 1);
area = [blobMeasurements.Area] % Vector of all areas (should only be 1).
% Get the bounding box.
bb = [blobMeasurements.BoundingBox] % Vector of all areas (should only be 1).
x1 = bb(1)
x2 = x1 + bb(3)
y1 = bb(2)
y2 = y1 + bb(4)
% Plot red blob with box around it.
subplot(2, 3, 6);
imshow(coloredLabelsImage, []);
axis on;
title('Red Blob With Bounding Box', 'FontSize', fontSize);
hold on;
plot([x1 x2 x2 x1 x1], [y1 y1 y2 y2 y1], 'y-', 'LineWidth', 2);
% Display message
message = sprintf('Done with demo!\nWidth = %f\nHeight = %f', bb(3), bb(4));
msgbox(message);
5 Comments
Syahrul Niezam
on 21 Jan 2012
Syahrul Niezam
on 21 Jan 2012
Image Analyst
on 22 Jan 2012
Um,these images are TOTALLY different than the computer graphics image you gave us first. These are thermal grayscale images of a real outdoor scene. So of course the code I wasted my time on for your computer graphics image won't work for this image. Good luck with this.
Syahrul Niezam
on 22 Jan 2012
Image Analyst
on 22 Jan 2012
See my BlobsDemo in my File Exchange for example/tutorial. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!