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
