【问题标题】:Find biggest contour for shadow detection找到阴影检测的最大轮廓
【发布时间】:2020-04-15 22:20:36
【问题描述】:

我在图像处理中遇到了这个问题,我找不到在这种情况下表现良好的算法。它很容易理解,但我不知道如何在 ‍‍OpenCV 或 Matlab 中实现它,因此其中一个(MATLAB或opencv)中的任何算法或函数都是有帮助的。

1 。假设image 和场景背景如下所示

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

现在我的问题是我们如何在edge 图像中找到最大的轮廓或区域,如下图所示?


如果你想要原图原图是

在matlab中你可以通过下面的代码得到边缘图像。

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)

在 OpenCV 中你可以使用下面的代码

#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
   Mat img = imread("1.png");

   Mat gray;
   cvtColor(img,           //BGR form image
            gray,          //Mat for gray(destination)
            CV_BGR2GRAY);  //type of transform(in here BGR->GRay)

   Mat edgeImage;
   Canny(gray,       //Input Array
         edgeImage,  //Output Array
         40,         // Lower threshold
         120);        //Upper threshold

   namedWindow("Edge-Image");       //create a window for display image
   imshow("Edge-Image",edgeImage);  //Display edgeImage in window that in before line create
   waitKey(0);                      //stick display window and wait for any key

   return 0;

}

【问题讨论】:

  • 您应该提供您的输入图像,以便其他人可以根据需要处理它们
  • @Piglet 感谢您的评论,但是这张图片是从一篇文章中裁剪出来的,并且在那篇文章中作者没有解释他们如何识别这个区域,所以很遗憾我没有它,但我认为如果您将边缘检测器应用于大多数图像,则会有一个最大的轮廓。但如果这有帮助,我可以从文章中裁剪图像

标签: matlab opencv image-processing


【解决方案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)

结果是:

最好的,

【讨论】:

    【解决方案2】:

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

    关闭后,只需使用findContours()函数,并使用其输出得到每个轮廓的面积,并最终使用contourArea()函数找到最大值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-04
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 2020-09-14
      • 1970-01-01
      相关资源
      最近更新 更多