ROI Trace boundaries result
1 view (last 30 days)
Show older comments
I did a trace boundaries on the coins image using a manual ROI selection. I ploted their outlines back on the original image in grayscale.
I noticed that I have a shift of the outline compered to the original object location. I presume that this is because the threshold process is running on the ROI and not on the full image. Am I correct? What is more correct threshold on all the image and crop or threshold on the ROI?
Code:
close all;
clear all;
clc;
I = imread('coins.png');
figure('Name','Original');
imshow(I)
x1 = 0;
y1 = 75
Iroi = imcrop(I,[x1,y1,150,150]);
figure('Name','ROI');
imshow(Iroi);
GrayRoi = (Iroi);
BWRoi = imbinarize(GrayRoi);
BWRoi = bwareaopen(BWRoi, 100);
BWRoi = imfill(BWRoi,'holes');
[B,L] = bwboundaries(BWRoi,'noholes');
stat = regionprops(L, 'Centroid');
figure('Name','Original+outline');
imshow(I);
hold on;
for k = 1 :numel(stat)
b = B{k};
c = stat(k).Centroid;
plot(b(:,2)+x1,b(:,1)+y1,'r' );
end
0 Comments
Answers (2)
Steve Eddins
on 14 Jan 2022
I think some careful coordinate system tweaks will get you there. In many Image Processing Toolbox functions, pixels are treated as squares having a width and height of 1. If you want the upper-left corner of your crop region to include the pixel centered at x=1, y=75, then set your upper-left crop corner to be x1=0.5, y1=74.5 so that you are perfectly unambiguous about which pixels are inside the crop region. Then, when you add x1 and y1 back to the coordinates returned by bwboundaries, there is another offset to consider. If the upper-left crop corner is at x=0.5, y=74.5, then the original pixel centered at x=1,y=75 becomes xc=1,yc=1 in the cropped image. If bwboundaries tells you that there is a boundary point there, at xc=1,yc=1, you need to add x1-0.5 and y1-0.5 to shift (1,1) to the expected location of (1,75).
I = imread('coins.png');
x1 = 0.5;
y1 = 74.5;
Iroi = imcrop(I,[x1,y1,150,150]);
GrayRoi = (Iroi);
BWRoi = imbinarize(GrayRoi);
BWRoi = bwareaopen(BWRoi, 100);
BWRoi = imfill(BWRoi,'holes');
[B,L] = bwboundaries(BWRoi,'noholes');
stat = regionprops(L, 'Centroid');
figure('Name','Original+outline');
imshow(I);
hold on;
for k = 1 :numel(stat)
b = B{k};
c = stat(k).Centroid;
plot(b(:,2)+x1-0.5,b(:,1)+y1-0.5,'r' );
end
0 Comments
yanqi liu
on 17 Jan 2022
yes,sir,may be add some process to logical image,such as
close all;
clear all;
clc;
I = imread('coins.png');
figure('Name','Original');
imshow(I)
x1 = 0;
y1 = 75;
Iroi = imcrop(I,[x1,y1,150,150]);
figure('Name','ROI');
imshow(Iroi);
GrayRoi = (Iroi);
BWRoi = imbinarize(GrayRoi);
BWRoi = bwareaopen(BWRoi, 100);
BWRoi = imclose(BWRoi, strel('disk', 5));
BWRoi = imfill(BWRoi,'holes');
[B,L] = bwboundaries(BWRoi,'noholes');
stat = regionprops(L, 'Centroid');
figure('Name','Original+outline');
imshow(I);
hold on;
for k = 1 :numel(stat)
b = B{k};
c = stat(k).Centroid;
plot(b(:,2)+x1,b(:,1)+y1,'r' );
end
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!