如何将输入图像保存到变量中并在另一个函数中调用它?
How to save an input image into a variable and call it in another function?
我写了一个代码来比较两个图像,它工作正常。现在我想开发一个 UI 来浏览两个,将两个图像作为输入,然后我的比较代码应该可以工作并给出结果。
下面是我的代码,两个浏览本地驱动器中的两个不同图像。
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':
现在我下面的代码是两个比较两个图像
# 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)
我需要用户输入的图像保存在图像和图像 1 中进行比较。
如何进行?
您可以使用 event, values = window.read()
行获取您的 sg.Input()
值,并且您可以访问您的输入以获取您的输入。
你的代码将是这样的:
# 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
注意:您应该关闭 window 并从 opencv imshow 中销毁 windows,并在代码末尾使用以下行:
cv2.waitKey()
cv2.destroyAllWindows()
window.close()
我写了一个代码来比较两个图像,它工作正常。现在我想开发一个 UI 来浏览两个,将两个图像作为输入,然后我的比较代码应该可以工作并给出结果。 下面是我的代码,两个浏览本地驱动器中的两个不同图像。
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':
现在我下面的代码是两个比较两个图像
# 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)
我需要用户输入的图像保存在图像和图像 1 中进行比较。 如何进行?
您可以使用 event, values = window.read()
行获取您的 sg.Input()
值,并且您可以访问您的输入以获取您的输入。
你的代码将是这样的:
# 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
注意:您应该关闭 window 并从 opencv imshow 中销毁 windows,并在代码末尾使用以下行:
cv2.waitKey()
cv2.destroyAllWindows()
window.close()