2013-09-24 73 views
8

Làm cách nào để vẽ hình chữ nhật có các góc tròn trong OpenCV? Tôi biết rằng các hàm ellipse() và line() có thể được đặt cùng nhau để vẽ nó. Tôi chỉ tự hỏi nếu có ai đó đã làm nó trước và đã đặt nó vào một chức năng thích hợp để tôi có thể sử dụng nó? Lý tưởng nhất là bán kính góc là hiệu chỉnh trong một tham số.Làm thế nào để vẽ một hình chữ nhật tròn (hình chữ nhật với các góc tròn) với OpenCV?

Tôi đã tìm kiếm rất nhiều về điều đó, nhưng có vẻ như không có ai gặp vấn đề đó trước đây. Nếu không có ai có chức năng như vậy tôi có thể sẽ đăng giải pháp của riêng tôi ở đây trong một vài ngày.

Trả lời

12

Tôi nhận ra, điều này dễ dàng hơn nhiều mà tôi nghĩ. Đây là chức năng của tôi. Tôi hy vọng nó là hữu ích cho một ai đó.

/** 
* Draws a rectangle with rounded corners, the parameters are the same as in the OpenCV function @see rectangle(); 
* @param cornerRadius A positive int value defining the radius of the round corners. 
* @author K 
*/ 
void rounded_rectangle(Mat& src, Point topLeft, Point bottomRight, const Scalar lineColor, const int thickness, const int lineType , const int cornerRadius) 
{ 
    /* corners: 
    * p1 - p2 
    * |  | 
    * p4 - p3 
    */ 
    Point p1 = topLeft; 
    Point p2 = Point (bottomRight.x, topLeft.y); 
    Point p3 = bottomRight; 
    Point p4 = Point (topLeft.x, bottomRight.y); 

    // draw straight lines 
    line(src, Point (p1.x+cornerRadius,p1.y), Point (p2.x-cornerRadius,p2.y), lineColor, thickness, lineType); 
    line(src, Point (p2.x,p2.y+cornerRadius), Point (p3.x,p3.y-cornerRadius), lineColor, thickness, lineType); 
    line(src, Point (p4.x+cornerRadius,p4.y), Point (p3.x-cornerRadius,p3.y), lineColor, thickness, lineType); 
    line(src, Point (p1.x,p1.y+cornerRadius), Point (p4.x,p4.y-cornerRadius), lineColor, thickness, lineType); 

    // draw arcs 
    ellipse(src, p1+Point(cornerRadius, cornerRadius), Size(cornerRadius, cornerRadius), 180.0, 0, 90, lineColor, thickness, lineType); 
    ellipse(src, p2+Point(-cornerRadius, cornerRadius), Size(cornerRadius, cornerRadius), 270.0, 0, 90, lineColor, thickness, lineType); 
    ellipse(src, p3+Point(-cornerRadius, -cornerRadius), Size(cornerRadius, cornerRadius), 0.0, 0, 90, lineColor, thickness, lineType); 
    ellipse(src, p4+Point(cornerRadius, -cornerRadius), Size(cornerRadius, cornerRadius), 90.0, 0, 90, lineColor, thickness, lineType); 
} 
1

Dưới đây là một việc thực hiện Python (trong trường hợp bất cứ ai đang tìm kiếm một): nó vẽ một góc được làm tròn (bán kính ngẫu nhiên và độ dày dòng --- thay đổi điều đó nếu bạn muốn) biên giới xung quanh một hình ảnh:

def addRoundedRectangleBorder(img): 
    height, width, channels = img.shape 

    border_radius = int(width * random.randint(1, 10)/100.0) 
    line_thickness = int(max(width, height) * random.randint(1, 3)/100.0) 
    edge_shift = int(line_thickness/2.0) 

    red = random.randint(230,255) 
    green = random.randint(230,255) 
    blue = random.randint(230,255) 
    color = (blue, green, red) 

    #draw lines 
    #top 
    cv2.line(img, (border_radius, edge_shift), 
    (width - border_radius, edge_shift), (blue, green, red), line_thickness) 
    #bottom 
    cv2.line(img, (border_radius, height-line_thickness), 
    (width - border_radius, height-line_thickness), (blue, green, red), line_thickness) 
    #left 
    cv2.line(img, (edge_shift, border_radius), 
    (edge_shift, height - border_radius), (blue, green, red), line_thickness) 
    #right 
    cv2.line(img, (width - line_thickness, border_radius), 
    (width - line_thickness, height - border_radius), (blue, green, red), line_thickness) 

    #corners 
    cv2.ellipse(img, (border_radius+ edge_shift, border_radius+edge_shift), 
    (border_radius, border_radius), 180, 0, 90, color, line_thickness) 
    cv2.ellipse(img, (width-(border_radius+line_thickness), border_radius), 
    (border_radius, border_radius), 270, 0, 90, color, line_thickness) 
    cv2.ellipse(img, (width-(border_radius+line_thickness), height-(border_radius + line_thickness)), 
    (border_radius, border_radius), 10, 0, 90, color, line_thickness) 
    cv2.ellipse(img, (border_radius+edge_shift, height-(border_radius + line_thickness)), 
    (border_radius, border_radius), 90, 0, 90, color, line_thickness) 

    return img 
Các vấn đề liên quan