I am trying to detect an object on a solid background using canny edge detection.
我正在尝试用精明的边缘检测在坚实的背景上检测一个物体。
I am able to get all the edges and draw rectangles around them but I'm struggling with drawing a rectangle around all the rectangles to hopefully use to crop out the object.
我可以得到所有的边,并画出矩形周围的矩形,但我正在努力画一个矩形,在所有矩形的周围,希望用它来裁剪出物体。
I am reasonably happy with the following code (which gave me the image above from the original image):
我对下面的代码非常满意(这段代码给了我上面的图片):
original = imread('1.jpg');
img = rgb2gray(original);
BW = edge(img,'canny',0.09);
[B,L,N,A] = bwboundaries(BW);
figure; imshow(BW); hold on;
for k=1:length(B),
if(~sum(A(k,:)))
boundary = B{k};
plot(boundary(:,2),boundary(:,1),'r','LineWidth',2);hold on;
end
end
blobMeasurements = regionprops(logical(BW), 'BoundingBox');
numberOfBlobs = size(blobMeasurements, 1);
rects = [];
for k = 1 : numberOfBlobs % Loop through all blobs.
rects = blobMeasurements(k).BoundingBox; % Get list ofpixels in current blob.
x1 = rects(1);
y1 = rects(2);
x2 = x1 + rects(3);
y2 = y1 + rects(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'LineWidth', 2);
end
But I'm not getting the following to work (Where I would hope to get the outer rectangle and crop the original image):
但是我并没有得到下面的工作(我希望得到外部矩形并裁剪原始图像):
% get min max
xmin=min(rects(:,1));
ymin=min(rects(:,2));
xmax=max(rects(:,1)+rects(:,3));
ymax=max(rects(:,2)+rects(:,4));
% define outer rect:
outer_rect=[xmin ymin xmax-xmin ymax-ymin];
rect = rectangle('Position',outer_rect));
crop = imcrop(original,rect);
figure
imshow(crop);
1 个解决方案
#1
0
Got it working with the following code
让它使用下面的代码。
original = imread('1.jpg');
img = rgb2gray(original);
BW = edge(img,'canny',0.08);
[B,L,N,A] = bwboundaries(BW);
for k=1:length(B),
if(~sum(A(k,:)))
boundary = B{k};
end
end
blobMeasurements = regionprops(logical(BW), 'BoundingBox');
numberOfBlobs = size(blobMeasurements, 1);
rectCollection = [];
for k = 1 : numberOfBlobs % Loop through all blobs.
rects = blobMeasurements(k).BoundingBox; % Get list ofpixels in current blob.
x1 = rects(1);
y1 = rects(2);
x2 = x1 + rects(3);
y2 = y1 + rects(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
rectCollection(k,:,:,:) = [x1; y1; x2; y2];
end
% get min max
xmin=min(rectCollection(:,1));
ymin=min(rectCollection(:,2));
xmax=max(rectCollection(:,3));
ymax=max(rectCollection(:,4));
% define outer rect:
outer_rect=[xmin ymin xmax-xmin ymax-ymin];
crop = imcrop(original,outer_rect);
imshow(crop);
#1
0
Got it working with the following code
让它使用下面的代码。
original = imread('1.jpg');
img = rgb2gray(original);
BW = edge(img,'canny',0.08);
[B,L,N,A] = bwboundaries(BW);
for k=1:length(B),
if(~sum(A(k,:)))
boundary = B{k};
end
end
blobMeasurements = regionprops(logical(BW), 'BoundingBox');
numberOfBlobs = size(blobMeasurements, 1);
rectCollection = [];
for k = 1 : numberOfBlobs % Loop through all blobs.
rects = blobMeasurements(k).BoundingBox; % Get list ofpixels in current blob.
x1 = rects(1);
y1 = rects(2);
x2 = x1 + rects(3);
y2 = y1 + rects(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
rectCollection(k,:,:,:) = [x1; y1; x2; y2];
end
% get min max
xmin=min(rectCollection(:,1));
ymin=min(rectCollection(:,2));
xmax=max(rectCollection(:,3));
ymax=max(rectCollection(:,4));
% define outer rect:
outer_rect=[xmin ymin xmax-xmin ymax-ymin];
crop = imcrop(original,outer_rect);
imshow(crop);