简体   繁体   中英

how to draw a rectangle around the logo?

I have a litte problem. source image There is a logo which i must to detect and make rectangle around it.

I wrote some code:

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('9.png')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(img, (3, 3), 0)
_, binary = cv2.threshold(blur, 100, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C)

plt.imshow(binary, cmap='gray')
plt.show()

And got this result: result And now i dont know how to draw a rectangle around it

here is how:

img = cv2.imread('img.png')

temp = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, temp = cv2.threshold(temp, thresh=0, maxval=255, type=cv2.THRESH_BINARY + cv2.THRESH_OTSU)

kernel = np.ones((5, 5), np.uint8)
temp = cv2.dilate(temp, kernel, iterations=1)

contours, hierarchy = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
x, y, w, h = cv2.boundingRect(contours[0])

# draw a rectangle around the image
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM