How to draw a rectangle on a specific object using OpenCV and Python
I want to wrap the rectangle in the first position (red 1), but when I use cv2.rectangle()
to draw, I get 2 rectangles. When I adjusted the contrast (I just want to mark the center of the 1st rectangle) but I can't get rid of the yellow area (many more have been marked) like the image below:
Here is an original image when I run my code:
here is what I want to get
contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cntr in contours:
x, y, w, h = cv2.boundingRect(cntr)
rect_img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
# ----------put text on img
cX = int(x + (w / 2))
cY = int(y + (h / 2))
img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(img, "+", (cX, cY), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
cv2.putText(rect_img, ("X = " + str(cX)), (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.putText(rect_img, ("Y = " + str(cY)), (5, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1)
cv2.imshow("img2", img)
from Recent Questions - Stack Overflow https://ift.tt/30KlCtB
https://ift.tt/3H4BxT2
Comments
Post a Comment