finding length of scale bar from matrix form of image

11 Ansichten (letzte 30 Tage)
I have a grey image which I have converted into matrix form, but below the image there is a scale bar. The scale bar is surrounded by white background and scale bar is black. I am wondering if there is a way to code something to go through my matrix of the whole image until it detects the first black pixel (the first black pixel on the scale bar)
  1 Kommentar
KSSV
KSSV am 10 Jan. 2020
Attach your image and tell us more clearly, what you are expecting.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Constantino Carlos Reyes-Aldasoro
As said before, not too difficult if you know what you need. Here is the code
% Read the image
imageWithScaleBar = imread('/Users/ccr22/Desktop/1.png');
%display to visualise
imagesc(imageWithScaleBar(:,:,1))
% select the low intensity pixels notice that you only need one
% channel of the RGB as it is a gray scale image
% You need to close as there may be small gaps in the scale bar
blackPart = imclose(imageWithScaleBar(:,:,1)<20,ones(1,3));
% find the major axis length to know which is the scale bar and
% the bounding box for the position
blackPartLabelled = bwlabel(blackPart);
imagesc(blackPartLabelled)
darkParts.jpg
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:
a =
173.2051
b =
85
Now you know that the object you want is number 85, so select it
% so the object in question is label 85
scaleBar = (blackPartLabelled==b);
imagesc(scaleBar)
scaleBar.jpg
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.
  2 Kommentare
Xanm ft
Xanm ft am 10 Jan. 2020
thank you very much, just wondering also if there is a way to detect the first black pixel in the image - maybe using a for loop?
Constantino Carlos Reyes-Aldasoro
Bearbeitet: Constantino Carlos Reyes-Aldasoro am 10 Jan. 2020
In Matlab you rarely use for-loops in that sense as Matlab is a Matrix-oriented environment. Put it another way, if you need a for-loop, think if there is a better way to do it, as Matlab has thousands of pre-defined functions, e.g.
>> [r,c]=find(scaleBar,1,'first')
r =
189
c =
97
>> [r,c]=find(scaleBar,1,'last')
r =
189
c =
246
>>
In this case you find the first and last pixels of the scaleBar, but you could also retrieve all of them
>> [r,c]=find(scaleBar);
>> size(r)
ans =
150 1
In summary, to use Matlab effectively you need to "think" in Matlab rather than thinking how traditional programming environments like C or C++ work.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Constantino Carlos Reyes-Aldasoro
This should be easy by thresholding for the levels expected (black will be low in the three RGB channels, but not necessarily 0) and then use find() to detect the locations of those pixels. But as said previously, attach your image if you need more help.
  1 Kommentar
Xanm ft
Xanm ft am 10 Jan. 2020
okay basically I'm trying to find the length of the scale bar in pixels on the image. I'm really new to matlab so if you could explain it a bit it would really help me out.
or if there is a code to go through and detect the first black pixel on the scale bar as i tried to explain above

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by