Height and width of non uniform image.
2 views (last 30 days)
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.
0 Comments
Answers (3)
Walter Roberson
on 19 Jan 2012
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
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
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
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!