//el indx dice que zona es, indx = 1...4 Point2f getAreaCenter(RotatedRect& rect, int indx){ Point2f aux(0,0); if (abs(rect.angle)<1) { aux.x = ((indx == 1 || indx == 3) ? rect.center.x-rect.size.width/4 : rect.center.x+rect.size.width/4); aux.y = ((indx == 1 || indx == 2) ? rect.center.y-rect.size.height/4 : rect.center.y+rect.size.height/4); } else { CvPoint2D32f boxPoints[4]; RotatedRect box2(rect.center,cv::Size(rect.size.width/2,rect.size.height/2),-rect.angle+90); cvBoxPoints(CvBox2D(box2), boxPoints); if (indx == 1) { aux.x = (double)boxPoints[1].x; aux.y = (double)boxPoints[1].y; } else if(indx == 2){ aux.x = (double)boxPoints[2].x; aux.y = (double)boxPoints[2].y; } else if(indx == 3){ aux.x = (double)boxPoints[0].x; aux.y = (double)boxPoints[0].y; } else if(indx == 4){ aux.x = (double)boxPoints[3].x; aux.y = (double)boxPoints[3].y; } else CV_Assert(false); } return aux; }
void drawRotatedRect(Mat& img, const RotatedRect& rect, CvScalar color, int thickness){ if (abs(rect.angle)<1) { rectangle(img, Point2i(rect.center.x-rect.size.width/2,rect.center.y-rect.size.height/2), Point2i(rect.center.x+rect.size.width/2,rect.center.y+rect.size.height/2), color, thickness); } else { RotatedRect box(rect.center,rect.size,-rect.angle+90); CvPoint2D32f boxPoints[4]; cvBoxPoints(CvBox2D(box), boxPoints); line(img,Point2f((int)boxPoints[0].x, (int)boxPoints[0].y), Point2f((int)boxPoints[1].x, (int)boxPoints[1].y),color,thickness); line(img,Point2f((int)boxPoints[1].x, (int)boxPoints[1].y), Point2f((int)boxPoints[2].x, (int)boxPoints[2].y),color,thickness); line(img,Point2f((int)boxPoints[2].x, (int)boxPoints[2].y), Point2f((int)boxPoints[3].x, (int)boxPoints[3].y),color,thickness); line(img,Point2f((int)boxPoints[3].x, (int)boxPoints[3].y), Point2f((int)boxPoints[0].x, (int)boxPoints[0].y),color,thickness); } }
void draw() { double scale = this->scale == 0? 1.0 : this->scale; CvScalar colors[5] = { #if !defined CV_VERSION_EPOCH && (CV_VERSION_MAJOR >= 3) CvScalar(cvRound(color[0].r * 255), cvRound(color[0].g * 255), cvRound(color[0].b * 255), cvRound(alpha * 255)), CvScalar(cvRound(color[1].r * 255), cvRound(color[1].g * 255), cvRound(color[1].b * 255), cvRound(alpha * 255)), CvScalar(cvRound(color[2].r * 255), cvRound(color[2].g * 255), cvRound(color[2].b * 255), cvRound(alpha * 255)), CvScalar(cvRound(color[3].r * 255), cvRound(color[3].g * 255), cvRound(color[3].b * 255), cvRound(alpha * 255)), CvScalar(cvRound(color[4].r * 255), cvRound(color[4].g * 255), cvRound(color[4].b * 255), cvRound(alpha * 255)), #else {{cvRound(color[0].r * 255), cvRound(color[0].g * 255), cvRound(color[0].b * 255), cvRound(alpha * 255)}}, {{cvRound(color[1].r * 255), cvRound(color[1].g * 255), cvRound(color[1].b * 255), cvRound(alpha * 255)}}, {{cvRound(color[2].r * 255), cvRound(color[2].g * 255), cvRound(color[2].b * 255), cvRound(alpha * 255)}}, {{cvRound(color[3].r * 255), cvRound(color[3].g * 255), cvRound(color[3].b * 255), cvRound(alpha * 255)}}, {{cvRound(color[4].r * 255), cvRound(color[4].g * 255), cvRound(color[4].b * 255), cvRound(alpha * 255)}}, #endif }; for (int i = 0; i < (objects ? objects->total : 0); i++) { CvRect* r = (CvRect*) cvGetSeqElem(objects, i); CvPoint center; int thickness = stroke <= 0? CV_FILLED : cvRound(stroke * 100); int linetype = antialias? CV_AA : 8; center.x = cvRound((r->x + r->width * 0.5) / scale); center.y = cvRound((r->y + r->height * 0.5) / scale); switch (shape == 1.0? (rand() % 3) : cvRound(shape * 10)) { default: case 0: { int radius = cvRound((r->width + r->height) * 0.25 / scale); cvCircle(image, center, radius, colors[i % 5], thickness, linetype); break; } case 1: { #if !defined CV_VERSION_EPOCH && (CV_VERSION_MAJOR >= 3) CvBox2D box = CvBox2D(CvPoint2D32f(center.x, center.y), CvSize2D32f(r->width / scale, (r->height / scale) * 1.2), 90); #else CvBox2D box = {{center.x, center.y}, {r->width / scale, (r->height / scale) * 1.2}, 90}; #endif cvEllipseBox(image, box, colors[i % 5], thickness, linetype); break; } case 2: { #if !defined CV_VERSION_EPOCH && (CV_VERSION_MAJOR >= 3) CvPoint pt1 = CvPoint(r->x / scale, r->y / scale); CvPoint pt2 = CvPoint((r->x + r->width) / scale, (r->y + r->height) / scale); #else CvPoint pt1 = {r->x / scale, r->y / scale}; CvPoint pt2 = {(r->x + r->width) / scale, (r->y + r->height) / scale}; #endif cvRectangle(image, pt1, pt2, colors[i % 5], thickness, linetype); break; } } } }