简体   繁体   English

plot 这个白色像素有正确的方法吗?

[英]is there a correct way to plot this white pixel?

iam a beginner at python and image processing etc. I want to plot this white pixel.我是 python 和图像处理等方面的初学者。我想要 plot 这个白色像素。 as i know the pixel color identifier for black is 0 and for white is 255. here's the image that i want to plot:据我所知,黑色的像素颜色标识符为 0,白色为 255。这是我想要的图像 plot:

The Image图片

i try to print out the image ndarray with these following command:我尝试使用以下命令打印出图像 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()

this is what i get..这就是我得到的..

output image output 图片

i think it's very noisy but i dont have a clue.我认为它很吵,但我不知道。 Thank you very much for help非常感谢你的帮助

I think there is some confusion in the dimensions of you array arr0 .我认为数组arr0的维度有些混乱。 I think you should look at the indices of the white pixels:我认为您应该查看白色像素的索引

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()

Note: taking the mean because a vertical line can have more than one white pixel (also some won't have any, see picture below)注意:取mean因为一条垂直线可以有多个白色像素(也有一些没有,见下图)

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM