OpenCV的为Android:错误,当我转换彩色图像为灰度 [英] OpenCV for Android: Error when i convert color image to grayscale

查看:510
本文介绍了OpenCV的为Android:错误,当我转换彩色图像为灰度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换彩色图像为灰度。对于我使用的OpenCV cvtColor()方法,但它给予以下错误:

OpenCV的错误:断言失败(SCN == 1安培;及(DCN == || 3 DCN == 4))的无效简历:: cvtColor(CV :: InputArray,CV: :OutputArray,INT,INT),文件/home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp,线路3789

这是我的code:

  IMG = Highgui.imread(映像路径);    / *垫转换=新垫(img.size(),CvType.CV_8UC3);
转换=新垫(img.size(),CvType.CV_8UC3,新的标量(255,
        255,255)); * /
    Imgproc.cvtColor(IMG,IMG,Imgproc.COLOR_GRAY2RGBA,0);    Imgproc.threshold(finalresult,转换254,255,1 / * * THRESH_BINARY_INV /);    bitmap1 = Bitmap.createBitmap(finalresult.cols(),finalresult.rows(),Bitmap.Config.ARGB_8888);    的System.out.println(+ finalresult);    Utils.matToBitmap(finalresult,bitmap1);    帆布=新的Canvas(bitmap1);    iv.setImageBitmap(bitmap1);
    }赶上(例外五)
    {
        的System.out.println(e.toString());
    }


解决方案

您使用了错误的标志。更改Imgproc.cvtColor从Imgproc.COLOR_GRAY2RGBA到Imgproc.COLOR_BGRA2GRAY第三个参数。

I want to convert color image to grayscale. For that I am using cvtColor() method of openCV but its giving following error:

OpenCV Error: Assertion failed (scn == 1 && (dcn == 3 || dcn == 4)) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/color.cpp, line 3789

This is my code :

img= Highgui.imread(pathToImage);

    /*Mat convert= new Mat(img.size(), CvType.CV_8UC3);
converted=new Mat(img.size(), CvType.CV_8UC3, new Scalar(255,
        255, 255));*/
    Imgproc.cvtColor(img,img,Imgproc.COLOR_GRAY2RGBA,0); 



    Imgproc.threshold(finalresult, converted, 254, 255, 1 /* THRESH_BINARY_INV */);     

    bitmap1=Bitmap.createBitmap(finalresult.cols(), finalresult.rows(), Bitmap.Config.ARGB_8888);

    System.out.println(""+finalresult);

    Utils.matToBitmap(finalresult, bitmap1);

    canvas = new Canvas(bitmap1);

    iv.setImageBitmap(bitmap1);
    }catch(Exception e)
    {
        System.out.println(e.toString());
    }

解决方案

You are using the wrong flag. Change the third parameter in Imgproc.cvtColor from Imgproc.COLOR_GRAY2RGBA to Imgproc.COLOR_BGRA2GRAY.

这篇关于OpenCV的为Android:错误,当我转换彩色图像为灰度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆