一、问题提出

这是一个来自"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<Point> FindBigestContour(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 main( void )
{
    Mat src = imread("e:/sandbox/question.png");
    Mat src_hsv;
    Mat bin;
    Mat src_h;
 
    cvtColor(src,src_hsv,COLOR_BGR2HSV);
    vector<Mat> 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<Point> bigestcontrour =  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、不断阅读思考实践的习惯;

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

寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)的更多相关文章

  1. Ubuntu下多个版本OpenCV管理(Multiple Opencv version)

    背景: 最近,在Nvidia的GPU嵌入式开发板Jetson TX1(简称TX1)上移植深度学习目标检测算法YOLO.在TX1上安装了官方提供的opencv版本——OpenCV4Tegra(OpenC ...

  2. CV-视频分析:静态背景下的运动检测

    ref : Chapter 2  Motion Detection in Static Backgrounds.   [ Github :…… ] -------------------------- ...

  3. vs2015+opencv3.3.1+ c++实现 静态背景下多运动目标提取,检测

    静止背景下运动物体的提取,跟踪出运动轨迹 下载地址 https://download.csdn.net/download/li_haoren/10761361 1.两遍扫描法得到第n帧的连通域,分离出 ...

  4. windows平台下基于QT和OpenCV搭建图像处理平台

        在之前的博客中,已经分别比较详细地阐述了"windows平台下基于VS和OpenCV"以及"Linux平台下基于QT和OpenCV"搭建图像处理框架,并 ...

  5. 中国将有可能在全球化的背景下收获新的人口红利:3星|《<财经>2019:预测与战略》

    <财经>2019 :预测与战略 <财经>杂志的年刊.内容是针对2019年的预测分析.我认为<财经>的调查报告比较有深度,分析则不是我爱看的类型. 总体评价3星,有参 ...

  6. Ubuntu 16.04下为Android编译OpenCV 3.2.0 Manager

    http://johnhany.net/2016/07/build-opencv-manager-for-android-on-ubuntu/ 最近想在Android上尝试一下SIFT和SURF匹配算 ...

  7. 解决a标签IE下点击后出现轮廓框

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. [转]真正了解CSS3背景下的@font face规则

    本文转自:http://www.zhangxinxu.com/wordpress/2017/03/css3-font-face-src-local/ by zhangxinxu from http:/ ...

  9. 对GraphQL-BFF:微服务背景下的前后端数据交互方案的研究-------引用

    随着多终端.多平台.多业务形态.多技术选型等各方面的发展,前后端的数据交互,日益复杂. 同一份数据,可能以多种不同的形态和结构,在多种场景下被消费. 在理想情况下,这些复杂性可以全部由后端承担.前端只 ...

随机推荐

  1. Amber learning note A8: Loop Dynamics of the HIV-1 Integrase Core Domain

    1. Prepare Input File $ tleap >source leaprc.protein.ff14SB ----- Source: /home/wangq/Programs/am ...

  2. bowtie2 Linux安装

    目前最新版本为2.3.2,网址为:https://sourceforge.net/projects/bowtie-bio/files/bowtie2/2.3.2 安装分为简单的下载可执行文件和源编译安 ...

  3. RF基础(一) RF内建函数库BuiltIn

    Robot framework做为一个测试框架,并不是只能做selenium测试,是支持扩展的, 比如说,你引用requests库就可以做接口测试, 那么无论你用什么库 首先要了解, RF本身提供的内 ...

  4. 44.JS--hover事件防重复效果

    遇到一种情况,就是运用hover事件的时候,会出现对象(主要是图片)闪动现象.主要是由于hover事件重复触发导致这一现象. html: <p class="smallImg" ...

  5. Javascript-do_while....

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  6. Bootstrap-按钮相关的class

    .btn          基础class.btn-default  白底黑字的按钮.btn-warning  红色按钮.btn-success  绿色按钮.btn-info     浅蓝色按钮.bt ...

  7. rabbitmq坑点与异常处理

    from:http://www.cnblogs.com/gossip/p/4573056.html 一.None of the specified endpoints were reachable 这 ...

  8. linux基本格式和常用目录命令一

    1: cd 绝对路径 2: cd 相对路径 名字开头 ./开头 ../开头 3: cd ~: 去到当前用户所在目录; 4: pwd获取当前的路径的绝对路径; 5: ls 与ls -l 6: cat 操 ...

  9. Unknown Treasure (卢卡斯 + 孙子定理, 模板题)

    Unknown Treasure 参考链接 : https://www.cnblogs.com/linyujun/p/5199684.html 卢卡斯定理 : C(n, m) % p  =  C(n ...

  10. 多线程:Operation(一)

    1. 进程和线程 1.1 进程 进程:正在运行的应用程序叫进程 进程之间都是独立的,运行在专用且受保护的内存空间中 两个进程之间无法通讯 通俗的理解,手机上同时开启了两个App.这两个App肯定是在不 ...