寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)

一、问题提出

这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-holes/)整编如下:

title:OpenCV / C++ - Filling holes

content:

Hello there,

For a personnel projet, I'm trying to detect object and there shadow. These are the result I have for now: Original: 

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第1张图片题,原始问题

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第2张图片

Object: 

Shadow: 

The external contours of the object are quite good, but as you can see, my object is not full. Same for the shadow. I would like to get full contours, filled, for the object and its shadow, and I don't know how to get better than this (I juste use "dilate" for the moment). Does someone knows a way to obtain a better result please? Regards.

二、问题分析

从原始图片上来看,这张图片的拍摄的背景比较复杂,此外光照也存在偏光现象;而提问者虽然提出的是“将缝隙合并”的要求,实际上他还是想得到目标物体的准确轮廓。

三、问题解决

基于现有经验,和OpenCV,GOCVhelper等工具,能够很快得出以下结果

h通道:

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第3张图片

去光差:

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第4张图片

阈值:

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第5张图片

标注:

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)_第6张图片

 

四、算法关键

这套算法首先解决了这个问题,而且我认为也是稳健鲁棒的。其中,算法中除了经典的“hsv分解->ostu阈值->最大轮廓标注”外,最为关键的算法为顶帽去光差。这个算法来自于冈萨雷斯《数字图像处理教程》形态学篇章,完全按照书本建议实现,体现良好作用。

#include "stdafx.h"
#include 
#include 
 
 
using namespace cv;
using namespace std;
 
//find the biggest contour
vector FindBigestContour(Mat src){    
    int imax = 0;  
    int imaxcontour = -1;  
    std::vector >contours;    
    findContours(src,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
    for (int i=0;i rgb_planes;
    split(src_hsv, rgb_planes );
    src_h = rgb_planes[0]; // h channel is useful
 
    src_h = moveLightDiff(src_h,40);
    threshold(src_h,bin,100,255,THRESH_OTSU);
 
    //find and draw the biggest contour
    vector bigestcontrour =  FindBigestContour(bin);
    vector > controus;
    controus.push_back(bigestcontrour);
    cv::drawContours(src,controus,0,Scalar(0,0,255),3);
    
    waitKey();
    return 0;
}

 

你可能感兴趣的:(Opencv,视觉算法)