gpt4 book ai didi

python - 更改值时Opencv重绘图像

转载 作者:太空宇宙 更新时间:2023-11-03 22:41:10 25 4
gpt4 key购买 nike

我刚开始用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com