I'm trying to filter out the contours in an image. My code has no syntax errors but I just get a lot of red text when I run my program. I have located the point of failure to be cvInRangeS.
cvInRangeS(imghsv,minc,maxc,imgbin);
You can see this with the println statments only making it to "thourgh3"
square.jpg is in the project directory so that shouldn't be the problem if that helps.
The console returnes
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
The full code is as follows
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
cvInRangeS()
assumes the type of the input image to be CV_8U
, so you have to convert it first.
...
cvtColor(imghsv, grayscale, CV_BGR2GRAY );
cvInRangeS(grayscale,minc,maxc,imgbin);
...
Thanks for your help. the problem was in this line I have it set to a 3 channel image "3"' imgbin = cvCreateImage(cvGetSize(img1),8,3);
It should be a binary image. imgbin = cvCreateImage(cvGetSize(img1),8,1);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.