[英]RGB values as tuple in python
我是编程新手,练习有问题。 我有一个重现图像的 rgb 值列表。 我需要一个 output 具有以下值的字符串:X、Y、W、H、R、G、B 其中 X 和 Y 是坐标,W 和 H 是矩形的宽度和高度,R GB 是 colors。
例如:
image=[[(255, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(255, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 255, 0), (0, 255, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 255, 0), (0, 255, 0)]]
我需要这个 output:
0,0,2,2,255,0,0
4,4,2,2,0,255,0
我正在尝试使用坐标,但我被卡住了。
xy=[]
for row in image:
for x in row:
if x != (0,0,0):
xy.append(row.index(x))
output:
xy=[0, 0, 0, 0, 4, 4, 4, 4]
任何帮助深表感谢!
有和没有枚举的例子。
# Some scratch code
image = [
[(255, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(255, 0, 0), (255, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 255, 0), (0, 255, 0)],
[(0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 255, 0), (0, 255, 0)]]
image_width = len(image[0])
image_height = len(image)
print(f'{image_height=} {image_width=}')
print(' xy without enumerate'.center(40, '*'))
xy = ''
for row in range(image_height):
for column in range(image_width):
result = (f'{column},{row},{image_width},{image_height},'
f'{image[row][column][0]},'
f'{image[row][column][0]},'
f'{image[row][column][0]}')
xy = xy + result + '\n'
print(xy)
print(' xy with enumerate'.center(40, '*'))
xy = ''
""
for row, _ in enumerate(image):
for column, _ in enumerate(image[0]):
result = (f'{column},{row},{image_width},{image_height},'
f'{image[row][column][0]},'
f'{image[row][column][0]},'
f'{image[row][column][0]}')
xy = xy + result + '\n'
print(xy)
你为什么不使用二维图像? 使用此表示,您可能会丢失空间信息(垂直信息)。
另外,你没有解释你想如何检测你的矩形,它们可以有任何颜色吗? 那么它会更难,我建议使用图像(numpy,pillow,opencv 或类似的)
如果你仍然想这样做,我们可以假设矩形有 3 colors (R, G or B)
# define colors
R = (255, 0, 0)
G = (0, 255, 0)
B = (0, 0, 255)
# detect them
rs = []
gs = []
bs = []
# loop for each row
for j, row in enumerate(image):
# and for each value
for k, val in enumerate(row):
if val == R:
rs.append((j,k))
if val == G:
gs.append((j,k))
if val == B:
bs.append((j,k))
print(rs, gs, bs)
# now you can extract the values
if len(rs) > 0:
# the coordinates are the first value
R_x, R_y = rs[0]
# width is last minus first (+1)
R_w = (rs[-1][0] - rs[0][0]) + 1
# height the same
R_h = (rs[-1][1] - rs[0][1]) + 1
print(R_x, R_y, R_w, R_h, R)
if len(gs) > 0:
G_x, G_y = gs[0]
G_w = (gs[-1][0] - gs[0][0]) + 1
G_h = (gs[-1][1] - gs[0][1]) + 1
print(G_x, G_y, G_w, G_h, G)
output 是0 0 2 2 (255, 0, 0)
你可能需要在元组中调整它,但这应该很容易。
您可以对 rs、gs 和 bs 执行此操作(即使没有蓝色矩形),gs 的 output 是4 4 2 2 (255, 0, 0)
,这是您想要的。
这是你的问题吗?
注意:如前所述,如果矩形可以是任何颜色,这将不起作用,也许可以遵循一些教程?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.