[英]screen capture and syntax error using cv2.imread
I'm new to python and OPENCV and I need to write a python code that reads an image. 我是python和OPENCV的新手,我需要编写一个读取图像的python代码。 I used some examples I've found online and I get some strange results.
我使用了一些在网上找到的示例,但得到了一些奇怪的结果。
Im running ubuntu12.04 and opencv2.4.9 我正在运行ubuntu12.04和opencv2.4.9
First, when I run the file my mouse turn to cross wait for my input (4 clicks). 首先,当我运行文件时,我的鼠标转向交叉等待我的输入(4次单击)。
Second, I get a syntax massage that I can't find what is wrong in my code. 其次,我得到了一个语法提示,我找不到代码中的错误。
And the last one, when I look in the folder of the py file I see 2 new files: 最后一个,当我查看py文件的文件夹时,看到2个新文件:
"cv2" -A screenshot of my first click and "np" - the other 3 screenshots (the other 3 clicks) “ cv2”-我的第一次点击的屏幕截图,“ np”-其他3个屏幕截图(其他3次点击)
Here is the code: 这是代码:
import cv2
import numpy as np
img = cv2.imread('clutter.jpg')
Yellow_MIN = np.array([20, 50, 45],np.uint8)
Yellow_MAX = np.array([30, 255, 255],np.uint8)
hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
frame_threshed = cv2.inRange(hsv_img, Yellow_MIN, Yellow_MAX)
cv2.imwrite('output2.jpg', frame_threshed)
--end of code-- -代码结尾-
The syntax error is : 语法错误是 :
./color_find_et.py: line 9: syntax error near unexpected token `('
./color_find_et.py: line 9: `img = cv2.imread('clutter.jpg')'
You need to add the Flags specifying the color type of a loaded image 您需要添加标志以指定加载图像的颜色类型
Use the function cv2.imread() to read an image.
使用函数cv2.imread()读取图像。 The image should be in the working directory or a full path of image should be given.
该映像应位于工作目录中,或者应提供完整的映像路径。
Second argument is a flag which specifies the way image should be read.
第二个参数是一个标志,用于指定应读取图像的方式。
cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be neglected. It is the default flag. cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
Note
注意
Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
除了这三个标志,您可以分别简单地传递整数1、0或-1。
specifically your code needs to be: 具体来说,您的代码需要为:
img = cv2.imread('clutter.jpg', 0)
Instead of writing "import cv2" you have to write 不必编写“ import cv2”,而必须编写
"from cv2 import * " “从cv2导入*”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.