简体   繁体   English

使用cv2.imread的屏幕截图和语法错误

[英]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: 这是代码:

!/usr/bin/python !/ usr / bin / python

import cv2
import numpy as np

Read image 阅读图片

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.

相关问题 使用 cv2.imread() 时出现 Python openCV 错误 - Python openCV error while using cv2.imread() 无法使用cv2.imread()读取图像 - Cannot read image using cv2.imread() cv2.imread 错误:img = cv2.imread(0) 系统错误:<built-in function imread> 返回 NULL 没有设置错误</built-in> - cv2.imread error:img = cv2.imread(0) SystemError: <built-in function imread> returned NULL without setting an error 使用 cv2.imread:“<built-in function imread> 没有设置错误就返回NULL”,好像无法打开图片或获取数据</built-in> - Using cv2.imread: “<built-in function imread> returned NULL without setting an error”, as if it can't open the picture or get the data 在 Django views.py 中使用 cv2.imread() 读取图像时出错 - Error while reading image using cv2.imread() in Django views.py 使用 cv2.imread() 在 Jupyter 笔记本上上传图像时出错 function - Error while uploading an image on Jupyter notebook using cv2.imread() function 找不到cv2.imread标志 - cv2.imread flags not found CV2.imread() Python 图像未加载且未显示错误 - CV2.imread() Python image not loading and no error displayed cv2.imread阻塞了流量 - cv2.imread is blocking flow 为什么使用 django python 时 cv2.imread 不返回任何结果? - Why cv2.imread returns none when using django python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM