提高 OpenCV 中提取图像的质量

发布于 2025-01-14 07:32:09 字数 751 浏览 0 评论 0原文

#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?

Map

Arrow

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2025-01-21 07:32:09

您可以使用:

  1. 扩张来填充形状中的内部噪声
  2. 外部轮廓查找来获取三角形凸包的轮廓
  3. 以进一步平滑它
import cv2
import numpy as np

img = cv2.imread('dCkpC.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_red = np.array([140, 60, 0])
upper_red = np.array([255, 255, 255])

mask_red = cv2.inRange(hsv, lower_red, upper_red)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
mask_red = cv2.dilate(mask_red, element)

contours, _ = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
hull_list = [cv2.convexHull(contour) for contour in contours]
drawing = np.zeros_like(img)
for hull in hull_list:
    cv2.fillConvexPoly(img, hull, (255, 0, 0))

cv2.imshow('Image', img)
cv2.imwrite('out.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

out.jpg 最终看起来像

在此处输入图像描述< /a>

三角形有已被蓝色填充。

You could use:

  1. dilate to fill up the internal noise in the shape
  2. external contour finding to get the outline of the triangle
  3. convex hull to further smooth it out
import cv2
import numpy as np

img = cv2.imread('dCkpC.jpg')
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

lower_red = np.array([140, 60, 0])
upper_red = np.array([255, 255, 255])

mask_red = cv2.inRange(hsv, lower_red, upper_red)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
mask_red = cv2.dilate(mask_red, element)

contours, _ = cv2.findContours(mask_red, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
hull_list = [cv2.convexHull(contour) for contour in contours]
drawing = np.zeros_like(img)
for hull in hull_list:
    cv2.fillConvexPoly(img, hull, (255, 0, 0))

cv2.imshow('Image', img)
cv2.imwrite('out.jpg', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

out.jpg ends up looking like

enter image description here

where the triangle has been filled in with blue.

审判长 2025-01-21 07:32:09

我查看了 HSL/HSV 空间中的通道。

箭头是图片中唯一具有饱和度的东西。这将是锁定所需箭头所需的(但不充分)方面之一。我选择了这些像素,它们的饱和度似乎略高于 50%,因此我将使用 25% (64) 的下限。

饱和度

红色箭头的色调在 0 度(红色)附近抖动...这意味着它的一些像素位于 0 的一侧,即大约 359 度。

色调色调hue

您需要使用两次 inRange 调用来收集从 0 向上的所有色调以及从 359 向下的所有色调。由于 OpenCV 以 2 度为步长对色调进行编码,因此该值将为 180 及以下。我将选择 0 +- 20 度(0 .. 10170 .. 180)。

总之:

hsv_im = cv.cvtColor(im, cv.COLOR_BGR2HSV)

mask1 = cv.inRange(hsv_im, np.array([0, 64, 0]), np.array([10, 255, 255]))
mask2 = cv.inRange(hsv_im, np.array([170, 64, 0]), np.array([180, 255, 255]))
mask = mask1 | mask2

cv.imshow("mask", mask)
cv.waitKey()

“面具”

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).

saturation

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.

hue hue hue

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 and 170 .. 180).

In summary:

hsv_im = cv.cvtColor(im, cv.COLOR_BGR2HSV)

mask1 = cv.inRange(hsv_im, np.array([0, 64, 0]), np.array([10, 255, 255]))
mask2 = cv.inRange(hsv_im, np.array([170, 64, 0]), np.array([180, 255, 255]))
mask = mask1 | mask2

cv.imshow("mask", mask)
cv.waitKey()

mask

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文