[英]How to save an input image into a variable and call it in another function?
I have written a code to compare two images and it is working fine.我写了一个代码来比较两个图像,它工作正常。 Now I want to develop an UI to browse the two get the two images as an input and then my code of comparison should work and give the results.现在我想开发一个 UI 来浏览这两个获取两个图像作为输入,然后我的比较代码应该可以工作并给出结果。 Below is my code two browse the two different images from my local drive.下面是我的代码二从我的本地驱动器浏览两个不同的图像。
import PySimpleGUI as sg
import cv2
import numpy as np
supportedextensions = ['jpg','png']
layoutprefile = [
[sg.Text('Select two images to proceed')],
[sg.Text('Origi Image'), sg.Input(), sg.FileBrowse()],
[sg.Text('Input Image'), sg.Input(), sg.FileBrowse()],
# *list1,
[sg.Output(size=(61, 5))],
[sg.Submit('Compare'), sg.Cancel('Cancel')]
]
window = sg.Window('Image Compare', layoutprefile)
while True:
event, values = window.read()
if event in (None, 'Exit', 'Cancel'):
secondwindow = 0
break
elif event == 'Compare':
Now my below code is two compare two images现在我下面的代码是两个比较两个图像
# Original image
image =
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram = cv2.calcHist([gray_image], [0],
None, [256], [0, 256])
# Input1 image
image1 =
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
histogram1 = cv2.calcHist([gray_image1], [0],
None, [256], [0, 256])
c1 = 0
i = 0
while i<len(histogram) and i<len(histogram1):
c1+=(histogram[i]-histogram1[i])**2
i+= 1
c1 = c1**(1 / 2)
if(c1==0):
print("Input image is matching with original image.")
elif(c1>0 or c1<0):
print("Input image is not matching with original image")
imS = cv2.resize(image, (250, 200))
imS1 = cv2.resize(image1, (250, 200))
cv2.imshow("Original", imS)
cv2.imshow("Input1", imS1)
I need user input image to save in image and image 1 to compare.我需要用户输入图像保存在图像和图像 1 中进行比较。 How to proceed to that?如何进行?
you can get your sg.Input()
values with the line event, values = window.read()
and you can access your inputs in order to your input.您可以通过 line 事件获取sg.Input()
值event, values = window.read()
并且您可以访问您的输入以获取您的输入。 your code will be like this:你的代码将是这样的:
# Original image
image = cv2.imread(values[0])
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
histogram = cv2.calcHist([gray_image], [0],
None, [256], [0, 256])
# Input1 image
image1 = cv2.imread(values[1])
gray_image1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
histogram1 = cv2.calcHist([gray_image1], [0],
None, [256], [0, 256])
break
Note: you should close your window and destroy windows from opencv imshow with these lines at the end of your code:注意:您应该关闭 window 并从 opencv imshow 中销毁 windows,并在代码末尾使用这些行:
cv2.waitKey()
cv2.destroyAllWindows()
window.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.