简体   繁体   English

如何识别和填充我在javacv中塑造轮廓?

[英]How to identify and fill I shape Contours in javacv?

I'm developing project on javacv and I need to know how to identify following image and fill that Image using particular color ? 我正在开发关于javacv的项目,我需要知道如何识别以下图像并使用特定颜色填充图像?

I try to go through this question and This is the image that I use 我尝试通过这个问题 ,这是我使用的图像

在此输入图像描述

I try to go through this code and I developed a code in javacv 我尝试通过这段代码,并在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 box 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, 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, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
        }
        IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT,    null);
        cvDilate(gry, gry, mat, CV_C);
        cvErode(gry, gry, mat, CV_C);
        cnvs.showImage(gry);

    }
}

My final result should be like this image 我的最终结果应该像这张图片

在此输入图像描述

Put above code resulted this kind of image. 上面的代码产生了这种形象。 Please can some one help me to resolve this issue ? 有人可以帮我解决这个问题吗?

在此输入图像描述

The code does exactly what is supposed to do: 代码确实完成了应该做的事情:

// as stated [in the answer to your previous question][1], findContours leaves gry set to 0, or black
cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class),   
         CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 

    System.out.println(squares.total()); // nothing interesting

    for (int i = 0; i < squares.total(); i++)
    {
        // draw a gray contour (127) on a black image
        cvDrawContours(gry, squares, CvScalar.ONE, CvScalar.ONE, 127, 1, 8);
    }

To correct it, you have to set gry to 255 (white, before any call to drawContours, and draw black contours! that is 0, not 127.) 要纠正它,你必须将gry设置为255(白色,在任何调用drawContours之前,并绘制黑色轮廓!即0,而不是127.)

You just have to find contours using external retrieval mode: 您只需使用外部检索模式找到轮廓:

CV_RETR_EXTERNAL retrives only the extreme outer contours

In your situation this is the best mode because you have one external contour and this contour is what you're looking for. 在您的情况下,这是最佳模式,因为您有一个外部轮廓,这个轮廓是您正在寻找的。 Here's documantation. 这是文档。

After this just draw it using drawContours with CV_FILLED as thickness parameter to fill external polygon. 在此之后,使用带有CV_FILLED作为厚度参数的drawContours绘制它以填充外部多边形。

You can archive that using this code 您可以使用此代码对其进行归档

    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, sizeof(CvContour.class), sizeof(CvSeq.class), storage);
            JFileChooser f=new JFileChooser();
            int result=f.showOpenDialog(f);//show dialog box 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, gry, 230, 255, CV_THRESH_BINARY_INV);
            cvFindContours(gry, storage, squares, Loader.sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
            CvSeq ss=null;
            for (int i=0; i<1; i++)
            {
                cvDrawContours(gry, squares, CvScalar.WHITE, CV_RGB(248, 18, 18), 1, -1, 8);
                ss=cvApproxPoly(squares, sizeof(CvContour.class), storage, CV_POLY_APPROX_DP, 8, 0);
            }
            IplConvKernel mat=cvCreateStructuringElementEx(7, 7, 3, 3, CV_SHAPE_RECT, null);
            cvDilate(gry, gry, mat, CV_C);
            cvErode(gry, gry, mat, CV_C);
            cnvs.showImage(gry);

        }
    }

Result 结果

在此输入图像描述

This will be resulted out put and I believe this might help you to solve your problem. 这将是结果,我相信这可以帮助您解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM