I am implementening SURF to HSV image, but It is not working. And when I am doing same to RGB image, It works fine.
from PIL import Image
import cv2
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
#loaading image
rgb_img_arr= np.array(Image.open("myImage.jpg"))
hsv_image_arr=matplotlib.colors.rgb_to_hsv(rgb_img_arr)
surf = cv2.xfeatures2d.SURF_create()
#it works fine
keypoints, descriptors = surf.detectAndCompute(rgb_img_arr, None)
rgb_img = cv2.drawKeypoints(rgb_img_arr, keypoints, None)
plt.imshow(rgb_img )
#But it doent work
keypoints, descriptors = surf.detectAndCompute(hsv_image_arr, None)
hsv_img = cv2.drawKeypoints(hsv_image_arr, keypoints, None)
plt.imshow(hsv_img )
Error I am getting is -
C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\surf.cpp:892: error: (-215) !_img.empty() && ((imgtype) & ((1 << 3) - 1)) == 0 && (imgcn == 1 || imgcn == 3 || imgcn == 4) in function cv::xfeatures2d::SURF_Impl::detectAndCompute
Please tell me how can i fix this ?
detectAndCompute
expects a CV_8U image. (imgtype) & ((1 << 3) - 1)) == 0
is a bit cryptic, but if you follow the error to the source you can see that there: https://github.com/opencv/opencv_contrib/blob/2231018c839d728811a39556ec83741bf9a27614/modules/xfeatures2d/src/surf.cpp#L892
The HSV conversion matplotlib.colors.rgb_to_hsv
is returning a float image instead.
Either you convert your float image back to an unsigned one. Or you can directly use the OpenCV color conversion, which will return an 8U image by default:
hsv_image_array = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)