2012-11-23 14 views
9

मैं ओपनसीवी में नया हूं इसलिए पिछले 3 से 4 दिनों तक संघर्ष कर रहा हूं, मैं पहले से ही पेपर शीट सीमा का पता लगाता हूं, अब मैं कोनों पर 4 मंडल खींचना चाहता हूं।कागजात के कोने को ढूंढें

मैं इस कोड से सीमारेखा खींचना

const cv::Point* p = &squares[i][0]; 

int n = (int)squares[i].size(); 

polylines(image, &p,&n, 1, true, Scalar(255,255,0), 5, CV_AA); 

मैं openCV में नया हूँ, तो मेरी राय में मैं ऊपरी बाएँ कोने अंक p-> एक्स और p-> y है, लेकिन यह कैसे मैं दूसरों कोनों मिल , मैं इस पॉलीलाइन विधि में पैरामीटर & एन में भी उलझन में हूं, यह पॉलिलाइन विधि पूरी आयताकार कैसे आकर्षित करती है?

जब मैं बाउंडिंग रेक्ट का उपयोग करता हूं, तो यह सही नहीं है कि यह पेपर शीट के पक्ष में थोड़ी सी जगह देता है।

किसी भी मदद वास्तव में सराहना की

कोड है:

- (cv::Mat)finshWork:(cv::Mat &)image 
{ 
// read in the apple (change path to the file) 
Mat img0 =image;// imread("/home/philipp/img/apple.jpg", 1); 

Mat img1; 
cvtColor(img0, img1, CV_RGB2GRAY); 

// apply your filter 
Canny(img1, img1, 100, 200); 

// find the contours 
vector< vector<cv::Point> > contours; 
findContours(img1, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE); 

/////for SQUARE CODE 
std::vector<std::vector<cv::Point> > squares; 
std::vector<cv::Point> approx; 
for(size_t i = 0; i < contours.size(); i++) 
{ 
    cv::approxPolyDP(cv::Mat(contours[i]), approx, arcLength(cv::Mat(contours[i]), true)*0.02, true); 
    if(approx.size() == 4 && fabs(contourArea(cv::Mat(approx))) > 1000 && cv::isContourConvex(cv::Mat(approx))) { 
     double maxCosine = 0; 

     for(int j = 2; j < 5; j++) 
     { 
      double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1])); 
      maxCosine = MAX(maxCosine, cosine); 
     } 

     if(maxCosine < 0.3) { 
      squares.push_back(approx); 
      cv::Point newPoint = approx[0]; 

      NSLog(@"x is %d and y is %d",newPoint.x,newPoint.y); 
     } 
    } 
} 

const cv::Point* p = &squares[0][0]; 


int n = (int)squares[0].size(); 

NSLog(@"%d",n); 


//THIS IS WORKING CODE    

    polylines(image, &p,&n, 1, true, Scalar(0,0,255), 10, CV_AA); 
    //polylines(image, &p,&n, 1, true, Scalar(255,255,0), 5, CV_AA); 
//////////// 
} 

धन्यवाद

+0

क्या आप स्पष्टीकरण दे सकते हैं कि आपको पेपर कैसे मिलता है? सबसे अच्छा आपको अधिक कोड जोड़ना चाहिए। – dom

+0

@moosgummi कृपया मेरे संपादन की जांच करें, मुझे लगता है कि मैं आपके प्रश्न से आपके कोडिंग के कुछ हिस्से का उपयोग करता हूं, उस हिस्से को साझा करने के लिए धन्यवाद। – QueueOverFlow

उत्तर

18

संदर्भ my original code, जो केवल एक छवि पर वर्गों का पता लगाता है के लिए ले लो।

इसका मतलब है कि मुख्य आवेदन की विधि में आप कॉल करने के लिए निम्नलिखित छद्म कोड की तरह कुछ लिखते थे find_squares():

Mat image = imread("test.jpg", 1); 

// Detect all regions in the image that are similar to a rectangle 
vector<vector<Point> > squares; 
find_squares(image, squares); 

// The largest of them probably represents the paper 
vector<Point> largest_square; 
find_largest_square(squares, largest_square); 

// Print the x,y coordinates of the square 
cout << "Point 1: " << largest_square[0] << endl; 
cout << "Point 2: " << largest_square[1] << endl; 
cout << "Point 3: " << largest_square[2] << endl; 
cout << "Point 4: " << largest_square[3] << endl; 

चाल find_largest_square() पर निर्भर करता है नीचे प्रस्तुत:

void find_largest_square(const vector<vector<Point> >& squares, vector<Point>& biggest_square) 
{ 
    if (!squares.size()) 
    { 
      // no squares detected 
      return; 
    } 

    int max_width = 0; 
    int max_height = 0; 
    int max_square_idx = 0; 
    const int n_points = 4; 

    for (size_t i = 0; i < squares.size(); i++) 
    { 
      // Convert a set of 4 unordered Points into a meaningful cv::Rect structure. 
      Rect rectangle = boundingRect(Mat(squares[i])); 

    //  cout << "find_largest_square: #" << i << " rectangle x:" << rectangle.x << " y:" << rectangle.y << " " << rectangle.width << "x" << rectangle.height << endl; 

      // Store the index position of the biggest square found 
      if ((rectangle.width >= max_width) && (rectangle.height >= max_height)) 
      { 
        max_width = rectangle.width; 
        max_height = rectangle.height; 
        max_square_idx = i; 
      } 
    } 

    biggest_square = squares[max_square_idx]; 
} 
+1

धन्यवाद, आप एक lifesaver =) –

+0

नमस्ते सफेद सफेद पर अगर मैं सफेद कागज का पता लगाने के लिए कैसे कर सकता हूं? ... मैंने भूरे रंग के पैमाने और कैनी रूपांतरण बदलने के साथ कोशिश की बीटी एम एनटी tht rectngle खोजने में सक्षम .... thnks:) – user1140237

+0

घुमावदार आयत के कोनों को कैसे ढूंढें (आयताकार जिनके पक्ष धुरी को समन्वयित करने के समानांतर नहीं हैं)? –

संबंधित मुद्दे