Matlab代码中轨迹边界的移动?

q1qsirdb  于 2023-01-26  发布在  Matlab
关注(0)|答案(2)|浏览(209)

我尝试使用手动ROI选择来跟踪对象的边界,并在原始图像上以灰度绘制轮廓。我注意到,与原始对象位置相比,轮廓发生了偏移。为什么?我的代码中遗漏了什么吗?
代码:

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

ltskdhd1

ltskdhd11#

**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

没有label2rgb生成的颜色

figure;
imshow(L);
hold on
for k = 1:length(B)
   boundary = B{k};
   plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end

**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

元素过多

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.-**移除小元件

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.-**其他有趣的命令:

imfill:补孔,可补特定位置。
imdilate:扩展细线也可能有用
imclearborder:锉粗边

egmofgnx

egmofgnx2#

看起来您的图像并不像regionprops喜欢的那样是 * 二进制 * 的。regionprops函数似乎是从左到右的光栅扫描,最亮的开始,最暗的停止,并延迟任何渐变操作。
简单来说:

  • 在渐变最亮(类似于白色)时,选择开始
  • 在渐变最暗(类似于黑色)时,选择结束

因此,如果您使暗背景一起模糊到相同的最暗级别,然后在使用区域属性处理之前,将任何非暗背景加到最亮的前景(底部两个最暗级别以上的任何灰色都设置为白色),您可能会得到更好的轮廓。

相关问题