Ask Your Question
0

OpenCV / C++ - Filling holes

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

ChavaBloc gravatar image

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

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

Comments

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

berak gravatar imageberak ( 2018-10-02 03:30:26 -0600 )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 -0600 )edit

sure. thanks for the update, anyway !

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

1 answer

Sort by ยป oldest newest most voted
0

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

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;    
    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;
}
edit flag offensive delete link more

Comments

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 -0600 )edit

yes,i think

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

Question Tools

1 follower

Stats

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

Seen: 4,855 times

Last updated: Oct 08 '18