我有我想要复制到数组/列表的1x128灰度图像。 当我读出的图像我看到它只有一个指数大:n = len(res)
如何在OpenCV中访问图像的像素值
我想读出每个像素值,并且说,如果像素I> 128它必须是1,否则必须为0
for index in res:
if res[h] > 128:
res[h] = 1
else:
res[h] = 0
h = h + 1
但资源只有一个元素,看起来像这样:res = [[ 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 128 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255]]
我想这有一个数组,这样我可以把每一个指标(即是转换(到1或0)到输入的神经网络
Can s omeone解释我该如何做到这一点? 这里是我的一块主要的:
if __name__ == "__main__":
# Camera ID to read video from (numbered from 0)
camera_id = 0
dev = open_camera(camera_id) # open the camera as a video capture device
while True:
img_orig = get_frame(dev) # Get a frame from the camera
if img_orig is not None: # if we did get an image
cv2.imshow("video", img_orig) # display the image in a window named "video"
else: # if we failed to capture (camera disconnected?), then quit
break
gray = cv2.cvtColor(img_orig, cv2.COLOR_BGR2GRAY)
cv2.imshow("video1", gray) # display the image in a window named "video"
crop = gray[310:410, 0:1280]
gauss = cv2.GaussianBlur(crop,(5,5),0)
(thresh, im_bw) = cv2.threshold(gauss, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('frame2',im_bw)
height1, width1 = gauss.shape
res = cv2.resize(im_bw,(width1/5, height1/100), interpolation = cv2.INTER_LINEAR)
height2, width2 = res.shape
cv2.imshow('frame3',res)
n = len(res)
h = 0
for index in res:
if res[h] > 128:
res[h] = 1
else:
res[h] = 0
h = h + 1
#print "Binair is: %s" % img_binair
print "image line is: %s" % res
尽可能避免python for循环。你可以使用numpy索引。尝试'y = np.where(x> 127,1,0)' –