'Working with large (over 3000x3000) images in OpenCV, and they don't fit in my screen

I'm developing a program to cut the faces out of large images in python. However, I'm having a problem even seeing them.

The images I'm working with can be upwards of 2000x2000, and they don't fit on my screen. This is the code:

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)

And this is the part I'm concerned with:

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

Now, the opencv documentation has instructions on how to change window size, but so far I keep getting errors:

Error 1:

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'

Error 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

Error 3:

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

Can anyone show me what I'm doing wrong?



Solution 1:[1]

You have mis-typed cv2.NamedWindow instead of cv2.namedWindow, note the case. Also, WINDOW_NORMAL needs to be cv2.WINDOW_NORMAL. Then you can use cv2.resizeWindow to set the desired size.

# 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)

As a side note, when the documentation uses the following format

cv2.namedWindow(winname[, flags])

The [] means that flags is an optional positional input. It is not valid Python syntax and therefore can't be copy/pasted into your code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1