如何绘制阈值变化的阈值图像

时间:2019-02-19 21:45:00

标签: python matplotlib image-processing

我正在用matplotlib绘制阈值numpy图像,我想在运行时更改阈值,我的意思是在图中查看阈值图像如何随值变化,matplotlib中是否有任何功能需要执行是吗?

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

img = cv2.imread("image.jpg", 0 )
thresh_val = 127
thresh_image = cv2.threshold(img, thresh_val, 255, cv2.THRESH_BINARY)

plt.imshow(thresh_image) # how to change the thresh value here
plt.show()

1 个答案:

答案 0 :(得分:0)

您可以通过以下方式简单地使用opencv(cv2):创建跟踪栏以实时更改阈值,并使用cv2.imshow()方法显示图像(而不是绘制图像),

def callback(value):
    pass

cv2.namedWindow("Trackbar",0)  # create a window with the trackbar
cv2.createTrackbar('thresshold_value',"Trackbar",0,255,callback) # add the track bar to the window

while True:

    img = cv2.imread("image.jpg",0)
    thresh_val = cv2.getTrackbarPos('thresshold_value',"Trackbar")
    thresh_image = cv2.threshold(img, thresh_val, 255, cv2.THRESH_BINARY)

    cv2.imshow("th", thresh_image) # display the image
    cv2.waitKey(1)  # wait for 1 milliseconds for the display