在边缘图像中查找最大轮廓的像素

时间:2017-08-01 20:13:50

标签: matlab opencv image-processing

我在图像处理中遇到这个问题,我找不到一个算法来解决这个问题。理解起来很简单,但我不知道如何在OpenCV或{{1}中实现它所以其中一个(MATLAB或opencv)中的任何算法或函数都是有用的。

1 。假设我有一个Matlab和场景背景,如下所示

enter image description here

2 。我将边缘检测器应用于image,而我的image将如下图所示。

enter image description here

我的问题是如何在current image图片中找到如下所示的最大轮廓或区域

enter image description here

如果您想要原始图片,原始图片是 enter image description here

在matlab中,您可以通过以下代码获得边缘图像。

edge

在OpenCV中,您可以使用以下代码

clc
clear

img = imread('1.png');                 % read Image
gray = rgb2gray(img);                  % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09);   % apply canny to gray-scale image
imshow(edgeImage)                      % Display result in figure(MATLAB)

2 个答案:

答案 0 :(得分:1)

首先用形态学闭合来关闭轮廓,因为你现在无法找到它,因为它不是一个真正独特的轮廓,而是较大轮廓的一部分。

关闭后,只需使用findContours()函数并使用其输出来获取每个轮廓的区域,并最终使用contourArea()函数找到最大值。

答案 1 :(得分:1)

以下是Matlab中使用imdilate关闭轮廓和regionprops来获取闭合对象区域的解决方案:

% Your code to start
img = imread('Image.png');             % read Image
gray = rgb2gray(img);                  % Convert RGB to gray-scale
edgeImage = edge(gray,'canny',0.09);   % apply canny to gray-scale image

% First dilate to close contours
BW = imdilate(edgeImage, strel('disk',4,8));

% Then find the regions
R = regionprops(~BW, {'Area', 'PixelIdxList'});

% Find the second biggest region (the biggest is the background)
[~, I] = sort([R(:).Area], 'descend');
Mask = zeros(size(img));
Mask(R(I(2)).PixelIdxList) = 1;

% Display
clf
imshow(Mask)

结果是:

The result: Mask

最佳,

相关问题