[英]is there a correct way to plot this white pixel?
我是 python 和图像处理等方面的初学者。我想要 plot 这个白色像素。 据我所知,黑色的像素颜色标识符为 0,白色为 255。这是我想要的图像 plot:
我尝试使用以下命令打印出图像 ndarray:
#importing module
import cv2
import numpy as np
import matplotlib.pyplot as plt
import sys
#load image
img = cv2.imread('thin.png')
#image to nd.array
arr0 = np.array(img)
#finding white pixel
arr1 = np.where(arr0 == 255)
#indexing tuple, the printout arr1 is tuple with dtype=int64
tuple1 = arr1[0]
tuple2 = arr1[1]
tuple3 = arr1[2]
#defining x and y axis
x = np.array(tuple1)
y = np.array(tuple2)
z = np.array(tuple3)
plot = plt.plot(x,y)
plt.show()
这就是我得到的..
我认为它很吵,但我不知道。 非常感谢你的帮助
我认为数组arr0
的维度有些混乱。 我认为您应该查看白色像素的索引:
import cv2
import numpy as np
import matplotlib.pyplot as plt
#load image
img = cv2.imread('thin.png')
#image to nd.array
arr0 = np.array(img)
height, width, _ = arr0.shape
x = range(width)
y = [height - np.argwhere(arr0[:, i, 0]==255).mean() for i in x] # "height -" to reverse the y-axis
plt.plot(x,y)
plt.show()
注意:取mean
因为一条垂直线可以有多个白色像素(也有一些没有,见下图)
Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.