4

After searching in the net, I could not find a way to draw a bounding box like the one in this image using OpenCV in Python. It has two features first four corners which are not connected with each other and second one the transparent bounding box.

I know I should use Polygon, but I can not go further that this point.

Ishara Madhawa
  • 3,549
  • 5
  • 24
  • 42
Majid Azimi
  • 907
  • 1
  • 11
  • 29

3 Answers3

5

The following function draws an incomplete rectangle around the region of interest. I made use of cv2.line() twice for each point provided. Additionally I also made use of cv2.circle() to mark the 4 points.

There is one constraint. You will have to provide the 4 points in the following order: top-left, bottom-left, top-right, bottom-right.

Also there is an option to vary the length of the line you want to draw line_length.

Code:

def draw_border(img, point1, point2, point3, point4, line_length):

    x1, y1 = point1
    x2, y2 = point2
    x3, y3 = point3
    x4, y4 = point4    

    cv2.circle(img, (x1, y1), 3, (255, 0, 255), -1)    #-- top_left
    cv2.circle(img, (x2, y2), 3, (255, 0, 255), -1)    #-- bottom-left
    cv2.circle(img, (x3, y3), 3, (255, 0, 255), -1)    #-- top-right
    cv2.circle(img, (x4, y4), 3, (255, 0, 255), -1)    #-- bottom-right

    cv2.line(img, (x1, y1), (x1 , y1 + line_length), (0, 255, 0), 2)  #-- top-left
    cv2.line(img, (x1, y1), (x1 + line_length , y1), (0, 255, 0), 2)

    cv2.line(img, (x2, y2), (x2 , y2 - line_length), (0, 255, 0), 2)  #-- bottom-left
    cv2.line(img, (x2, y2), (x2 + line_length , y2), (0, 255, 0), 2)

    cv2.line(img, (x3, y3), (x3 - line_length, y3), (0, 255, 0), 2)  #-- top-right
    cv2.line(img, (x3, y3), (x3, y3 + line_length), (0, 255, 0), 2)

    cv2.line(img, (x4, y4), (x4 , y4 - line_length), (0, 255, 0), 2)  #-- bottom-right
    cv2.line(img, (x4, y4), (x4 - line_length , y4), (0, 255, 0), 2)

    return img

line_length = 15

img = np.zeros((512,512,3), np.uint8)
cv2.imshow('img', img)

point1, point2, point3, point4 = (280,330), (280,390), (340,330), (340,390)
fin_img = draw_border(img, point1, point2, point3, point4, line_length)

cv2.imshow('fin_img', fin_img)

cv2.waitKey()
cv2.destroyAllWindows() 

Result:

enter image description here

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • @MajidAzimi Cjeck out [THIS ANSWER](https://stackoverflow.com/questions/46036477/drawing-fancy-rectangle-around-face) for drawing rectangle with curved edges – Jeru Luke May 28 '18 at 06:07
-1

Try this code. Might be helpful for you.

Code:

import matplotlib.pyplot as plt
import matplotlib.patches as patches
from PIL import Image
import numpy as np

image = np.array(Image.open('Image.jpg'), dtype=np.uint8)

# Create figure and axes
fig,ax = plt.subplots(1)

# Display the image
ax.imshow(image)

# Create a Rectangle patch
rect = patches.Rectangle((102,55),160,162,linewidth=1,edgecolor='r',facecolor='none')

# Add the patch to the Axes
ax.add_patch(rect)

plt.show()

Image Output:

Output Would be like this

Muhammad Usman
  • 1,220
  • 13
  • 22
-1

Also if someone needs: Fancy rectangle with curved corners in C: However I needed this code in python was replied by the community.

void draw_border(IplImage* show_img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness, int r, int d):

        // Top left
        cvLine(show_img, cvPoint( pt1.x +r, pt1.y), cvPoint( pt1.x + r + d, pt1.y), color, thickness, 8, 0 );
        cvLine(show_img, cvPoint( pt1.x, pt1.y + r), cvPoint( pt1.x, pt1.y + r + d), color, thickness, 8, 0 );
        cvEllipse(show_img, cvPoint(pt1.x +r, pt1.y + r), cvSize( r, r ), 180, 0, 90, color, thickness, 8, 0);

        // Top right
            cvLine(show_img, cvPoint( pt2.x - r, pt1.y), cvPoint( pt2.x - r - d, pt1.y), color, thickness,  8, 0 );
            cvLine(show_img, cvPoint( pt2.x, pt1.y + r), cvPoint( pt2.x, pt1.y + r + d), color, thickness,  8, 0 );
        cvEllipse(show_img, cvPoint(pt2.x -r, pt1.y + r), cvSize( r, r ), 270, 0, 90, color, thickness, 8, 0);


            // Bottom left
            cvLine(show_img, cvPoint( pt1.x + r, pt2.y), cvPoint( pt1.x + r + d, pt2.y), color, thickness,  8, 0);
            cvLine(show_img, cvPoint( pt1.x, pt2.y - r), cvPoint( pt1.x, pt2.y - r - d), color, thickness,  8, 0);
        cvEllipse(show_img, cvPoint(pt1.x  + r, pt2.y - r), cvSize( r, r ), 90, 0, 90, color, thickness, 8, 0);

            // Bottom right
            cvLine(show_img, cvPoint( pt2.x - r, pt2.y), cvPoint( pt2.x - r - d, pt2.y), color, thickness, 8, 0);
            cvLine(show_img, cvPoint( pt2.x, pt2.y - r), cvPoint( pt2.x, pt2.y - r - d), color, thickness, 8, 0);
        cvEllipse(show_img, cvPoint(pt2.x  - r, pt2.y - r), cvSize( r, r ), 0, 0, 90, color, thickness, 8, 0);
Majid Azimi
  • 907
  • 1
  • 11
  • 29