简体   繁体   English

如何将输入图像保存到变量中并在另一个 function 中调用它?

[英]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.

相关问题 如何从一个函数调用一个变量到另一个函数? - How to call a variable from one function to another? 如何将另一个变量输入到 scipy 优化 function - How to input another variable into scipy optimize function 如何将图像保存为变量? - How to save an image as a variable? 如何将变量从一个 function 调用到另一个 function? - How to call a variable from one function into another function? 如何在函数内调用变量然后在另一个函数中使用它 - How to call a variable inside a function then use it in another function PYTHON:如何创建一个函数来接收变量输入,然后将其发送到另一个函数 - PYTHON: how to create a function to receive a variable input and then send it to another function 调用另一个 function 中的变量 - Call to a variable that is inside another function 如何将图像存储在变量中并将该变量作为参数传递给另一个 function? - how to store an image in a variable and pass that variable as a argument in another function? 如何使用输入函数调用变量的值? - How do I use the input function to call the value of a variable? 如何从另一个脚本的函数调用python变量? - How to call a python variable from another script's function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM