2012-10-26 27 views
7

में RotatedRect ROI मेरे पास RotatedRect है, मैं घुमावदार क्षेत्र में कुछ छवि प्रसंस्करण करना चाहता हूं (रंग हिस्टोग्राम निकालें)। मैं आरओआई कैसे प्राप्त कर सकता हूं? मेरा मतलब है कि क्षेत्र (पिक्सेल) प्राप्त करें ताकि मैं प्रसंस्करण कर सकूं।ओपनसीवी

मैं this लगता है, लेकिन यह getRotationMatrix2D और warpAffine का उपयोग करके क्षेत्र बदलता है, तो यह मेरी स्थिति के लिए काम नहीं करता है (मैं मूल छवि पिक्सेल संसाधित करने की जरूरत)।
फिर मुझे thisमास्क का उपयोग करने का सुझाव देता है, जो उचित लगता है, लेकिन क्या कोई मुझे मास्क को हरा RotatedRect के रूप में कैसे प्राप्त कर सकता है।
The green RotatedRect is the ROI I want to do some processing

मुखौटा को छोड़कर, क्या कोई अन्य समाधान है?
किसी भी संकेत

+1

http://stackoverflow.com/questions/9082204/opencv-draw-a-white- भरे बहुभुज) मदद? आपको अपने घुमावदार रेक्ट को आकर्षित करने के लिए केवल fillPoly का उपयोग करने की आवश्यकता है। आप अपने आकार और कोण का उपयोग करके 4 अंक प्राप्त कर सकते हैं। – Hammer

उत्तर

7

यहाँ मेरी हल है के लिए धन्यवाद, मुखौटा का उपयोग कर:
विचार मेरी RotatedRect ROI करने के लिए 255 सौंप कर Mat mask का निर्माण है।

कैसे पता चलेगा कि आरओआई में कौन सा बिंदु है (जिसे 255 को असाइन किया जाना चाहिए)?
समस्या का समाधान करने के लिए मैं निम्नलिखित फ़ंक्शन isInROI का उपयोग करता हूं।

/** decide whether point p is in the ROI. 
*** The ROI is a rotated rectange whose 4 corners are stored in roi[] 
**/ 
bool isInROI(Point p, Point2f roi[]) 
{ 
    double pro[4]; 
    for(int i=0; i<4; ++i) 
    { 
     pro[i] = computeProduct(p, roi[i], roi[(i+1)%4]); 
    } 
    if(pro[0]*pro[2]<0 && pro[1]*pro[3]<0) 
    { 
     return true; 
    } 
    return false; 
} 

/** function pro = kx-y+j, take two points a and b, 
*** compute the line argument k and j, then return the pro value 
*** so that can be used to determine whether the point p is on the left or right 
*** of the line ab 
**/ 
double computeProduct(Point p, Point2f a, Point2f b) 
{ 
    double k = (a.y-b.y)/(a.x-b.x); 
    double j = a.y - k*a.x; 
    return k*p.x - p.y + j; 
} 

कैसे मुखौटा निर्माण करने के लिए?
निम्नलिखित कोड का उपयोग कर।

Mat mask = Mat(image.size(), CV_8U, Scalar(0)); 
for(int i=0; i<image.rows; ++i) 
{ 
    for(int j=0; j<image.cols; ++j) 
    { 
     Point p = Point(j,i); // pay attention to the cordination 
     if(isInROI(p,vertices)) 
     { 
      mask.at<uchar>(i,j) = 255; 
     } 
    } 
} 

हो गया,
vancexu

+0

शिखर क्या हैं? मेरे मामले में, मेरे पास roi निर्देशांक हैं rect_points सरणी के रूप में Point2f। –

1

आप एक सुपरफास्ट समाधान की जरूरत है, मेरा सुझाव है:

  • फसल एक रेक्ट अपने RotatedRect rr enclosing।
  • घूमना + फसल की गई छवि का अनुवाद करें ताकि rotatedRect अब एक आय के बराबर हो। (रोटेशन के उत्पाद पर warpAffine का उपयोग करके और अनुवाद 3x3 matrices)
  • घुमावदार छवि (roi=Rect(Point(0,0), rr.size())) की रोई रखें।

लिखने के लिए थोड़ा समय लेने वाला है हालांकि आपको संयुक्त एफ़िन ट्रांसफॉर्म की गणना करने की आवश्यकता है।

1

आप गति के बारे में परवाह नहीं करते हैं और इस क्षेत्र के किसी भी आकार के लिए एक तेजी से प्रोटोटाइप बनाना चाहते हैं, तो आप एक openCV समारोह pointPolygonTest() है कि एक सकारात्मक मान देता है उपयोग कर सकते हैं अंदर बिंदु:

double pointPolygonTest(InputArray contour, Point2f pt, bool measureDist) 

सरल कोड:

vector<Point2f> contour(4); 
contour[0] = Point2f(-10, -10); 
contour[1] = Point2f(-10, 10); 
contour[2] = Point2f(10, 10); 
contour[3] = Point2f(10, -10);  
Point2f pt = Point2f(11, 11); 
an double res = pointPolygonTest(contour, pt, false); 
if (res>0) 
    cout<<"inside"<<endl; 
else 
    cout<<"outside"<<endl; 
3

मैं निम्नलिखित पोस्ट बहुत ही करने के लिए उपयोगी पाया। http://answers.opencv.org/question/497/extract-a-rotatedrect-area/

केवल चेतावनियां कि (क) "कोण" यहाँ (सीमांकन बॉक्स नहीं) पूरी छवि के केंद्र और (ख) नीचे अंतिम पंक्ति में (मुझे लगता है कि के बारे में रोटेशन माना जाता है कर रहे हैं) "आयत।केंद्र "(रोटेशन-मैट्रिक्स लगाने से) करता है [इस] (घुमाया छवि को बदल दिया जाना चाहिए।

// rect is the RotatedRect 
    RotatedRect rect; 
    // matrices we'll use 
    Mat M, rotated, cropped; 
    // get angle and size from the bounding box 
    float angle = rect.angle; 
    Size rect_size = rect.size; 
    // thanks to http://felix.abecassis.me/2011/10/opencv-rotation-deskewing/ 
    if (rect.angle < -45.) { 
     angle += 90.0; 
     swap(rect_size.width, rect_size.height); 
    } 
    // get the rotation matrix 
    M = getRotationMatrix2D(rect.center, angle, 1.0); 
    // perform the affine transformation 
    warpAffine(src, rotated, M, src.size(), INTER_CUBIC); 
    // crop the resulting image 
    getRectSubPix(rotated, rect_size, rect.center, cropped);