- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 cv2.inRange(python 2.7)根据颜色执行对象检测。使用 BGR 颜色时,一切似乎都工作正常。但是,当我将 BGR 颜色映射到 HSV 时,无法获得正确的蒙版。请参见下面的示例:
1) bgr 中的阈值
img_test = cv2.imread("test_img/mario.jpeg")
#define color range for object detection
step = 10
r,g,b = 203, 31, 25 #red
lower_bgr = np.uint8([b-step, g-step, r-step])
upper_bgr = np.uint8([b + step, g + step, r + step])
# plot mario in BGR and corresponding mask
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(img_test, cv2.COLOR_BGR2RGB))
mask = cv2.inRange(img_test, lower_bgr, upper_bgr)
plt.subplot(1,2,2)
plt.imshow(mask, cmap='gray')
2) hsv 中的阈值(不能正常工作)
# first convert the img, and the associated lower and upper bound to HSV
hsv_img_test = cv2.cvtColor(img_test, cv2.COLOR_BGR2HSV)
lower_hsv = cv2.cvtColor(np.uint8([[[b-step,g-step,r-step]]]), cv2.COLOR_BGR2HSV)
upper_hsv = cv2.cvtColor(np.uint8([[[b+step,g+step,r+step]]]), cv2.COLOR_BGR2HSV)
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(hsv_img_test, cv2.COLOR_BGR2RGB))
# apply threshold on hsv image
mask = cv2.inRange(hsv_img_test, lower_hsv, upper_hsv)
plt.subplot(1,2,2)
plt.imshow(mask, cmap='gray')
...这显然是不正确的。我无法弄清楚代码中有什么问题,非常感谢任何帮助!
最佳答案
似乎意外行为来自 uint8 数组格式。我没有弄清楚确切的原因,但您应该谨慎使用无符号整数运算(例如:0 - 1 = 255)。
最后我想我设法获得了您可能正在寻找的结果:
# first convert the img to HSV
img_test_hsv = cv2.cvtColor(img_test, cv2.COLOR_BGR2HSV)
# convert the target color to HSV
target_color = np.uint8([[[b, g, r]]])
target_color_hsv = cv2.cvtColor(target_color, cv2.COLOR_BGR2HSV)
# boundaries for Hue define the proper color boundaries, saturation and values can vary a lot
target_color_h = target_color_hsv[0,0,0]
tolerance = 2
lower_hsv = np.array([max(0, target_color_h - tolerance), 10, 10])
upper_hsv = np.array([min(179, target_color_h + tolerance), 250, 250])
plt.figure(figsize=(20,10))
plt.subplot(1,2,1)
plt.imshow(cv2.cvtColor(img_test_hsv, cv2.COLOR_HSV2RGB));
# apply threshold on hsv image
mask = cv2.inRange(img_test_hsv, lower_hsv, upper_hsv)
plt.subplot(1,2,2)
plt.imshow(mask, cmap='gray');
要考虑的另一点是不同颜色空间 RGB 和 HSV 的拓扑差异。 RGB 空间中的框不会转换为 HSV 坐标中的框。引用下面的维基百科文章: HSL ans HSV Color Spaces
关于python - opencv - python - 在 cv2.inRange 中使用 HSV 颜色时感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43305234/
我必须使用 opencv 开发 android 代码,代码相当于 MATLAB。因为我必须读取图像并将其转换为 HSV,我已经使用: Imgproc.cvtColor(temp, hsv , Im
我正在寻找从 RGB 到 HSV 的颜色空间转换器,特别是用于两种颜色空间的 0 到 255 范围。 最佳答案 我已经使用了很长时间 - 现在不知道它们是从哪里来的......请注意,输入和输出,除了
我正在使用 openCV 3.1.0(我试过 2.4.9,但有同样的问题)。我想将 HSV 垫输出到 jpeg: // .. Getting JPEG content into memory // J
我有这段代码可以将 RGB 转换为 HSV func rgbToHSV (_ r:Double, _ g:Double, _ b:Double) -> (Double, Double, Doubl
我不熟悉图像检测技术。我正在使用 java openCV 来检测图像中的颜色。我能够检测到黄色和红色。我从命中和试用方法中获得了值(value),这些方法工作正常。但我还想从下面的手图像中检测绿色、橙
我正在开发一款应用程序, children 需要在其中使用相机屏幕上的特定目标点来找到特定的颜色 - 有点像增强现实。我已经完成了所有工作,但使用了 RGB 颜色。对于每种颜色,红色、绿色、蓝色、黄色
我有一个将 RGB 颜色空间中的 vector 转换为 HSV 的函数 - 该例程通过了所有测试并且在绝大多数时间都有效。然而,崩溃报告表明它偶尔会失败(0.0001% 的调用或更少)。但是,由于此函
我正在开发一个应用程序,该应用程序创建的图像的色调、饱和度和值会根据不同的参数发生变化。出于性能原因,最好分别渲染色调、饱和度和值组件,然后使用 Photoshop 风格的混合模式(乘法、叠加、屏幕、
我正在使用 matplotlib 中的 HSV 颜色图来绘制一些向量场。有没有办法使 HSV 颜色变暗或变得更平滑,使它们看起来更像这样 比我原来的绘图颜色太亮: 最佳答案 简介 假设您正在尝试绘制这
我正在尝试遍历 HSV 图像,但它一直在崩溃。 Mat a=imread("play.jpg"); Mat hsvimage, hsvimage2,cont; cvtColor(a,
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭10 年前。 Improve th
我听说,如果您需要在软件上进行颜色分割(如果满足某些阈值规则,例如 R100、10<,则通过将像素设置为 1,从彩色图像创建二进制图像) B < 123) 最好先将图像转换为 HSV。这是真的吗?为什
我正在 win32 api 中创建一个应用程序,它将使用进度条。这个进度条应该改变它的颜色。从红色(左端)到绿色(右端),中间有一些黄色。 我搜索了一下,发现我应该使用 HSV 来实现这一点。我就是不
虽然我通常更喜欢 RGB 颜色空间而不是 HSV,但我希望 SKEmitterNode 发射具有最大饱和度和值的粒子,并且具有涵盖所有颜色的色调值。这可能吗? 我是 XCode、Swift 和 Spr
假设我有两种 RGB 颜色 (255,0,0) 和 (0,0,255),我还需要它们之间的 100 种颜色。我如何根据许多所需步骤生成 HSV 颜色值列表? 当前使用 python 3 我创建了以下代
我正在从如下图像创建一个 hsv 直方图。 - (void)processImageWithHsv:(Mat&)image; { Mat image_hsv; cvtColor(ima
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我正在寻找一种为 3 channel 矩阵的元素赋值的有效方法。特别是,我需要将 HSV 值分配给 2D cv::Mat 的元素,其初始化如下: cv::Mat clusterImage(height
我正在尝试使用 openCV 和 C++ 来均衡 HSV 图像的直方图。我知道有些带有 openCV 的库可以为我执行此操作,但我想手动尝试以了解该方法。 我假设均衡将在 HSV 图像的 V chan
所以我一直在尝试使用 HSV 在一定程度范围内循环颜色,但我似乎无法在颜色之间平滑过渡。理想情况下,我希望能够在特定的度数范围 (80-140) 内移动,并在 0.5 - 1 之间循环饱和度和值。老实
我是一名优秀的程序员,十分优秀!