How to calculate area on image ?

40 views (last 30 days)
Paul Germann
Paul Germann on 6 Apr 2021
Hello,
for a project, I need to be able to input an image with a ruler on it. Through processing, I should be able to convert pixel to metrics and then select different areas that display the value.
The idea would be first to select and design a line with the cursor on the ruler and second to select an area.
I saw different possibiliy on internet with e.g images.roi.Line and images.roi.Rectangle. But I don't know how to link them because since I change pixel to metrics with images.roi.Line, images.roi.Rectangle doesn't follow with metrics.
thank you,
  2 Comments
darova
darova on 6 Apr 2021
you can use bwselect to select specified region (in binary image)
Paul Germann
Paul Germann on 7 Apr 2021
Thank you for your answer but it is not a binary image unfortunately

Sign in to comment.

Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 22 Feb 2024
Hello Paul,
I understand you are trying to measure distances in an image using a ruler for scale. The process involves using the images.roi.Line to define the scale based on the known length of the ruler, and then using images.roi.Rectangle or other ROI objects to select areas and measure them in the defined scale.
To make sure the images.roi.Rectangle follows the scale set by images.roi.Line, you need to calculate the pixels-to-metric conversion factor and then apply it to the measurements obtained with the rectangle ROI. Here's an outline of how you might proceed:
  • Select the Ruler Line: Use images.roi.Line to draw a line along the ruler in the image. You need to know the actual length that this line represents in real-world units (e.g., centimeters).
  • Calculate Conversion Factor: Once you have the length of the line in both pixels and real-world units, you can calculate the conversion factor as realWorldUnits / pixels.
  • Select Areas to Measure: Use images.roi.Rectangle or any other ROI object to select the areas you want to measure in the image.
  • Apply Conversion Factor: Measure the dimensions of the selected area in pixels and then apply the conversion factor to translate these measurements into real-world units.
Here's a simplified example of how the code might look:
% Load the image
image = imread('your_image_with_ruler.jpg');
imshow(image);
% User draws a line along the ruler
hLine = drawline;
% Get the position of the line endpoints
linePosition = hLine.Position;
% Calculate the length of the line in pixels
pixelDistance = sqrt(sum((linePosition(2,:) - linePosition(1,:)).^2));
% Known length of the ruler in real-world units (e.g., centimeters)
realWorldLength = 10; % for example, if the ruler shows 10 cm
% Calculate the pixels-to-metrics conversion factor
conversionFactor = realWorldLength / pixelDistance;
% Now user can draw a rectangle to select an area
hRect = drawrectangle;
% Get the position and size of the rectangle in pixels
rectPosition = hRect.Position;
rectWidthPixels = rectPosition(3);
rectHeightPixels = rectPosition(4);
% Convert the rectangle dimensions to real-world units
rectWidthRealWorld = rectWidthPixels * conversionFactor;
rectHeightRealWorld = rectHeightPixels * conversionFactor;
% Display the measured dimensions
fprintf('Width: %.2f cm, Height: %.2f cm\n', rectWidthRealWorld, rectHeightRealWorld);
Output Image :
I hope it helps!

Community Treasure Hunt

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

Start Hunting!