微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在开发关于 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,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);

    }
}

我的最终结果应该像这张图片

上面的代码产生了这种形象.有人可以帮我解决这个问题吗?

解决方法

您可以使用此代码对其进行归档
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 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),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);

        }
    }

结果

这将是结果,我相信这可以帮助您解决您的问题.

原文地址:https://www.jb51.cc/java/128370.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐