提高 OpenCV 中提取图像的质量
#Segmenting the red pointer
img = cv2.imread('flatmap.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([140, 110, 0])
upper_red = np.array([255, 255 , 255])
# Threshold with inRange() get only specific colors
mask_red = cv2.inRange(hsv, lower_red, upper_red)
# Perform bitwise operation with the masks and original image
red_pointer = cv2.bitwise_and(img,img, mask= mask_red)
# Display results
cv2.imshow('Red pointer', red_pointer)
cv2.imwrite('redpointer.jpg', red_pointer)
cv2.waitKey(0)
cv2.destroyAllWindows()
我有一张地图,需要提取红色箭头。代码有效,但箭头中有黑色斑点。我将如何更改代码以改进箭头的输出,使其成为实体形状?
#Segmenting the red pointer
img = cv2.imread('flatmap.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower_red = np.array([140, 110, 0])
upper_red = np.array([255, 255 , 255])
# Threshold with inRange() get only specific colors
mask_red = cv2.inRange(hsv, lower_red, upper_red)
# Perform bitwise operation with the masks and original image
red_pointer = cv2.bitwise_and(img,img, mask= mask_red)
# Display results
cv2.imshow('Red pointer', red_pointer)
cv2.imwrite('redpointer.jpg', red_pointer)
cv2.waitKey(0)
cv2.destroyAllWindows()
I have a map and need to extract the red arrow. The code works but the arrow has black patches in it. How would I go about altering the code to improve the output of the arrow so it's a solid shape?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用:
out.jpg
最终看起来像< /a>
三角形有已被蓝色填充。
You could use:
out.jpg
ends up looking likewhere the triangle has been filled in with blue.
我查看了 HSL/HSV 空间中的通道。
箭头是图片中唯一具有饱和度的东西。这将是锁定所需箭头所需的(但不充分)方面之一。我选择了这些像素,它们的饱和度似乎略高于 50%,因此我将使用 25% (64) 的下限。
红色箭头的色调在 0 度(红色)附近抖动...这意味着它的一些像素位于 0 的负一侧,即大约 359 度。
您需要使用两次
inRange
调用来收集从 0 向上的所有色调以及从 359 向下的所有色调。由于 OpenCV 以 2 度为步长对色调进行编码,因此该值将为 180 及以下。我将选择 0 +- 20 度(0 .. 10
和170 .. 180
)。总之:
I've looked at the channels in HSL/HSV space.
The arrows are the only stuff in the picture that has any saturation. That would be one required (but insufficient) aspect to get a lock on the desired arrow. I've picked those pixels and they appear to have a bit more than 50% saturation, so I'll use a lower bound of 25% (64).
That red arrow's hue dithers around 0 degrees (red)... that means some of its pixels are on the negative side of 0, i.e. something like 359 degrees.
You need to use two
inRange
calls to collect all hues from 0 up, and all hues from 359 down. Since OpenCV encodes hues in 2-degree steps, that'll be a value of 180 and down. I'll select 0 +- 20 degrees (0 .. 10
and170 .. 180
).In summary: