简体   繁体   English

如何使用 OpenCV 和 Python 以正确的方式将多个图像传递给函数

[英]How to pass multiple images to a function in a correct way using OpenCV with Python



I have 2 python files and one picture:我有 2 个 python 文件和一张图片:

functions.py函数.py

import numpy as np 
import cv2

def imageinput(image_name_with_extension):

    img = cv2.imread(image_name_with_extension,1)
    grayscale = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    cv2.imshow("grayscaled.jpg",grayscale)

my picture: image1.jpg我的图片: image1.jpg

and the next python file: main.py和下一个python文件: main.py

import numpy as np 
import cv2
from functions import imageinput


print(imageinput(image1.jpg))

and the following error message:以及以下错误消息:

print(imageinput(image1.jpg))打印(图像输入(图像1.jpg))
NameError: name 'image1' is not defined NameError: 名称 'image1' 未定义

This is a simplified example of my problem.这是我的问题的一个简化示例。 In this example the functions.py make my picture grayscale and then show me the grayscaled picture in a separate window.在这个例子中,functions.py 使我的图片灰度化,然后在单独的窗口中显示灰度图片。 My problem is though i have 10 picture and i want my main.py work like this:我的问题是虽然我有 10 张图片,但我希望我的main.py像这样工作:

import numpy as np 
import cv2
from functions import imageinput


print(imageinput(image1.jpg))
print(imageinput(image2.jpg))
print(imageinput(image3.jpg))
print(imageinput(image4.jpg))
print(imageinput(image5.jpg))
print(imageinput(image6.jpg))
print(imageinput(image7.jpg))
print(imageinput(image8.jpg))
print(imageinput(image9.jpg))
print(imageinput(image10.jpg))

I just want all my pictures work separately with the function i described above, but i get that error message.我只是希望我的所有图片都与我上面描述的功能分开工作,但我收到了该错误消息。 How can i solve this problem or what did i write wrong?我该如何解决这个问题或者我写错了什么?

I use the following versions: Python 3.6.0 and OpenCV 4.1.1.26我使用以下版本:Python 3.6.0 和 OpenCV 4.1.1.26

将图像文件名放在引号之间。

print(imageinput('image1.jpg'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM