JavaCV cvInRangeS 错误
Posted
技术标签:
【中文标题】JavaCV cvInRangeS 错误【英文标题】:JavaCV cvInRangeS Error 【发布时间】:2014-03-08 15:47:54 【问题描述】:我正在尝试过滤掉图像中的轮廓。我的代码没有语法错误,但是当我运行我的程序时,我得到了很多红色文本。我已将故障点定位为 cvInRangeS。
cvInRangeS(imghsv,minc,maxc,imgbin);
您可以通过 println 语句看到这一点,仅使其成为“thourgh3”
square.jpg 位于项目目录中,因此如果有帮助,这应该不是问题。
控制台返回
Through 1
Through 2
Through 3
OpenCV Error: Assertion failed (src1.size == dst.size && dst.type() == CV_8U) in cvInRangeS, file ..\..\..\..\opencv\modules\core\src\arithm.cpp, line 2972
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\arithm.cpp:2972: error: (-215) src1.size == dst.size && dst.type() == CV_8U in function cvInRangeS
at com.googlecode.javacv.cpp.opencv_core.cvInRangeS(Native Method)
at opencv2.OpenCV2.main(OpenCV2.java:50)
Java Result: 1
完整代码如下
package opencv2;
/*There are import statments here but for the sake of space I have left them out :D*/
public class OpenCV2
public static void main(String[] args)
IplImage img1;
IplImage imghsv;
IplImage imgbin;
CvScalar minc = cvScalar(95,150,75,0), maxc = cvScalar(145,255,255,0);
CvSeq contour1 = new CvSeq(), contour2;
CvMemStorage storage = CvMemStorage.create();
double areaMax = 1000, areaC = 0;
System.out.println("Through 1");
img1 = cvLoadImage("square.jpg");
imghsv = cvCreateImage(cvGetSize(img1),8,3);
imgbin = cvCreateImage(cvGetSize(img1),8,3);
System.out.println("Through 2");
cvCvtColor(img1,imghsv,CV_BGR2HSV);
System.out.println("Through 3");
cvInRangeS(imghsv,minc,maxc,imgbin);
System.out.println("Through 4");
cvFindContours(imgbin,storage,contour1,Loader.sizeof(CvContour.class),
CV_RETR_LIST, CV_LINK_RUNS,cvPoint(0,0));
contour2 = contour1;
System.out.println("Through 5");
while(contour1 != null && !contour1.isNull())
areaC = cvContourArea(contour1,CV_WHOLE_SEQ,1);
if(areaC > areaMax)
areaMax = areaC;
contour1 = contour1.h_next();
//end of while
while(contour2 != null && !contour2.isNull())
areaC = cvContourArea(contour2,CV_WHOLE_SEQ,1);
System.out.println("Through 6");
if(areaC < areaMax)
cvDrawContours(imgbin,contour2,CV_RGB(0,0,0),CV_RGB(0,0,0),
0,CV_FILLED,8,cvPoint(0,0));
//end of if
System.out.println("Through 7");
contour2 = contour2.h_next();
//end of while2
System.out.println("Through 8");
cvShowImage("Color",img1);
cvShowImage("CF",img1);
cvWaitKey();
cvReleaseImage(img1);
cvReleaseImage(imghsv);
cvReleaseImage(imgbin);
cvReleaseMemStorage(storage);
//end of main
//end of class
【问题讨论】:
【参考方案1】:cvInRangeS()
假定输入图像的类型为CV_8U
,所以你必须先转换它。
...
cvtColor(imghsv, grayscale, CV_BGR2GRAY );
cvInRangeS(grayscale,minc,maxc,imgbin);
...
【讨论】:
【参考方案2】:感谢您的帮助。问题出在这一行中,我将其设置为 3 通道图像“3”' imgbin = cvCreateImage(cvGetSize(img1),8,3);
它应该是一个二值图像。 imgbin = cvCreateImage(cvGetSize(img1),8,1);
【讨论】:
感谢您的帮助。你的评论让我发现了!以上是关于JavaCV cvInRangeS 错误的主要内容,如果未能解决你的问题,请参考以下文章
JavaCV入门教程(JavaCV从入门到实战,JavaCV指南手册,免费JavaCV教程)
JavaCV入门教程目录(JavaCV从入门到实战,JavaCV指南手册,免费JavaCV教程)