Ask Your Question
4

Source code to Closing Holes Leaf Image

asked 2012-11-12 02:35:32 -0600

Jenang gravatar image

updated 2020-10-24 13:55:28 -0600

Please help me. First, this is original/source picture. (Picture 1)

image description

with canny and morphology, I processing picture from that original picture like this (Picture 2) :

image description

And now, I want to process the image so that it looks like this (Picture 3) :

image description

But I don't know how to make them into images like that (Picture 3). Plese help me. Can you give me the source code to make it like Picture 3 ?

This is my source code (Convert Picture 1 to Picture 2) :

//Area_Daun.cpp : main project file.

#include "stdafx.h"
#include "cxcore.h"
#include "highgui.h"
#include "cv.h"
#include "stdio.h"

using namespace System;

int main()
{
    IplImage* img = NULL;
    IplImage* OpenImg = NULL;

    // load original image
    img = cvLoadImage("kubis2.jpg");

    IplImage* newImg = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U,1);
    IplImage* newImg1 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U,1);
    IplImage* newImg2 = cvCreateImage (cvGetSize(img), IPL_DEPTH_8U,1);
    CvMat *mat= cvCreateMat(newImg->height, newImg->width, CV_64FC1);
    CvMat *mat1= cvCreateMat(newImg->height, newImg->width, CV_64FC1);
    CvMat *mat2= cvCreateMat(newImg->height, newImg->width, CV_64FC1);

    cvCvtColor(img,newImg,CV_RGB2GRAY);
    cvThreshold(newImg, newImg, 200,255,CV_THRESH_BINARY_INV);
    cvConvert(newImg, mat);

    cvCanny(newImg,newImg1,1,1,3);
    cvConvert(newImg1, mat1);

    cvMorphologyEx( newImg, newImg2, 0, 0, CV_MOP_OPEN, 3);
    cvConvert(newImg2, mat2);

    float putih1 = 0;

    for(int x=0; x<newImg->height; x++){
        for(int y=0; y<newImg->width; y++)
        {
            if (cvmGet (mat,x,y)==255){
                putih1++;
            }
        }
    }

    printf("Luas Daerah Citra / Area Yang Rusak: %.0f\n",putih1);

    cvShowImage( "original", img );
    cvShowImage( "src", newImg );
    cvShowImage( "canny", newImg1 );
    //cvShowImage( "opening", newImg2 );

    cvWaitKey(0);
    return 0;
}
edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
8

answered 2012-11-12 03:45:09 -0600

Michael Burdinov gravatar image

updated 2012-11-12 03:48:00 -0600

What you actually need is:

Mat leafBinaryImage;
// Find binary image of leaf with holes in some way.
.....    

// Find outer contour of your leaf
vector<vector<Point> > contours;
findContours(leafBinaryImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

// clean the image
leafBinaryImage.setTo(Scalar(0));

// draw contour of leaf in image
polylines(leafBinaryImage, contours, true, Scalar(255), CV_FILLED);

See here details of polylines and findContours.

edit flag offensive delete link more

Comments

Please, i don't know what i should do in this part :

// Find binary image of leaf with holes in some way. .....

can't you help me with example in code for that part?

Jenang gravatar imageJenang ( 2012-11-12 10:24:57 -0600 )edit

What do you mean you don't know what should you do in this part? You posted your own code for finding binary image of the leaf.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-11-13 00:41:55 -0600 )edit

Oh, i'm sorry, i know it now.

Then, I apply the code from you in my code, but this error appears.

error C2664: 'cvFindContours': cannot convert parameter 2 from 'IplImage' to 'CvMemStorage'

error C2228 : left of '.setTo' must have class/struct/union

error C3861 : 'Scalar': identifier not found

error C3861 : 'polylines': identifier not found

error C3861 : 'Scalar': identifier not found

Sorry, i'm newbie, sometimes I don't quite understand about OpenCV. Still need to learn a lot. Hehe.

Jenang gravatar imageJenang ( 2012-11-13 07:38:27 -0600 )edit

What version of OpenCV are you using?

Michael Burdinov gravatar imageMichael Burdinov ( 2012-11-13 09:11:52 -0600 )edit

OpenCV-2.1.0-win32-vs2008

Jenang gravatar imageJenang ( 2012-11-13 22:14:03 -0600 )edit

This is the problem. The version you are using is quite outdated, so you would have to use old interface of those functions. Use cvFindContours instead of findContours, cvFillPoly instead of polylines, CvScalar instead of Scalar and so on.

I strongly recommend you to switch to recent version of OpenCV (2.4.3) if possible. This is not only issue of interface; versions 2.0 and 2.1 had very considerable amount of bugs that were fixed in later versions of OpenCV.

Michael Burdinov gravatar imageMichael Burdinov ( 2012-11-14 06:54:07 -0600 )edit
2

answered 2012-11-12 06:48:31 -0600

You have to use cvFindContour and set mode = CV_RETR_EXTERNAL. In this mode you have the external shape and fills all holes by cvDrawContour by level=1

edit flag offensive delete link more
-1

answered 2013-07-08 02:09:34 -0600

there's a really simple way to do this. as long as the back ground is always white, u can do this, u don't even need to use morphology for this

take a point near the edge of the image, a white pixel

do a floodfill giving new color with new color as black and low and high thresholds as cvScalarAll(0) in case, u can do it on all possible corners if u think the leaf might be till the edge

so now u are left with only the leaf with white holes still present now, use threshold with a very low threshold, say, low threshold=1 and high threshold=255 and set mode as binary making all non zero points white so u'll get ur required image

edit flag offensive delete link more

Question Tools

Stats

Asked: 2012-11-12 02:35:32 -0600

Seen: 3,934 times

Last updated: Jul 08 '13