DEV Community

Cover image for Region of interest with Python
petercour
petercour

Posted on

2 1

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:

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay