gpt4 book ai didi

python - 在 OpenCV 中处理大型(超过 3000x3000)图像,但它们不适合我的屏幕

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:09 27 4
gpt4 key购买 nike

我正在开发一个程序,用 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

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

最佳答案

您输入了错误的 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 语法,因此无法复制/粘贴到您的代码中。

关于python - 在 OpenCV 中处理大型(超过 3000x3000)图像,但它们不适合我的屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659149/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com