【问题标题】:Working with large (over 3000x3000) images in OpenCV, and they don't fit in my screen在 OpenCV 中处理大(超过 3000x3000)图像,它们不适合我的屏幕
【发布时间】:2016-04-16 01:20:59
【问题描述】:

我正在开发一个程序来从 python 中的大图像中剪切出人脸。但是,即使看到它们,我也遇到了问题。

我正在使用的图像可以超过 2000x2000,它们不适合我的屏幕。这是代码:

import cv2
import sys

# Get user supplied values
imagePath = sys.argv[1]
cascPath = sys.argv[2]

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.2,
    minNeighbors=5,
    minSize=(100, 100),
    flags = cv2.cv.CV_HAAR_SCALE_IMAGE
 )

print "Found {0} faces!".format(len(faces))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)


cv2.NamedWindow(name, flags=WINDOW_NORMAL)
cv2.imshow("Faces found", image)
cv2.waitKey(0)

这是我关心的部分:

cv2.NamedWindow(name, flags=WINDOW_NORMAL)
cv2.imshow("Faces found", image)
cv2.waitKey(0)

现在,opencv 文档中有 instructions 关于如何更改窗口大小,但到目前为止我一直收到错误:

错误一:

Found 2 faces!
Traceback (most recent call last):
  File "face_detect.py", line 31, in <module>
    cv2.NamedWindow(name, flags=WINDOW_NORMAL)
AttributeError: 'module' object has no attribute 'NamedWindow'

错误 2:

Found 2 faces!
Traceback (most recent call last):
  File "face_detect.py", line 31, in <module>
    cv2.namedWindow("", WINDOW_NORMAL)

NameError: name 'WINDOW_NORMAL' is not defined

错误 3:

  File "face_detect.py", line 31
    cv2.namedWindow(winname[, WINDOW_NORMAL])
                            ^
SyntaxError: invalid syntax

谁能告诉我我做错了什么?

【问题讨论】:

  • 可能值得尝试使用sudo apt-get install python-opencv 重新安装软件包... :)
  • @filaton 对一些错别字进行了彻底的修复。
  • @Suever 对! :) 但我记得有些情况下,OpenCV 在未正确安装时会抛出相同的异常,所以这就是为什么我说它可能值得。

标签: python python-2.7 opencv


【解决方案1】:

您输入了错误的cv2.NamedWindow 而不是cv2.namedWindow,请注意大小写。此外,WINDOW_NORMAL 必须是 cv2.WINDOW_NORMAL。然后你可以使用cv2.resizeWindow来设置想要的大小。

# Specify an appropriate WIDTH and HEIGHT for your machine
WIDTH = 1000
HEIGHT = 1000

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.resizeWindow('image', WIDTH, HEIGHT)

附带说明,当文档使用以下格式时

cv2.namedWindow(winname[, flags])

[] 表示flags 是一个可选 位置输入。它不是有效的 Python 语法,因此无法复制/粘贴到您的代码中。

【讨论】:

  • 它给了我:“NameError: name 'WIDTH' is not defined”。我在哪里添加宽度?
  • @David 您根据屏幕大小自行指定WIDTHHEIGHT
猜你喜欢
  • 2016-09-26
  • 1970-01-01
  • 1970-01-01
  • 2017-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
相关资源
最近更新 更多