Height and width of non uniform image.

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)

regionprops() ?

1 Comment

Yup. Label the image, regionprops() the labeled image, look at the bounding box.

Sign in to comment.

Image Analyst
Image Analyst on 19 Jan 2012
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

is it possible to do segmentation into several areas, not binarization before labeling?
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.
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.
Creeping Featuritis! ;-)
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().
however, i'm facing problem in labeling the image, after segment it into several regions via threshold method. The bwlabel function is supposedly for binary imagery only. :(
What output do you get from segmenting? How can you tell the different segments apart?
The threshold method DOES give a binary image:
binaryImage = grayImage < thresholdValue;
% binaryImage is a logical image.
I segmented the image into four regions-background and three different regions within the subject. However, i failed to do labeling.
So is it working now?
I tried using code given in my previous questions like this:
clear all;
a = imread('image1_069.jpg');
b = zeros(size(a));
b = uint8(a<90);% In this order!
b(a >= 110) = 180;
b(a >= 150) = 200;
b(a >= 200) = 255;
figure(1);
imshow(b);
[X Y] = size(b);
OW = zeros(X, Y);
L = 1;
tab = zeros(255);
b(1,:)=0; b(xsize,:)=0; b(:,1)=0;
b(:,ysize)=0; %????
label=0;
for kx=2:xsize-1
for ky=2:ysize-1
W=zeros(3);
W(1:3, 1:3)=b(kx-1:kx+1,ky-1:ky+1); %8??
maxW=max(W(:));
if b(kx, ky)==1 & b(kx,ky-1)==0 & maxW==1
label=label+1;
b(kx,ky)=label;
end
if b(kx,ky)==1 & maxW>=1
b(kx,ky)=maxW;
end
end
end
figure(2);
imshow(OW, []);
However, the labeling part is not well. I just got the code from the matlab central but i don't understand it. I want to labeled the four segmented regions. Do you understand the code, and find any mistakes?
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?
oh, is it mistakes there in this code?
b = uint8(a<90);% In this order!
b(a >= 110) = 180;
b(a >= 150) = 200;
b(a >= 200) = 255;
Is this code convert the image into gray-scale and rgb image? i thought it changes the image into four different regions (black, dark gray, light gray and white).
I plan to segment the infrared gray-scale image into four different regions image- (black, dark gray, light gray and white regions) using threshold before labeling them.
Is there any suggestion to do that?
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.

Sign in to comment.

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

thank you very much for your help. i'll try this codes :)
btw, this is the image i got after running my previous codes:
http://i42.tinypic.com/21o0dxc.jpg
I'm sorry. I tried to apply this codes to infrared grey-scale image, but it failed.
this is the image i used.
http://i42.tinypic.com/oa3jtl.jpg
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.
I'm sorry for making you trouble.
My first question - the height and width, is the consequence from labeled images. I want to calculated the width and height of human images after segmentation (into four regions-white, light gray, dark gray and black) and labeling.
See my BlobsDemo in my File Exchange for example/tutorial. http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862

Sign in to comment.

Asked:

on 19 Jan 2012

Community Treasure Hunt

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

Start Hunting!