Ask Your Question

OpenCV / C++ - Filling holes

asked 2018-10-02 02:58:30 -0500

ChavaBloc gravatar image

updated 2018-10-02 04:00:37 -0500

Hello there,

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

Object: image description

Shadow: image description

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.

edit retag flag offensive close merge delete


can you also show your code ? and maybe the original (untampered) images ?

berak gravatar imageberak ( 2018-10-02 03:30:26 -0500 )edit

I don't have the code right now (not at home), but I edited the post to add the original image.

ChavaBloc gravatar imageChavaBloc ( 2018-10-02 04:01:05 -0500 )edit

sure. thanks for the update, anyway !

berak gravatar imageberak ( 2018-10-02 04:03:26 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-08 20:58:07 -0500

here is my result

image description image description

here is my suggest code,may it help you .

//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;    
    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);
    //top hat
    dst =  src - srcclone;
    return dst;

int main( void )
    Mat src = imread("e:/sandbox/question.png");
    Mat src_hsv;
    Mat bin;
    Mat src_h;

    vector<Mat> rgb_planes;
    split(src_hsv, rgb_planes );
    src_h = rgb_planes[0]; // h channel is useful

    src_h = moveLightDiff(src_h,40);

    //find and draw the biggest contour
    vector<Point> bigestcontrour =  FindBigestContour(bin);
    vector<vector<Point> > controus;

    return 0;
edit flag offensive delete link more


Hello, thank you, I'm gonna try this when I'll be back home tonight! You think your functions will work for the shadows too?

ChavaBloc gravatar imageChavaBloc ( 2018-10-11 07:02:04 -0500 )edit

yes,i think

jsxyhelu gravatar imagejsxyhelu ( 2018-10-17 08:58:50 -0500 )edit

Question Tools

1 follower


Asked: 2018-10-02 02:58:08 -0500

Seen: 1,579 times

Last updated: Oct 08 '18