[英]Shifting of trace boundaries in Matlab code?
I am trying to trace boundaries of objects using a manual ROI selection and to plot there outlines back on the original image in grayscale.我正在尝试使用手动 ROI 选择来追踪对象的边界,并在 plot 处以灰度方式在原始图像上勾勒出轮廓。 I noticed that I have a shift of the outline compered to the original object location.
我注意到我将轮廓偏移到原始 object 位置。 Why?
为什么? did I missed something in my code?
我在我的代码中遗漏了什么吗?
Code:代码:
close all;
clear all;
clc;
I = imread('pillsetc.png');
figure('Name','pillsetc');
imshow(I)
x1 = 50;
y1 = 200
Iroi = imcrop(I,[x1,y1,400,150]);
GrayRoi = rgb2gray(Iroi);
figure('Name','pillsetcGrayCrop');
imshow(GrayRoi);
BWRoi = imbinarize(GrayRoi);
BWRoi = bwareaopen(BWRoi, 10);
BWRoi = imfill(BWRoi,'holes');
[B,L] = bwboundaries(BWRoi,'noholes');
stat = regionprops(L, 'Centroid');
figure('Name','pillsetcCropBoundaries');
imshow(rgb2gray(I));
hold on;
for k = 1 :numel(stat)
b = B{k};
c = stat(k).Centroid;
plot(b(:,2)+x1, b(:,1)+y1,'r');
end
Looks like your image is not as binary as regionprops prefers.看起来您的图像不像 regionprops 喜欢的那样二进制。 The regionprops function appears to be Left-to-Right raster scanning for the brightest to start and darkest to stop, and delays any action on gradients.
regionprops function 似乎是从左到右的光栅扫描,以寻找最亮的开始和最暗的停止,并延迟对渐变的任何操作。
To put it simply:简而言之:
So if you make make the dark background blur together to the same darkest level and then spike any non-dark to the brightest foreground (any grey above bottom couple darkest levels is set to white) before processing with regionprops, you might get better outlines.因此,如果在使用 regionprops 进行处理之前,将暗背景模糊到相同的最暗级别,然后将任何非暗背景添加到最亮的前景(底部一对最暗级别之上的任何灰色都设置为白色),您可能会得到更好的轮廓。
1.- Use MATLAB command bwboundaries
1.-使用 MATLAB 命令
bwboundaries
close all;clear all;clc
A=imread('001.jpg');
A2=rgb2gray(A);
BW = imbinarize(A2);
[B,L] = bwboundaries(BW,'noholes');
figure;
imshow(label2rgb(L, @jet, [.5 .5 .5]))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
Without the colouring generated with label2rgb
没有使用
label2rgb
生成的着色
figure;
imshow(L);
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
2.- Numbering all found boundaries 2.-对所有找到的边界进行编号
[B,L,N,A] = bwboundaries(BW);
figure;
imshow(BW); hold on;
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(B)
boundary = B{k};
cidx = mod(k,length(colors))+1;
plot(boundary(:,2), boundary(:,1),colors(cidx),'LineWidth',2);
rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));
col = boundary(rndRow,2); row = boundary(rndRow,1);
h = text(col+1, row-1, num2str(L(row,col)));
set(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');
end
Too many elements元素太多
B
30×1 cell array
{446×2 double}
{146×2 double}
{ 3×2 double}
{776×2 double}
{150×2 double}
{343×2 double}
{ 2×2 double}
{ 2×2 double}
{408×2 double}
...
{ 2×2 double}
{543×2 double}
{ 2×2 double}
{ 2×2 double}
{ 2×2 double}
{441×2 double}
{ 2×2 double}
{ 2×2 double}
size(B)
=
30 1
3.- Removing small elements 3.-删除小元素
th1=100; % threshold
B2={}
for k=1:1:size(B,1)
B0=B{k};
if size(B0,1)>th1
B2=[B2 B0];
end
end
figure;
imshow(BW); hold on;
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(B2)
boundary = B2{k};
cidx = mod(k,length(colors))+1;
plot(boundary(:,2), boundary(:,1),colors(cidx),'LineWidth',2);
rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));
col = boundary(rndRow,2); row = boundary(rndRow,1);
h = text(col+1, row-1, num2str(L(row,col)));
set(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');
end
4.- Other interesting commands: 4.-其他有趣的命令:
imfill
: fill holes. imfill
: 填洞。 Specific locations can be fed.可以喂特定位置。
imdilate
: Dilating thin lines may also be useful imdilate
:扩张细线也可能有用
imclearborder
: file rough edges imclearborder
:文件粗糙的边缘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.