Python OpenCV:沿x和y轴读取图像

时间:2018-04-16 10:04:53

标签: python image opencv

目标:我试图在此图片中读取第一个点(非零)(显示为红色箭头)

enter image description here

from __future__ import division
import numpy as np

import cv2
im1 = cv2.imread('C:/Users/Desktop/Line.png', 0)
for x in range(0, im1.shape[0], 1):
    for y in range(0, im1.shape[1], 1):
        cpt = im1[x][y]
        if 0 < cpt <= 255 :
           print("This is the value", x,y)

但是,正在打印的点是这个(零点):

enter image description here

这是怎么回事?

原始图片:

enter image description here

1 个答案:

答案 0 :(得分:1)

如果您的图像干净(没有噪音),请尝试以下操作:

import cv2
import numpy as np
img = cv2.imread("JKNp9.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ys,xs = np.nonzero(gray)
idx = np.argsort(xs)[0]

pt = xs[idx], ys[idx]

print(pt)
cv2.line(img, pt, pt, (0,255,0), 3, cv2.LINE_AA)
cv2.imshow("img", img)
cv2.waitKey()

pt是:(27, 388)

这是:

enter image description here