2013-11-01 54 views
0

我想檢測圖片的正方形,但在計算現有正方形的數量時遇到問題。圖中有6塊正方形,但有8塊被檢測到,其中3塊被檢測到兩次。檢測正方形opencv,但正方形檢測到比應該更多的圖像

這是圖像檢測 http://i698.photobucket.com/albums/vv347/holybring18/Screenshot_2013-11-01-07-37-17.png

的結果,這是預處理 http://i698.photobucket.com/albums/vv347/holybring18/Screenshot_2013-11-01-07-37-49.png

這是我的代碼,我從squares.cpp示例代碼了。我使用本地語言在我的Android中對此進行編碼,因此有關方塊檢測的所有內容都由我的本機代碼處理。但我修改了一點因爲在mixChannels()我有一個惱人的錯誤,我不知道如何解決它。

// jlong from java convert to references to Mat 
// for image source and image result 
Mat& matsrc = *(Mat*)alamatMatSrc; 
Mat& matres = *(Mat*)alamatMatRes; 

// needed matrix 
Mat gray; 
Mat blur; 
Mat bw; 
Mat dil; 
Mat er; 

// tempat menyimpan kontur dan estimasi sudut 
vector<vector<Point> > squares; 
vector<vector<Point> > contours; 
vector<Point> approx; 

// convert to grayscale 
cvtColor(matsrc, gray, CV_BGR2GRAY); 

// blur for reducing noise 
medianBlur(gray, blur, 9); 

// edge detection with Canny 
Canny(blur, bw, 0, 50); 

// dilate to ensure there is no cut off lines 
dilate(bw, dil, Mat(), Point(-1,-1)); 

// find all contours 
findContours(dil, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE); 

// loop to find the squares 
for (size_t i = 0; i < contours.size(); i++) { 

    // approximate contour with accuracy proportional 
    // to the contour perimeter 
    approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true); 

    // Note: absolute value of an area is used because 
    // area may be positive or negative - in accordance with the 
    // contour orientation 
    if (approx.size() == 4 && fabs(contourArea(Mat(approx))) > 1000 && isContourConvex(Mat(approx))) { 
     double maxCosine = 0; 

     for (int j = 2; j < 5; j++) { 
      Point pt1 = approx[j%4]; 
      Point pt2 = approx[j-2]; 
      Point pt3 = approx[j-1]; 
      double cosine = fabs(sudut(pt1, pt2, pt3)); 
      maxCosine = MAX(maxCosine, cosine); 
      } 
     if (maxCosine < 0.1) squares.push_back(approx); 
    } 
} 

,這是調用本地

// provide Mat for the source and for the result 
mymatsrc = new Mat(mybmp.getWidth(), mybmp.getHeight(), CvType.CV_8UC1); 
mymatres = new Mat(mybmp.getWidth(), mybmp.getHeight(), CvType.CV_8UC1); 

// convert bitmap to Mat, then pass the Mat adress to native, process, and convert it again to bitmap 
Utils.bitmapToMat(mybmp, mymatsrc); 
Utils.bitmapToMat(mybmp, mymatres); 
preProcess(mymatsrc.getNativeObjAddr(), mymatres.getNativeObjAddr()); 
Utils.matToBitmap(mymatres, mybmp); 

// show the processed picture 
imageView.setImageBitmap(mybmp); 

我的觀點時,我的Java代碼:

  1. 爲什麼我的代碼檢測方比它應該是什麼?
  2. 我該如何解決這個問題?
  3. 我發現有幾個人在mixChannel上遇到錯誤,我找不到解決方案,有誰知道如何解決這個問題,這是錯誤信息。

參數無效「考生:無效mixChannels(常量CV ::墊*, ,CV ::墊*,const int的*,???)無效mixChannels(常量 CV :: _ InputArray &,常量CV :: _ InputArray &,常量 的std ::矢量> &)空隙mixChannels(常量 的std ::矢量> &, 的std ::矢量> &,const int的*,?)'

+0

這是圖像來源http://opencv-code.com/wp-content/uploads/detect-simple-shapes-src-img.png – littledeveloper

+0

請包括您的'mixChannels()'調用,否則它是不可能的知道什麼是錯的 – Bull

+0

我很抱歉,但它與樣本,squares.cpp完全一樣,這是有同樣問題的人,仍然沒有人可以回答它http://answers.opencv。org/question/17568/opencv-mixchannels/ – littledeveloper

回答

0
  1. 由於medianBlur()的內核大小爲9像素。

  2. 減少內核大小爲3或5。

  3. 參見mixChannels() documentation

從squares.cpp

// find squares in every color plane of the image 
for(int c = 0; c < 3; c++) 
{ 
    int ch[] = {c, 0}; 
    mixChannels(&timg, 1, &gray0, 1, ch, 1); 

對DST gray0必須預先分配。在每次迭代中,由c指定的頻道從timg複製到gray0

相關問題