【发布时间】:2023-03-10 22:08:01
【问题描述】:
好的,所以我写了一些代码,我想比较两组。但是,长度只会返回 0 或 1,这取决于我使用的是两个图像还是同一个图像。这是因为我的集合仅形成为 1 个元素集合,而不是将数字混合在一起。例如,集合读作 [(a, b, c)] 而不是 [('a', 'b', 'c')]。
这是我的代码
import cv2
import numpy as np
import time
N=0
colour=[]
colourfile=open('Green from RGB.txt', 'r')
for line in colourfile.readlines():
colour.append([line])
colour_set=sorted(set(map(tuple, colour)))
def OneNumber(im): #Converts the pixels rgb to a single number.
temp_im=im.astype('int32')
r,g,b = temp_im[:,:,0], temp_im[:,:,1], temp_im[:,:,2]
combo=r*1000000+g*1000+b
return combo
while True:
cam = cv2.VideoCapture(0)
start=time.time()
while(cam.isOpened()): #Opens camera
ret, im = cam.read() #Takes screenshot
#im=cv2.imread('filename.type')
im=cv2.resize(im,(325,240)) #Resize to make it faster
im= im.reshape(1,-1,3)
im=OneNumber(im) #Converts the pixels rgb to a singe number
im_list=im.tolist() #Makes it into a list
im_set=set(im_list[0]) #Makes set
ColourCount= set(colour_set) & set(colour_set) #or set(im_set) for using/ comparing camera
print len(ColourCount)
我打开的文本文件也写成:
126255104, 8192000, 249255254, 131078, 84181000, 213254156,
在一个伟大的大行中。
所以基本上,我如何将数字分成集合中的不同元素,im_set 和 colour_set?
谢谢
【问题讨论】:
-
这很难理解。请发MCVE。