RAO*_*RAO 5 matlab image-processing
我正在开发一个MATLAB项目,用户将上传椭圆状物体的扫描图像,程序必须计算图像中每个物体的测量值(高度,宽度,面积等).
我开始使用阈值处理产生二进制图像(黑色背景和白色独立对象).这是阈值处理后的BW图像:
在那之后,我使用regionprops它,因为它返回了我需要的大部分测量,并且它完美地工作.
问题是功能"识别/检测"对象的顺序不一致.我添加了一个代码来显示每个对象的数量,这样我就可以知道哪个对象regionprops被认为是第一个,哪个是第二个..等等.
代码:
% read image
rgb=imread('bw');
s = regionprops(bw,'Area', 'BoundingBox', 'Eccentricity', 'MajorAxisLength', 'MinorAxisLength', 'Orientation', 'Perimeter','Centroid');
% show the number of each object
imshow(bw)
hold on;
for k = 1:numel(s)
c = s(k).Centroid;
text(c(1), c(2), sprintf('%d', k), ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', 'color', 'r');
end
hold off;
Run Code Online (Sandbox Code Playgroud)
这是显示对象顺序后的图像:
我需要订单从左上角到右下角.(第一行对象编号为1到6,第二行编号为7到12 ..等等).那可能吗?
非常感谢.
根据Suever 的使用Centroid数据的建议,并假设s示例中仅包含 18 个感兴趣的区域,以下是使用和s从左到右、从上到下排序的一种方法:histcountssortrows
coords = vertcat(s.Centroid); % 2-by-18 matrix of centroid data
[~, ~, coords(:, 2)] = histcounts(coords(:, 2), 3); % Bin the "y" data
[~, sortIndex] = sortrows(coords, [2 1]); % Sort by "y" ascending, then "x" ascending
s = s(sortIndex); % Apply sort index to s
Run Code Online (Sandbox Code Playgroud)
这是一张显示每个区域标签的图片(正如您在代码示例中所做的那样):
“y”数据的分箱首先允许我们将对象分为 3 行,每行 6 个。该sortrows函数在按此分箱值排序后,能够对每个唯一分箱组的所有“x”值进行子排序。
| 归档时间: |
|
| 查看次数: |
1247 次 |
| 最近记录: |