Determining pixel size based on an image of a scale/ruler.

I have an image with a scale on it, and I would like to know the size of each pixel. So that way, I can say that an area of x by x pixels, is equal to y mm^2. What's the best way to go about doing this? Here is the image sample:
My initial idea was to use IM profile along the lines, and determine the pixel distance between the intensity drops. However, if I don't draw my profile perfectly perpendicular, then this approach will not work.
Because I have a large data set of images that all have different scales, I would like to automated this process if possible. Something along the lines of detecting these dark lines, and then determining the minimum distance between neighboring lines.
Thanks in advance.

Answers (2)

See my spatial calibration demo, attached.
Of course you can do it automatically if you can automatically find the lines and know their true separation.

4 Comments

Hello. Thank you for your prompt reply. I am still concerned that the line I draw is too subjective. If I draw it not exactly perpendicular to my scale in the image, then it will not function properly. Is there a way to first detect the black lines and use the shortest line between them as the input to this demo?
I would like it to be as accurate as possible, and subjectively drawing the line doesn't provide the best solution.
Take the red signal. Threshold it. Label it. Throw out the two largest blobs with my attached function. Extract the 3 largest remaining blobs with my attached function. Then find the centroids. Find the farthest distance between them. That will be 10 mm or 1 mm - whatever it is. Here's a start:
redSignal = rgbImage(:,:,1);
binaryImage = redSignal < 128; % Or whatever number works best.
% Get rid of mm letters.
binaryImage = binaryImage - ExtractNLargestBlobs(binaryImage, 2);
% Extract 3 longest lines.
binaryImage = ExtractNLargestBlobs(binaryImage, 3);
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Centroid');
Let me know if you need further help.
Alright, attached is code that does it for you. If you agree, can you mark the Answer as "Accepted". Be sure to change the filename and folder in the attached test.m file.
Hello image analyst,
Would you help me solving my problem to identify the location of red colour in coordinat mm?

Sign in to comment.

Get your scale image in your current figure axis and call function imdistline. it will create a distance measuring tool in your figure. Drag the point of the line created by the function to your known positions of the scale image, for example two major ticks of the image which are 5 mm apart.Then read this distance in the pixel values, say 'x'. Then your pixel size calibration will be equal to 5/x in mm/pixel.

Categories

Asked:

on 29 Jan 2015

Commented:

on 16 Oct 2020

Community Treasure Hunt

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

Start Hunting!