[SOLVED] How to get the pixel coordinates of the region of interest using open-cv?

Issue

I’m trying to get the pixel coordinates of a specific roi in a image. I created the roi using mask. The code and the result is shown below.

import cv2
import numpy as np

img = cv2.imread("Inxee.jpg")
img = cv2.resize(img, (640, 480))
mask = np.zeros(img.shape, np.uint8)

points  = np.array([[273,167], [363, 167], [573, 353], [63, 353]])  ##taking random points for ROI.
cv2.fillPoly(mask, [points], (100, 0, 100))

img = cv2.addWeighted(img, 0.7, mask, 0.5, 0)

values = img[np.where((mask == (100, 0, 100)).all(axis=1))]
print(values)

##cv2.imshow("mask", mask)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

result image

so in the image we can see the ROI.

I tried to use the

values = img[np.where((mask == (100, 0, 100)).all(axis=1))]

but here I’m getting only values not coordinates.
So is there any way to get those coordinates?

Solution

Thanks for the solutions and possibilities friends,
I just did,

val = np.where(mask < 0)
coordinate = list(zip(val[0], val[1]))
print(coordinate)

with this i got the coordinates!
Thanks!

Answered By – Shibaditya99

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *