I'm doing a task in which i need to convert RGB image into an HSV image. But I only assign valid values to hue and saturation if the corresponding intensity is above a minimum value. Similarly I only assigned a valid value if the corresponding saturation is above another minimum value. non valid values will be 0. I wrote the following code for it. I don't understand how np.where works that well. Can you tell me if this code does what i need it to do:
sat=hsv[:,:,1] #saturation
iny=hsv[:,:,2] #intensty value
mask = np.zeros_like(hsv)
mask[:,:,2]=iny #all intensity values should be retained
selected_pixels = np.where(np.logical_and(np.greater_equal(iny,15),np.greater_equal(sat,15)))
mask[selected_pixels] = hsv[selected_pixels]
Secondly I also want to threshold the image using its histogram. The idea is to retain every pixel in the HSV image that has a hue and intensity value lower than a certain number in the histogram. To elaborate, if a pixel has 50 hue value and 50 intensity value. I'll check the histogram for both hue and intensity. If the bin value at 50 for both histogram is lower than a certain threshold, I'll retain that pixel. The exact thing I'm trying to follow is :
all pixels of the filtered input image are compared to the hue and the intensity histograms. A pixel is classified as an obstacle if either of the two following conditions is satisfied: i) The hue histogram bin value at the pixel’s hue value is below the hue threshold. ii) The intensity histogram bin value at the pixel’s intensity value is below the intensity threshold. If none of these conditions are true, then the pixel is classified as belonging to the ground.
Can anybody tell how can i do this without going into long FOR loops because I'll have to do this task on live video so it needs to be fast.
For the second task i tried using this:
hue=hsv[:,:,0] #hue value
iny=hsv[:,:,2] #intensity value
mask = np.zeros_like(frame)
hh=cv2.calcHist([hsv],[0],None,[256],[0,256])
ih=cv2.calcHist([hsv],[2],None,[256],[0,256])
selected_pixels = np.where(np.logical_and(np.less(hh[hue],5),np.less(ih[iny],400)))
mask[selected_pixels] = frame[selected_pixels] #frame is original image, HSV is the HSV format image, Mask is the thresholded image
But it shows something i don't expect. It retains the Blue portion of the original image and doesn't threshold the image like I intended