好的,所以我写了一些代码,我想比较两个集合。但是,长度将只返回0或1,具体取决于我使用的是两张图像还是同一张图像。这是因为我的套装只有1套,而不是将数字混合在一起。例如,这些集合读为[(a,b,c)]而不是[('a','b','c')]。为什么我的集合不包含多个元素? - python 2.7
这里是我的代码
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](http://stackoverflow.com/help/mcve)。 – timgeb