Filtering values of an array using pixel positions from an image

Question:

I have a image in greyscale. I have the value of each pixel saved to a text document that I pre-processed and loaded as an array, therefore my array has size 110529.

An example of how my array looks like:

import numpy as np
my_array = np.random.randint(low=18., high=36,size=(110592))

Then, I used OpenCV to draw a ROI around the face in my image like this:

x, y, w, h = cv2.selectROI(my_frame)

and the values of x, y, w, h are:

 95 2 184 286

What I want to do is use the pixel indices in the ROI from that image as reference and use those indices to extract to a new array the values that are inside my_array, so I can have a filtered array with 52624 values that corresponds to the ROI in the image

Asked By: xnok

||

Answers:

what you wanted is not called "filtering" but a "numpy slice":

x, y, w, h = cv2.selectROI(my_frame)
roi = my_frame[y:y+h, x:x+w]
Answered By: berak