Shifting of trace boundaries in Matlab code?
Asked Answered
B

2

8

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. I noticed that I have a shift of the outline compered to the original object location. 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

enter image description here

Baptistry answered 2/1, 2022 at 21:12 Comment(3)
needs source data to replicateUnderprop
Please explain what do you mean by "needs source data to replicate"? this is a built in image in MatlabBaptistry
Are you sure that you are doing everything correct? I applied your script for an image, it looks all good to me, i.e. no boundary shift like you have. Even if I crop my image, the boundaries are still at where they are supposed to be. Just to be sure, can you try your script without cropping your image? And it would be nice if you can upload your original image pillsetc.png, so I can work on it.Pile
A
0

1.- Use MATLAB command 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

enter image description here

Without the colouring generated with label2rgb

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

enter image description here

2.- Numbering all found boundaries

[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

enter image description here

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

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

enter image description here

4.- Other interesting commands:

imfill : fill holes. Specific locations can be fed.

imdilate : Dilating thin lines may also be useful

imclearborder : file rough edges

Aquifer answered 25/1, 2023 at 20:26 Comment(0)
A
-3

Looks like your image is not as binary as regionprops prefers. 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.

To put it simply:

  • Upon the brightest (similar to white) of a gradient, selection starts
  • Upon the darkest (similar to black) of a gradient, selection ends

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.

Advowson answered 6/1, 2022 at 21:22 Comment(2)
BaTycoon - the regionprop is done on binary ROI. The question how to overlay them correct remain open.Merissa
AsiJapan - The ROI is a grayscale cropped portion of the image shown above, what are you suggesting? I am suggesting the function regionprop is not as sophisticated as expected on a scale of gray.Advowson

© 2022 - 2024 — McMap. All rights reserved.