0

I am currently working on a small project, but I have an unresolved problem. That is I want to draw a shape through the desired objects , The first thing is to determine the coordinates of the starting and ending points but I don't have a specific idea yet but I don't know how to do it,I hope you can give me suggestions, Glad to have your help.

enter image description here

i want the result in like this

enter image description here

Mirote
  • 15
  • 4
  • Possible duplicate: https://stackoverflow.com/questions/23720875/how-to-draw-a-rectangle-around-a-region-of-interest-in-python – xprilion Feb 09 '23 at 05:34
  • See cv2.rectangle() for drawing a rectangle. Please read the information guides in the **help center** (https://stackoverflow.com/help), in particular, "How to Ask A Good Question" (https://stackoverflow.com/help/how-to-ask) and "How to create a Minimal, Reproducible Example" (https://stackoverflow.com/help/minimal-reproducible-example). – fmw42 Feb 09 '23 at 05:44

1 Answers1

0

Here is an example using opencv you can draw rectangle over an object: By giving the template image of the source image you can draw the shape over the image

# importing needed libraries
import cv2
import numpy as np

img_rgb = cv2.imread(source image)     # opening the source image
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)   # converting to the gray scale
template = cv2.imread(template image,0)   # opening the template image
w, h = template.shape[::-1]     # giving the sape of template image

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)   # matching both the image using the opencv methods for matching object
threshold = 0.9
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 1)
cv2.imshow('screen',img_rgb)
cv2.waitKey(0)

Source Image Source Image

Template Image

Template image

Result Image Result image

Manvi
  • 171
  • 11
  • the way you suggested is to use a template but in my case it really doesn't work because the input image can change continuously – Mirote Feb 09 '23 at 06:47