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