How do I identify and fill the contours I shape in javacv?

I'm developing a project on javacv. I need to know how to recognize the following image and fill it with specific colors?

I try to pass this question, which is the image I use

I tried this code and developed a code in javacv

import com.googlecode.javacpp.Loader;
import com.googlecode.javacv.CanvasFrame;
import static com.googlecode.javacpp.Loader.*;
import static com.googlecode.javacv.cpp.opencv_core.*;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;
import java.io.File;
import javax.swing.JFileChooser;

public class PolyGonIdentification {
    public static void main(String[] args) {
        CanvasFrame cnvs=new CanvasFrame("Polygon");
        cnvs.setDefaultCloSEOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

        CvMemStorage storage=CvMemStorage.create();
        CvSeq squares = new CvContour();
        squares = cvCreateSeq(0,sizeof(CvContour.class),sizeof(CvSeq.class),storage);
        JFileChooser f=new JFileChooser();
        int result=f.showOpenDialog(f);//show dialog @R_270_2419@ to choose files
        File myfile=null;
        String path="";
        if(result==0){
            myfile=f.getSelectedFile();//selected file taken to myfile
            path=myfile.getAbsolutePath();//get the path of the file
        }
        IplImage src = cvLoadImage(path);//hear path is actual path to image
        IplImage gry=cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);
        cvCvtColor(src,gry,CV_BGR2GRAY);
        cvThreshold(gry,230,255,CV_THRESH_BINARY_INV);
        cvFindContours(gry,storage,squares,Loader.sizeof(CvContour.class),CV_RETR_LIST,CV_CHAIN_APPROX_SIMPLE);
        System.out.println(squares.total());
        for (int i=0; i<squares.total(); i++)
        {
            cvDrawContours(gry,CvScalar.ONE,127,1,8);
        }
        IplConvKernel mat=cvCreateStructuringElementEx(7,7,3,CV_SHAPE_RECT,null);
        cvDilate(gry,mat,CV_C);
        cvErode(gry,CV_C);
        cnvs.showImage(gry);

    }
}

My final result should look like this picture

The code above produces this image Can someone help me solve this problem?

Solution

You can use this code to archive it

import com.googlecode.javacpp.Loader;
    import com.googlecode.javacv.CanvasFrame;
    import static com.googlecode.javacpp.Loader.*;
    import static com.googlecode.javacv.cpp.opencv_core.*;
    import static com.googlecode.javacv.cpp.opencv_highgui.*;
    import static com.googlecode.javacv.cpp.opencv_imgproc.*;
    import java.io.File;
    import javax.swing.JFileChooser;

    public class Ishape {
        public static void main(String[] args) {
            CanvasFrame cnvs=new CanvasFrame("Polygon");
            cnvs.setDefaultCloSEOperation(javax.swing.JFrame.EXIT_ON_CLOSE);

            CvMemStorage storage=CvMemStorage.create();
            CvSeq squares = new CvContour();
            squares = cvCreateSeq(0,storage);
            JFileChooser f=new JFileChooser();
            int result=f.showOpenDialog(f);//show dialog @R_270_2419@ to choose files
                File myfile=null;
                String path="";
            if(result==0){
                myfile=f.getSelectedFile();//selected file taken to myfile
                path=myfile.getAbsolutePath();//get the path of the file
            }
            IplImage src = cvLoadImage(path);//hear path is actual path to image
            IplImage gry=cvCreateImage(cvGetSize(src),1);
            cvCvtColor(src,CV_BGR2GRAY);
            cvThreshold(gry,CV_THRESH_BINARY_INV);
            cvFindContours(gry,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
            CvSeq ss=null;
            for (int i=0; i<1; i++)
            {
                cvDrawContours(gry,CvScalar.WHITE,CV_RGB(248,18,18),-1,8);
                ss=cvApproxPoly(squares,CV_POLY_APPROX_DP,8,0);
            }
            IplConvKernel mat=cvCreateStructuringElementEx(7,null);
            cvDilate(gry,CV_C);
            cvErode(gry,CV_C);
            cnvs.showImage(gry);

        }
    }

result

This will be the result, I believe it can help you solve your problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>