我有我想要複製到數組/列表的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)' –