As said before, not too difficult if you know what you need. Here is the code
imageWithScaleBar = imread('/Users/ccr22/Desktop/1.png');
imagesc(imageWithScaleBar(:,:,1))
blackPart = imclose(imageWithScaleBar(:,:,1)<20,ones(1,3));
blackPartLabelled = bwlabel(blackPart);
imagesc(blackPartLabelled)
You can see that each dark section of your image has a different colour, i.e. a label, now you need to know which is the one you want, i.e. the long thin one, so look for the major axis length
blackPartProperties = regionprops(blackPartLabelled,'MajoraxisLength','boundingbox');
[a,b]=(max([blackPartProperties.MajorAxisLength]));
When you run this you will have the output of a and b as there is no echo inhibitor in the last line:
Now you know that the object you want is number 85, so select it
scaleBar = (blackPartLabelled==b);
imagesc(scaleBar)
So now you have the scale bar, I have used the data tips to show where it starts manually but you can get this directly by retrieving the bounding box previously calculated:
>> blackPartProperties(b).BoundingBox
ans =
96.5000 188.5000 150.0000 1.0000
that gives you the coordinates where your scale bar is located and that solves the problem. Do let me know if you have questions and if this solves your problem, please accept the answer.
Best Answer