python - 更改值时Opencv重绘图像

标签 python python-3.x opencv matplotlib tkinter

我刚开始用OpenCV,一直在用harris角点检测功能。我写了一个脚本来寻找图片中的黑边。

我一直在努力实现一个 slider 来动态调整图像的阈值。

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

当您移动 slider 时,我该如何让 slider 持续更新图像?

提前致谢

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

最佳答案

实际上,您可以使用 horrisCorn() 作为 slider 的 command 函数,这样只要 slider 发生变化,图像就会更新。下面更改的代码块是基于您的代码:

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

关于python - 更改值时Opencv重绘图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53596678/

相关文章:

maven - 如何在 Maven 的 pom.xml 中找到 '"libopencv_java.so"和 "libnative_camera_r2.2.2.so"的 artifactId 和版本信息?

c++ - OpenCV:filter2D函数的计算效率

python - 如何修复 "TypeError: len() of unsized object"

string - 从 Pandas 数据框中的字符串中删除 "x"个字符?

python - HoughLinesP 未检测到预期的线条

python - 我们可以在 python 3.3 及更高版本中用 find_namespace_packages 替换 find_packages 并删除空的 init.py 文件吗?

python - 如何根据 Plotly 美国航类 map 示例中的航空公司更改颜色

python - Python 中的快速 RGB 阈值处理(可能是一些智能 OpenCV 代码?)

python - Python 实现中的 Sha-3

python - 查找我的套接字的公共(public) IP 地址?