目标:我正在尝试读取此图像中的第一个点(非零点)(显示为红色箭头)
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)
但是,打印的点是这个(零点):
怎么会这样?
原始图像:
最佳答案
如果你的图像很干净(没有噪点),那么试试这个:
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)
就是这样:
关于Python OpenCV : Reading an image along x and y axis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49854608/