Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ignore part of a image during feature extraction in OpenCV?

I am trying to extract features from a rectangle image. But I want to ignore certain part of the image, so that no features are extracted from the these areas inside the image. I can think of two approaches.

a) Get features from the entire image and using keypoints discard the ones that lie within the ignored areas. But this approach is not preferred as I have put a limit on the maximum number of features to be extracted from the image and discarding later on won't help in getting constant number of features.

b) Make the pixels on that part of the image as numpy zero. But I think SIFT/ORB or other feature extraction algorithm might detect this as a black rectangle and create features wrongly at the edge part of this blackened box. Although not sure about this.

Can someone confirm the best approach for the problem statement?

like image 683
Debasish Mitra Avatar asked Jan 29 '23 12:01

Debasish Mitra


1 Answers

You've two ways of detecting features on a selected portion of an image.

Method 1:

By selecting an ROI. The downside of this method is that the calculated feature points will have the coordinates of the ROI. For example if you've an image with dimensions width = 400, height = 400, you select an ROI with dims x=0, y=0, width=100, height=100, the keypoints will have coordinates according to the ROI dims. You'll have to manually map them back to the original image size.

img = cv2.imread("image.jpg", -1)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# select and ROI with user specified dimensions
roi = gray[50:150,50:150]

sift = cv2.xfeatures2d.SIFT_create()

kp = sift.detect(roi)

img=cv2.drawKeypoints(roi,kp,img)

Original roi

Method 2:

Provide a mask to the sift feature detector method. Mask must be a single channel, unsigned char image.

img = cv2.imread("image.jpg", -1)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# create a mask image filled with zeros, the size of original image
mask = np.zeros(img.shape[:2], dtype=np.uint8)

# draw your selected ROI on the mask image
cv2.rectangle(mask, (50,50), (150,150), (255), thickness = -1)

sift = cv2.xfeatures2d.SIFT_create()

# provide mask to the detect method
kp = sift.detect(gray,mask)

img=cv2.drawKeypoints(gray,kp,img)

masked

like image 191
zindarod Avatar answered Feb 06 '23 16:02

zindarod