OpenCV中文网站

 找回密码
 立即注册
搜索
热搜: 安装 配置
查看: 13803|回复: 5

关于ImageProc的findContours问题,请高手赐教!

[复制链接]
发表于 2013-4-28 10:44:50 | 显示全部楼层 |阅读模式
我在Android App里对一张图片中的目标求外部轮廓;
使用void org.opencv.imgproc.Imgproc.findContours(Mat image, List<MatOfPoint> contours, Mat hierarchy, int mode, int method)
单每次都出现如下问题:
04-28 09:59:48.857: E/cv::error()(1095): OpenCV Error: Unsupported format or combination of formats ([Start]FindContours support only 8uC1 and 32sC1 images) in _CvContourScanner* cvStartFindContours(void*, CvMemStorage*, int, int, int, CvPoint), file /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/contours.cpp, line 196
04-28 09:59:48.857: D/AndroidRuntime(1095): Shutting down VM
04-28 09:59:48.867: W/dalvikvm(1095): threadid=1: thread exiting with uncaught exception (group=0x409bd1f8)
04-28 09:59:48.867: E/AndroidRuntime(1095): FATAL EXCEPTION: main
04-28 09:59:48.867: E/AndroidRuntime(1095): CvException [org.opencv.core.CvException: /home/reports/ci/slave/50-SDK/opencv/modules/imgproc/src/contours.cpp:196: error: (-210) [Start]FindContours support only 8uC1 and 32sC1 images in function _CvContourScanner* cvStartFindContours(void*, CvMemStorage*, int, int, int, CvPoint)
04-28 09:59:48.867: E/AndroidRuntime(1095): ]
04-28 09:59:48.867: E/AndroidRuntime(1095):         at org.opencv.imgproc.Imgproc.findContours_1(Native Method)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at org.opencv.imgproc.Imgproc.findContours(Imgproc.java:5500)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at com.example.imagematch.MainActivity$1.onManagerConnected(MainActivity.java:106)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at org.opencv.android.AsyncServiceHelper$1.onServiceConnected(AsyncServiceHelper.java:318)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1068)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1085)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.os.Handler.handleCallback(Handler.java:605)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.os.Handler.dispatchMessage(Handler.java:92)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.os.Looper.loop(Looper.java:137)
04-28 09:59:48.867: E/AndroidRuntime(1095):         at android.app.ActivityThread.main(ActivityThread.java:4429)



根据提示,我把输入的Mat对象都转化成CvType.CV_32SC1类型还是不行:mat.convertTo(mat, CvType.CV_32SC1);


全部代码如下,求高手赐教,谢谢:
@Override
        public void onManagerConnected(int status) {
            switch (status) {
            case LoaderCallbackInterface.SUCCESS:{
                    Log.i(&quot;MainActivity&quot;, &quot;OpenCV loaded successfully&quot;);
                    
                    Mat mat = new Mat();
                    Utils.bitmapToMat(mTemplateImage, mat);

                    //二值化
                    Imgproc.GaussianBlur(mat, mat, new Size(9,9), 10);
                    Imgproc.threshold(mat, mat, 128, 200,Imgproc.THRESH_BINARY);
                    mat.convertTo(mat, CvType.CV_32SC1);

                    ArrayList<MatOfPoint> contoursList = new ArrayList<MatOfPoint>();
                    Mat hierarchy = new Mat();
                    hierarchy.convertTo(hierarchy, CvType.CV_32SC1);
                    //定义轮廓抽取模式
                    int mode = Imgproc.RETR_EXTERNAL;
                    //定义轮廓识别方法
                    int method = Imgproc.CHAIN_APPROX_NONE;
                    //轮廓识别
                    Imgproc.findContours(mat, contoursList, hierarchy, mode, method);
                    
                    if( contoursList != null && contoursList.size() > 0 ){
                            for( MatOfPoint matpoint : contoursList ){
                                    org.opencv.core.Point[] points = matpoint.toArray();
                                    for( org.opencv.core.Point point : points)
                                    Log.i(&quot;MainActivity&quot;, &quot;x=&quot;+point.x+&quot;,y=&quot;+point.y);
                            }
                    }
                    
                }
                break;
            default:{
                    super.onManagerConnected(status);
                } break;
            }
        }
回复

使用道具 举报

发表于 2015-4-21 10:16:27 | 显示全部楼层
Mat mat = new Mat();
Utils.bitmapToMat(img, mat);

Size dsize = new Size(mat.width() * scale, mat.height() * scale);
Mat mat2 = new Mat(dsize, Core.DEPTH_MASK_8U);
Mat mat3 = new Mat();
int threshold1 = 110;
int threshold2 = 110;
Imgproc.Canny(mat, mat3, threshold1, threshold2);
List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
Mat mat4 = new Mat();
Imgproc.cvtColor(mat, mat4, Imgproc.COLOR_RGB2GRAY, 1);
Imgproc.findContours(mat4, contours, new Mat(), Imgproc.RETR_TREE,
                                Imgproc.CHAIN_APPROX_NONE);

这样就可以了。
回复 支持 1 反对 0

使用道具 举报

发表于 2013-5-30 18:45:37 | 显示全部楼层

关于ImageProc的findContours问题,请高手赐教!

convertTo通过转换后,格式是没有发生变化的,你用canny方法可以转换成功,不这过个中边缘。。。。
回复 支持 反对

使用道具 举报

发表于 2013-11-11 04:58:50 | 显示全部楼层
我用的mat格式是32f,版本是2.4.6
回复 支持 反对

使用道具 举报

发表于 2015-4-17 16:54:15 | 显示全部楼层
hello  解决了没有呢
回复 支持 反对

使用道具 举报

发表于 2017-4-3 22:03:13 | 显示全部楼层
moonljt521 发表于 2015-4-21 10:16
**** 作者被禁止或删除 内容自动屏蔽 ****

其中的参数 new Mat(),那我要怎么才能将轮廓输出呢?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|OpenCV中文网站

GMT+8, 2024-3-28 22:50 , Processed in 0.011094 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表