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

简介: 一、问题提出这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-holes/)整编如下:title:OpenCV / C++ - Filling holescontent:H...
一、问题提出

这是一个来自"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: 

题,原始问题

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通道:

去光差:

阈值:

标注:

 
四、算法关键

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

//answerOpenCV OpenCV / C++ - Filling holes
#include "stdafx.h"
#include <iostream>
#include <vector>
 
 
using namespace cv;
using namespace std;
 
//find the biggest contour
vector<PointFindBigestContour(Mat src){    
    int imax = 0;  
    int imaxcontour = -1;  
    std::vector<std::vector<Point> >contours;    
    findContours(src,contours,CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
    for (int i=0;i<contours.size();i++){
        int itmp =  contourArea(contours[i]);
        if (imaxcontour < itmp ){
            imax = i;
            imaxcontour = itmp;
        }
    }
    return contours[imax];
}
 
//remove Light difference by using top hat
Mat moveLightDiff(Mat src,int radius){
    Mat dst;
    Mat srcclone = src.clone();
    Mat mask = Mat::zeros(radius*2,radius*2,CV_8U);
    circle(mask,Point(radius,radius),radius,Scalar(255),-1);
    //top hat
    erode(srcclone,srcclone,mask);
    dilate(srcclone,srcclone,mask);
    dst =  src - srcclone;
    return dst;
}
 
int mainvoid )
{
    Mat src = imread("e:/sandbox/question.png");
    Mat src_hsv;
    Mat bin;
    Mat src_h;
 
    cvtColor(src,src_hsv,COLOR_BGR2HSV);
    vector<Matrgb_planes;
    split(src_hsvrgb_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<Pointbigestcontrour =  FindBigestContour(bin);
    vector<vector<Point> > controus;
    controus.push_back(bigestcontrour);
    cv::drawContours(src,controus,0,Scalar(0,0,255),3);
    
    waitKey();
    return 0;

}

五、经验小结

解决这个问题我只用了10分钟的时间,写博客10分钟。能够快速解决问题并书写出来的关键为:

1、积累维护的代码库:GOCVHelper(https://github.com/jsxyhelu/GOCvHelper)

2、不断阅读思考实践的习惯;

感谢阅读至此,希望有所帮助!

目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
目录
相关文章
|
4月前
|
存储 算法 Linux
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
【实战项目】网络编程:在Linux环境下基于opencv和socket的人脸识别系统--C++实现
171 7
|
3月前
|
算法 开发工具 计算机视觉
【零代码研发】OpenCV实验大师工作流引擎C++ SDK演示
【零代码研发】OpenCV实验大师工作流引擎C++ SDK演示
58 1
|
3月前
|
存储 编解码 算法
【Qt&OpenCV 检测图像中的线/圆/轮廓 HoughLinesP/HoughCircles/findContours&drawContours】
【Qt&OpenCV 检测图像中的线/圆/轮廓 HoughLinesP/HoughCircles/findContours&drawContours】
54 0
|
3月前
|
计算机视觉
OpenCV轮廓分析
OpenCV轮廓分析
|
3月前
|
计算机视觉 C++
【见微知著】OpenCV中C++11 lambda方式急速像素遍历
【见微知著】OpenCV中C++11 lambda方式急速像素遍历
36 0
|
4月前
|
算法 Serverless 计算机视觉
【OpenCV】- 多边形将轮廓包围
【OpenCV】- 多边形将轮廓包围
|
4月前
|
存储 计算机视觉 流计算
【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
【OpenCV】计算视频的光流并跟踪物体calcOpticalFlowPyrLK
143 0
|
4月前
|
计算机视觉
OpenCV HSV物体跟踪
OpenCV HSV物体跟踪
32 0
|
4月前
|
计算机视觉
【OpenCV】-物体的凸包
【OpenCV】-物体的凸包
|
4月前
|
存储 计算机视觉 索引
【OpenCV】-查找并绘制轮廓
【OpenCV】-查找并绘制轮廓