更改值后,OpenCV重绘图像

时间:2018-12-03 15:20:56

标签: python python-3.x opencv matplotlib tkinter

我刚开始使用OpenCV,一直在使用harris拐角检测功能。我编写了一个脚本来查找图片中的黑色边缘。

我一直在努力实现动态调整图像阈值的滑块。

目前,滑块仅在加载图像之前设置值。使用滑块更改值时不会更新。

在移动滑块时,如何使滑块连续更新图像?

预先感谢

from threading import Thread
import time
import numpy as np
import cv2
from matplotlib import pyplot as plt
import tkinter
import sys

slideVal =1
def showVal(value):
        global slideVal
        value = int(value)
        print (type(value))
        slideVal = value

def harrisCorn():
        time.sleep(3)
        print(slideVal)

        print ("the current slider value is: ",slideVal)
        img = cv2.imread('rawGun.jpg')
        gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
        gray = np.float32(gray)
        #Changeing the value's in this function with : slideVal
        dst = cv2.cornerHarris(gray,slideVal,3,0.04)
        #result is dilated for marking the corners, not important
        dst = cv2.dilate(dst,None)

        # Threshold for an optimal value, it may vary depending on the image.
        img[dst>0.01*dst.max()]=[0,0,255]

        imgSize = cv2.resize(img,(1000,1000))

        cv2.imshow('dst', imgSize)
        if cv2.waitKey(0) & 0xff:
                cv2.destroyAllWindows()

def slider():
        root = tkinter.Tk()
        s = tkinter.Scale(orient='horizontal', from_=1, to=225, command=showVal)
        s.pack()
        root.mainloop()

if __name__ == '__main__':
    Thread(target = slider).start()
    time.sleep(3)
    Thread(target = harrisCorn).start()

1 个答案:

答案 0 :(得分:1)

实际上,您可以将horrisCorn()用作滑块的command功能,以便无论何时更改滑块,都将更新图像。下面更改的代码块基于您的代码:

import numpy as np
import cv2
import tkinter

def harrisCorn(slideVal):
    slideVal = int(slideVal)
    print("the current slider value is: ", slideVal)
    img = cv2.imread('rawGun.jpg')
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = np.float32(gray)
    #Changeing the value's in this function with : slideVal
    dst = cv2.cornerHarris(gray, slideVal, 3, 0.04)
    #result is dilated for marking the corners, not important
    dst = cv2.dilate(dst, None)
    # Threshold for an optimal value, it may vary depending on the image.
    img[dst>0.01*dst.max()] = [0, 0, 255]
    imgSize = cv2.resize(img, (1000, 1000))
    cv2.imshow('dst', imgSize)

def slider():
    root = tkinter.Tk()
    tkinter.Scale(orient='horizontal', from_=1, to=225, command=harrisCorn).pack()
    harrisCorn(1)
    root.mainloop()

if __name__ == '__main__':
    slider()