我在使用Core.inRange函数时遇到断言错误,实际上是任何Core。功能。我在类似问题的答案中都遵循了所有解决方案。其他解决方案是检查通道数,检查图像是否为空并验证安装。我在Mac上使用Android Studio 2.2。测试的电话是中兴Speed KitKat和Moto g3 Marshmallow。
我的目标是从图像中获取红色和蓝色 - >确定红灯是打开还是打开蓝灯。代码从Vuforia Frame获取图像,将其转换为位图,然后尝试使用OpenCV来操作图像。在我们必须将Vuforia作为核心的一部分实施之前,这正在处理以前的代码。
这是代码的主要部分,Imgproc.cvtColor函数工作正常,它是最后一个Core.inRange
Mat mat1 = new Mat(640,480, CvType.CV_8UC4);
Mat mat2 = new Mat(640,480, CvType.CV_8UC4);
Mat mat3 = new Mat(640,480, CvType.CV_8UC4);
.......
Log.d("OPENCV","Height " + rgb.getHeight() + " Width " + rgb.getWidth());
Bitmap bm = Bitmap.createBitmap(rgb.getWidth(), rgb.getHeight(), Bitmap.Config.RGB_565);
bm.copyPixelsFromBuffer(rgb.getPixels());
//Mat tmp = OCVUtils.bitmapToMat(bm, CvType.CV_8UC4);
Mat tmp = new Mat(rgb.getWidth(), rgb.getHeight(), CvType.CV_8UC4);
Utils.bitmapToMat(bm, tmp);
SaveImage(tmp, "-raw");
fileLogger.writeEvent("process()","Saved original file ");
Log.d("OPENCV","CV_8UC4 Height " + tmp.height() + " Width " + tmp.width());
Log.d("OPENCV","Channels " + tmp.channels());
tmp.convertTo(mat1, CvType.CV_8UC4);
Size size = new Size(640,480);//the dst image size,e.g.100x100
resize(mat1,mat1,size);//resize image
SaveImage(mat1, "-convertcv_8uc4");
Log.d("OPENCV","CV_8UC4 Height " + mat1.height() + " Width " + mat1.width());
fileLogger.writeEvent("process()","converted to cv_8uc4");
Log.d("OPENCV","Channels " + mat1.channels());
Imgproc.cvtColor(mat1, mat2, Imgproc.COLOR_RGB2HSV_FULL);
SaveImage(mat2, "-COLOR_RGB2HSV_FULL");
Log.d("OPENCV","COLOR_RGB2HSV Height " + mat2.height() + " Width " + mat2.width());
Log.d("OPENCV","Channels " + mat2.channels());
//Core.inRange(mat2, RED_LOWER_BOUNDS_HSV, RED_UPPER_BOUNDS_HSV, mat3);
Log.d("OPENCV","mat2 Channels " + mat2.channels() + " empty " + mat2.empty());
Log.d("OPENCV","mat3 Channels " + mat3.channels() + " empty " + mat3.empty());
Core.inRange(mat2, new Scalar(0,100,150), new Scalar(22,255,255), mat3);
fileLogger.writeEvent("process()","Set Red window Limits: ");
SaveImage(mat3, "-red limits");
这是我在命令运行时得到的2个错误
E/cv::error(): OpenCV Error: Assertion failed (scn == 3 || scn == 4) in void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int), file /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/color.cpp, line 7349
E/org.opencv.imgproc: imgproc::cvtColor_10() caught cv::Exception: /home/maksim/workspace/android-pack/opencv/modules/imgproc/src/color.cpp:7349: error: (-215) scn == 3 || scn == 4 in function void cv::cvtColor(cv::InputArray, cv::OutputArray, int, int)
3张图像按预期保存在图片目录中。
我的日志记录产生以下内容
D/OPENCV: mat2 Channels 3 empty false
D/OPENCV: mat3 Channels 4 empty false
我尝试了两种不同的手机,尝试调低分辨率。我已经重新安装了OpenCV模块,以防它没有正确安装。我已经将图像全部设为3个频道,全部是4个频道。
答案 0 :(得分:0)
经过一周的调试后,这是最愚蠢的错误! 在SaveImage函数内 Imgproc.cvtColor(mat,mIntermediateMat,Imgproc.COLOR_RGBA2BGR,3); 这就是造成这个问题的原因。 在Core.inRange之后是SaveImage函数。 Core.inRange将通道删除为1 - fileLogger没有刷新最后一个日志,如果我使用了Log而不是我可能会更快地选择它。
public void SaveImage (Mat mat, String info) {
Mat mIntermediateMat = new Mat();
Imgproc.cvtColor(mat, mIntermediateMat, Imgproc.COLOR_RGBA2BGR, 3); <--Here bad
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String filename = "ian" + info + ".png";
File file = new File(path, filename);
Boolean bool = null;
filename = file.toString();
bool = Imgcodecs.imwrite(filename, mIntermediateMat);
if (bool == true)
Log.d("filesave", "SUCCESS writing image to external storage");
else
Log.d("filesave", "Fail writing image to external storage");
}