finding length of scale bar from matrix form of image
10 次查看(过去 30 天)
显示 更早的评论
Xanm ft
2020-1-10
编辑: Constantino Carlos Reyes-Aldasoro
2020-1-10
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 个评论
采纳的回答
Constantino Carlos Reyes-Aldasoro
2020-1-10
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)
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)
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 个评论
Constantino Carlos Reyes-Aldasoro
2020-1-10
编辑:Constantino Carlos Reyes-Aldasoro
2020-1-10
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.
更多回答(1 个)
Constantino Carlos Reyes-Aldasoro
2020-1-10
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!