【问题标题】:How to identify and fill I shape Contours in javacv?如何在javacv中识别和填充我的形状轮廓?
【发布时间】:2012-07-10 17:14:46
【问题描述】:

我正在 javacv 上开发项目,我需要知道如何识别以下图像并使用特定颜色填充该图像?

我尝试浏览这个question,这是我使用的图像

我尝试浏览这段代码,并在 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);

    }
}

我的最终结果应该是这样的图片

上面的代码生成了这种图像。请问有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: java opencv javacv


    【解决方案1】:

    您可以使用此代码存档

        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);
    
            }
        }
    

    结果

    这将被输出,我相信这可能会帮助您解决问题。

    【讨论】:

      【解决方案2】:

      您只需要使用外部检索模式查找轮廓:

      CV_RETR_EXTERNAL retrives only the extreme outer contours
      

      在您的情况下,这是最好的模式,因为您有一个外部轮廓,而这个轮廓正是您要寻找的。 Here's 文档。

      在此之后,只需使用drawContoursCV_FILLED 作为厚度参数绘制它以填充外部多边形。

      【讨论】:

        【解决方案3】:

        代码完全按照预期执行:

        // 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);
            }
        

        要更正它,您必须将 gry 设置为 255(白色,在调用 drawContours 之前绘制黑色轮廓!即 0,而不是 127。)

        【讨论】:

        • 您能解释一下如何访问找到的 Contours 点值吗?
        猜你喜欢
        • 1970-01-01
        • 2021-03-17
        • 2014-04-05
        • 2012-07-12
        • 2021-08-21
        • 1970-01-01
        • 1970-01-01
        • 2012-02-03
        • 1970-01-01
        相关资源
        最近更新 更多