python - 如何将二值图像表示为图形,轴为高度和宽度尺寸,数据为像素

标签 python numpy opencv matplotlib

我正在尝试将 Python 与 opencv、numpy 和 matplotlib 一起使用,为将使用栏杆导航的机器人做一些计算机视觉。我目前非常卡住,没有地方可以看。我目前的代码是:

import cv2
import numpy as np
import matplotlib.pyplot as plt

image = cv2.imread('railings.jpg')
railing_image = np.copy(image)

resized_image = cv2.resize(railing_image,(881,565))

gray = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
canny = cv2.Canny(blur, 85, 255)

cv2.imshow('test',canny)

image_array = np.array(canny)
ncols, nrows = image_array.shape

count = 0
scan = np.array
for x in range(0,image_array.shape[1]):
    for y in range(0,image_array.shape[0]):
        if image_array[y, x] == 0:
            count += 1
    scan = [scan, count]

print(scan)

plt.plot([0, count])
plt.axis([0, nrows, 0, ncols])
plt.show()

cv2.waitKey(0)

我正在使用存储在 1 和 0 数组中的精明图像,我需要表示的图像是

Source image

最终结果应类似于下图。

terrible_drawing_of_image

我试过使用直方图函数,但我只设法让它基本上输出 1 或 0 出现次数的计数。

如果有人可以帮助我或将我指向正确的方向,那将生成一个表示高度和宽度尺寸图中的图像像素的图表。

谢谢

最佳答案

我不确定这有多普遍,但您可以使用 numpy argmax在您的情况下获取最大值的位置(如 this )。你应该避免循环,因为这会很慢,最好使用 numpy 函数。我已经导入了您的图像并使用了黄色 channel 中 200 或更多是栏杆的截止标准,

import cv2
import numpy as np
import matplotlib.pyplot as plt

#This loads the canny image you uploaded
image = cv2.imread('uojHJ.jpg')

#Trim off the top taskbar
trimimage = image[100:, :,0]

#Use argmax with 200 cutoff colour in one channel
maxindex = np.argmax(trimimage[:,:]>200, axis=0)

#Plot graph
plt.plot(trimimage.shape[0] - maxindex)
plt.show()

如下所示:

enter image description here

关于python - 如何将二值图像表示为图形,轴为高度和宽度尺寸,数据为像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54832694/

相关文章:

python - Bokeh 中的 TimeSeries 使用带索引的数据框

python - balebot 支付信息错误

python - matplotlib 中的绿色到红色颜色图,以数据的中位数为中心

python - axis = 0 在 Numpy 的 sum 函数中做了什么?

python - 转换自然范围外的角度(对于该范围内的 theta : 0 to 180 degrees) to an equivalent angle,

xcode - 在 mac os x 10.6 上安装 Opencv 以与 Xcode 3.2.2 项目一起使用

c++ - 使用复制赋值构造函数和迭代元素来修改 Mat 之间是否存在显着的性能差异?

python - OpenCV-Python : How Do I split an Image in a grid?

Python 2.7.11 将输出写入文本文件

python - 使用索引列表访问项目列表