c++ - OpenCV minimum upright bounding rect of a RotatedRect -
i'm trying determin minimum bouding rect of rotated rectangle. tried couple of samples this rotatedrect reference or this tutorial ellipses , bounding boxes. nothing satisfactory results. on image bellow, yellow rectangle desired result.
example data test:
image: width: 1500 height: 843 rotatedrect: center: x: 783.490417 y: 433.673492 size: width: 810.946899 height: 841.796997 angle: 95.4092407
sample code:
cv::rotatedrect r(cv::point2f(783.490417, 433.673492), cv::size2f(810.946899, 841.796997), 95.4092407); cv::mat img = mat::zeros(843, 1500, cv_8uc3); cv::rect rect = r.boundingrect(); cv::ellipse(img, r, cv::scalar(0, 0, 255)); // red point2f vertices[4]; r.points(vertices); (int = 0; < 4; i++) line(img, vertices[i], vertices[(i + 1) % 4], scalar(0, 255, 0)); // green rectangle(img, rect, scalar(255, 0, 0)); // blue cv::imshow("result", img);
- red - rotatedrect min bouding rect calculated
- blue - r.boundingrect()
- green - r.points()
- yellow - desired result
i think here can find function doing desired result.
#include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" using namespace cv; int main( int argc, char**) { rotatedrect r ; r.center = cv::point2f(783.490417, 433.673492); r.angle = 95.4092407; r.size = cv::size2f(810.946899, 841.796997); cv::mat img = mat::zeros(843, 1500, cv_8uc3); cv::rect rect = r.boundingrect(); cv::ellipse(img, r, cv::scalar(0, 0, 255)); // red point2f vertices[4]; r.points(vertices); (int = 0; < 4; i++) line(img, vertices[i], vertices[(i + 1) % 4], scalar(0, 255, 0)); // green rectangle(img, rect, scalar(255, 0, 0)); // blue float degree = r.angle*3.1415/180; float majoraxe = r.size.width/2; float minoraxe = r.size.height/2; float x = r.center.x; float y = r.center.y; float c_degree = cos(degree); float s_degree = sin(degree); float t1 = atan(-(majoraxe*s_degree)/(minoraxe*c_degree)); float c_t1 = cos(t1); float s_t1 = sin(t1); float w1 = majoraxe*c_t1*c_degree; float w2 = minoraxe*s_t1*s_degree; float maxx = x + w1-w2; float minx = x - w1+w2; t1 = atan((minoraxe*c_degree)/(majoraxe*s_degree)); c_t1 = cos(t1); s_t1 = sin(t1); w1 = minoraxe*s_t1*c_degree; w2 = majoraxe*c_t1*s_degree; float maxy = y + w1+w2; float miny = y - w1-w2; if (miny > maxy) { float temp = miny; miny = maxy; maxy = temp; } if (minx > maxx) { float temp = minx; minx = maxx; maxx = temp; } rect yellowrect(minx,miny,maxx-minx+1,maxy-miny+1); rectangle(img, yellowrect, scalar(0, 255, 255)); // yellow cv::imshow("result", img); waitkey(); }
Comments
Post a Comment