2014-02-11 54 views
0

我試圖濾除圖像中的輪廓。我的代碼沒有語法錯誤,但是當我運行我的程序時,我收到了很多紅色文本。我找到了失敗點cvInRangeS。JavaCV cvInRangeS錯誤

cvInRangeS(imghsv,minc,maxc,imgbin); 

你可以看到這個用的println statments只使它爲「thourgh3」

square.jpg是在項目目錄中,以便不應該成爲問題,如果沒有什麼幫助。

控制檯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 

完整的代碼如下

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 

快速響應,將不勝感激,因爲這個代碼是我的機器人團隊和競爭正在迅速接近!

謝謝!!! :D

回答

1

cvInRangeS()假定輸入圖像的類型爲CV_8U,因此您必須先將其轉換。

... 
cvtColor(imghsv, grayscale, CV_BGR2GRAY); 
cvInRangeS(grayscale,minc,maxc,imgbin); 
... 
0

感謝您的幫助。問題是在這一行,我把它設置爲3通道圖像「3」' imgbin = cvCreateImage(cvGetSize(img1),8,3);

它應該是一個二進制圖像。 imgbin = cvCreateImage(cvGetSize(img1),8,1);

+0

感謝您的幫助。你的評論讓我發現了! –