OpenCV寻找复杂背景下物体的轮廓

点击上方“小白学视觉”,选择加"星标"或“置顶

重磅干货,第一时间送达

本文转自:opencv学堂

问题提出

这是一个来自OPenCV问答社区 - "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;
}

下载1:OpenCV-Contrib扩展模块中文版教程

在「小白学视觉」公众号后台回复:扩展模块中文教程即可下载全网第一份OpenCV扩展模块教程中文版,涵盖扩展模块安装、SFM算法、立体视觉、目标跟踪、生物视觉、超分辨率处理等二十多章内容。

下载2:Python视觉实战项目52讲

在「小白学视觉」公众号后台回复:Python视觉实战项目即可下载包括图像分割、口罩检测、车道线检测、车辆计数、添加眼线、车牌识别、字符识别、情绪检测、文本内容提取、面部识别等31个视觉实战项目,助力快速学校计算机视觉。

下载3:OpenCV实战项目20讲

在「小白学视觉」公众号后台回复:OpenCV实战项目20讲即可下载含有20个基于OpenCV实现20个实战项目,实现OpenCV学习进阶。

交流群

欢迎加入公众号读者群一起和同行交流,目前有SLAM、三维视觉、传感器、自动驾驶、计算摄影、检测、分割、识别、医学影像、GAN、算法竞赛等微信群(以后会逐渐细分),请扫描下面微信号加群,备注:”昵称+学校/公司+研究方向“,例如:”张三 + 上海交大 + 视觉SLAM“。请按照格式备注,否则不予通过。添加成功后会根据研究方向邀请进入相关微信群。请勿在群内发送广告,否则会请出群,谢谢理解~

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值