using openCV2.4.3 for Detect Rectangle
- Library/opencv
- 2012. 11. 23.
using openCV2.4.3 for Detect Rectangle
(제목이 문법이 맞는가 모르겠네~ for 빼는게 나아보이는데 )
openCV version : 2.4.3 using Visual studio 2010
소스코드
001_DetectRectangle(opencv2.2).zip
002_DetectRectangle(opencv2.4.3).zip
003_DetectRectangle(opencv2.4.3)_newVersion.zip
#include
#include #include #include using namespace std; using namespace cv; // example : Creating Bounding rotated boxes and ellipses for contours // http://docs.opencv.org/doc/tutorials/imgproc/shapedescriptors/bounding_rotated_ellipses/bounding_rotated_ellipses.html Mat src; Mat src_gray; int thresh = 100; int max_thresh = 255; RNG rng(12345); /// Function header void thresh_callback(int, void* ); double angle( Point pt1, Point pt2, Point pt0 );; /** @function main */ int main( int argc, char** argv ) { /// Load source image and convert it to gray //src = imread( argv[1], 1 ); //Mat loadimage = imread( "rectangle.png" ); src = imread( "rectangle.png" ); /// Convert image to gray and blur it cvtColor( src, src_gray, CV_BGR2GRAY ); blur( src_gray, src_gray, Size(3,3) ); /// Create Window char* source_window = "Source"; namedWindow( source_window, CV_WINDOW_AUTOSIZE ); imshow( source_window, src ); createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback ); thresh_callback( 0, 0 ); waitKey(0); return(0); } /** @function thresh_callback */ void thresh_callback(int, void* ) { Mat threshold_output; vector > contours; vector hierarchy; /// Detect edges using Threshold threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY ); /// Find contours findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); /// Find the rotated rectangles and ellipses for each contour vector minRect( contours.size() ); vector minEllipse( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { // Test contours vector approx; 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++){ 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); minRect[i] = minAreaRect( Mat(contours[i]) ); } } } //minRect[i] = minAreaRect( Mat(contours[i]) ); if( contours[i].size() > 5 ) { //minEllipse[i] = fitEllipse( Mat(contours[i]) ); } } /// Draw contours + rotated rects + ellipses Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 ); for( int i = 0; i< contours.size(); i++ ) { Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) ); // contour //drawContours( drawing, contours, i, color, 1, 8, vector (), 0, Point() ); // ellipse ellipse( drawing, minEllipse[i], color, 2, 8 ); // rotated rectangle Point2f rect_points[4]; minRect[i].points( rect_points ); for( int j = 0; j < 4; j++ ) line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 ); } /// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", drawing ); } double angle( Point pt1, Point pt2, Point pt0 ) { double dx1 = pt1.x - pt0.x; double dy1 = pt1.y - pt0.y; double dx2 = pt2.x - pt0.x; double dy2 = pt2.y - pt0.y; return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); }
결과
01. 그림1
- 원본이미지
02. 그림2 - 기존에 코드를 사용하여 사각형을 검출
문제점 : Detection 사각형을 검출할때, 중복되어서 검출된 것을 모두 출력하게 된다.
그래서 groupRectangles() 함수를 사용하여서 해결을 하도록 하자. (다른방법을 써두된다.)
본 포스팅의 코드에서는 아래의 결과가 나오는 코드가 압축파일로 올려짐, 즉 코드를 사용하지 않음
03. 그림3
- groupRectangles() 를 사용하는것도 좋으나, 일단~ contours 를 이용하여서 모든 객체 검출
04. 그림4
- 처음에 사용한 사각형 검출을 이용하여 사각형일 경우에 vector list에 담도록 한다.
Think
- MQ : 생각해보니... groupRectangles() 함수 쓰려고 했는데안쓰게 되는건가.???
- MA : 안드로이드에 올려보고
[2] Extract a RotatedRect area
http://answers.opencv.org/question/497/extract-a-rotatedrect-area/
[3] GroupRectangle() Fuction Example
http://tech.groups.yahoo.com/group/OpenCV/message/69397http://tech.groups.yahoo.com/group/OpenCV/message/69397
'Library > opencv' 카테고리의 다른 글
opencv font (0) | 2012.11.23 |
---|---|
opencv camera frame rate (0) | 2012.11.23 |
groupRectangles (0) | 2012.11.23 |
Rectangle Detection (0) | 2012.11.21 |
IplImage or CvMat to Mat (0) | 2012.11.21 |