DEV Community

Cover image for Region of interest with Python
petercour
petercour

Posted on

Region of interest with Python

Often when doing computer vision tasks, it's not the entire image or photograph you are interested in.

A photo of scene may be contain many different objects that need to be classified each. so how do you achieve that?

You can use region-of-interest. A region of the image that is important.

Lets say you want to recognize faces, given a photo a person, you don't need the hat or the clothes, you need a close up of the face.

In Python you can easily do that. If you load an image like this

#!/usr/bin/python3
import cv2
import numpy as np


img = cv2.imread("lena.png", cv2.IMREAD_UNCHANGED)
cv2.imshow("Demo", img)
Enter fullscreen mode Exit fullscreen mode

Then you can create a region of interest like this

face = np.ones((200, 150, 3))
face = img[200:400, 200:350]
cv2.imshow("face", face)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

So the complte code

#!/usr/bin/python3
import cv2
import numpy as np

img = cv2.imread("lena.png", cv2.IMREAD_UNCHANGED)
face = np.ones((200, 150, 3))
cv2.imshow("Demo", img)
face = img[200:400, 200:350]
cv2.imshow("face", face)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Related links:

Top comments (0)