【问题标题】:How can i get the RGB value of specific straight line in the image?如何获取图像中特定直线的 RGB 值?
【发布时间】:2019-09-20 15:57:53
【问题描述】:

这是一个有两条红色(无论颜色)线的图像。我想检测该行,然后想获取该行的 rgb 值。如何使用 OpenV 或任何其他 python 库来做到这一点。

我尝试了这种打印许多值列表的代码:

import cv2

img = cv2.imread('C:/Users/Rizwan/Desktop/result.jpg')

print(img)

【问题讨论】:

  • 你能提供更多细节吗?您只需要知道这些线条的 RGB 值还是要先定位它们?
  • 也许将您的图片发布到一些免费的托管服务,然后将 URL 放在这里。
  • 要获取您的特定行,您可以使用 numpy 切片。要获取该 ROI 的 RGB 值,您可以使用 cv2.split()
  • Stack Overflow 不是免费的代码编写服务。您应该尝试自己编写代码。对于图像处理问题,您通常必须提供图片。
  • @aminrd 首先,如果图像中存在线条,我想在图像中定位这些线条,然后我想获取这些线条的 rgb 值。

标签: python opencv image-processing


【解决方案1】:

一种可能性是转到HSV colourspace 并寻找红色调。红色更难找到,因为它们在色相/饱和度/值轮上跨越 0 度和 360 度,所以我将反转图像并寻找青色,这是红色将出现在反转图像中的位置。

#!/usr/bin/env python3

import numpy as np
import cv2

# Load the image as BGR
im = cv2.imread('strip.jpg')

# Invert image, i.e. 255-im, to make reds into cyan and convert to HSV
hsv = cv2.cvtColor(255-im, cv2.COLOR_BGR2HSV) 

# Set low and high limit for the tones we want to identify - based on Hue of cyan=90 in OpenCV 
lo = np.uint8([80,30,0])
hi = np.uint8([95,255,255]) 

# Mask all red pixels
mask = cv2.inRange(hsv,lo,hi)

# Save mask for fun
cv2.imwrite('result1.png',mask)

这给出了这个:

继续编写代码,我们现在将该遮罩应用于原始图像以使不感兴趣的像素变黑:

# Zero out to black all uninteresting pixels in original image
im[mask<255] = 0
cv2.imwrite('result2.png',im)

# Reshape as a tall column of R, G, B values and find unique rows, i.e. unique colours
unique = np.unique(im.reshape(-1,3), axis=0)
print(unique)

这提供了大约 700 个 RGB 三元组。继续,抓取独特的颜色并写入图像,以便我们可以看到它们:

# This line is a hack based solely on the current image just for illustration
cv2.imwrite('unique.png',unique[:625,:].reshape((25,25,3)))

【讨论】:

  • 感谢 Mark Setchell,这对我帮助很大。请确定我如何获取这些检测到的线条的 RGB 值?
  • print(unique) 的行打印它们。尝试运行代码。
  • 通过运行代码我得到了这些 [[ 31 41 48] [ 33 43 50] [ 34 44 51] ... [241 243 243] [242 240 240] [242 244 244]]价值观。这些价值观是什么?如何理解这些价值观?请解释一下,我会感谢你的。
  • 这意味着有颜色为rgb(48,41,31) 的像素和颜色为rgb(50,43,33) 的像素......请记住,OpenCV 以 BGR 格式存储。
  • 这些 [241 243 243] [242 240 240] [242 244 244]] 值是什么?这些是二线的吗?
猜你喜欢
  • 2016-10-04
  • 2011-12-10
  • 2020-06-20
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 1970-01-01
  • 2013-08-08
  • 2010-09-13
相关资源
最近更新 更多