【问题标题】:Error when applying OpenCV SURF to HSV color space in python在 python 中将 OpenCV SURF 应用于 HSV 颜色空间时出错
【发布时间】:2018-06-11 23:13:23
【问题描述】:

我正在对 HSV 图像实施 SURF,但它不起作用。 当我对 RGB 图像做同样的事情时,它工作正常。

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 )

我得到的错误是 -

C:\projects\opencv-python\opencv_contrib\modules\xfeatures2d\src\surf.cpp:892: 错误:(-215) !_img.empty() && ((imgtype) & ((1

请告诉我如何解决这个问题 ?

【问题讨论】:

    标签: opencv image-processing surf


    【解决方案1】:

    detectAndCompute 需要一个 CV_8U 映像。 (imgtype) & ((1 << 3) - 1)) == 0 有点神秘,但是如果您按照错误的来源找到源代码,您会看到:https://github.com/opencv/opencv_contrib/blob/2231018c839d728811a39556ec83741bf9a27614/modules/xfeatures2d/src/surf.cpp#L892

    HSV 转换 matplotlib.colors.rgb_to_hsv 正在返回浮动图像。

    要么将浮动图像转换回未签名的图像。或者直接使用OpenCV颜色转换,默认返回8U图片:

    hsv_image_array = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
    

    【讨论】:

    • 哦...很棒的收获。感谢您的宝贵时间。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 2013-01-02
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多