1
我是Python的新手。Python + OpenCV:枚舉矩陣
我想從opencv訪問Mat(一個閾值二進制圖像)元素,並創建一個根據x軸的直方圖,所以我可以垂直做一些圖像分割。
我所做的是預處理圖像並枚舉兩次,如下所示。
def crack(src):
#src is a binary image
src = cv2.resize(src, (0,0), fx=6, fy=6)
thresh = preprocessing(src)
#create empty histogram
print(thresh.shape)
height, width = thresh.shape
size = height ,255, 3
hist = np.zeros(size, dtype=np.uint8)
#enumerate elements
for y, x in enumerate(thresh):
val = 0
for x, pix in enumerate(x):
val = val+ pix/255
print y,x, val
cv2.rectangle(hist, (y, 255-val), (y+val, 255-val+1), (0, 255, 0), 1)
cv2.imshow("thresh", thresh)
cv2.imshow("hist", hist)
如果我直接枚舉閾墊等
for y, x in enumerate(thresh):
我只能從外y軸的枚舉第一然後枚舉x軸。那麼我怎樣才能做到這一點? 我的目標是得到這樣的形象:
image1 http://7xsnr6.com2.z0.glb.clouddn.com/QQ%E5%9B%BE%E7%89%8720160509184009.jpg
圖片引用:
傑夫顏,在微軟CAPTCHA