mbeyeler / opencv-python-blueprints

M. Beyeler (2015). OpenCV with Python Blueprints: Design and develop advanced computer vision projects using OpenCV with Python, Packt Publishing Ltd., ISBN 978-178528269-0.
GNU General Public License v3.0
291 stars 184 forks source link

Chapter 1, filters.py OpenCV error #22

Closed aaronjolson closed 5 years ago

aaronjolson commented 6 years ago

I am trying to run the cartoonify code

import cv2
import numpy as np

numDownSamples = 2       # number of downscaling steps
numBilateralFilters = 7  # number of bilateral filtering steps

img_rgb = cv2.imread("testimg.jpg")

# -- STEP 1 --
# downsample image using Gaussian pyramid
img_color = img_rgb
for _ in range(numDownSamples):
    img_color = cv2.pyrDown(img_color)

# repeatedly apply small bilateral filter instead of applying
# one large filter
for _ in range(numBilateralFilters):
    img_color = cv2.bilateralFilter(img_color, 9, 9, 7)

# upsample image to original size
for _ in range(numDownSamples):
    img_color = cv2.pyrUp(img_color)

# -- STEPS 2 and 3 --
# convert to grayscale and apply median blur
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
img_blur = cv2.medianBlur(img_gray, 7)

# -- STEP 4 --
# detect and enhance edges
img_edge = cv2.adaptiveThreshold(img_blur, 255,
                                    cv2.ADAPTIVE_THRESH_MEAN_C,
                                    cv2.THRESH_BINARY, 9, 2)

# -- STEP 5 --
# convert back to color so that it can be bit-ANDed with color image
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
toon = cv2.bitwise_and(img_color, img_edge)
cv2.imwrite('cartoon.jpg', toon)

When I run it however I get the following error

OpenCV Error: Sizes of input arguments do not match (The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array') in cv::binary_op, file C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp, line 225
Traceback (most recent call last):
  File "cartoonify.py", line 38, in <module>
    toon = cv2.bitwise_and(img_color, img_edge)
cv2.error: C:\projects\opencv-python\opencv\modules\core\src\arithm.cpp:225: error: (-209) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function cv::binary_op

I am running python 3.6 numpy==1.14.2 opencv-python==3.4.0.12 Pillow==5.1.0

Any ideas on how I can get this working?

mbeyeler commented 6 years ago

I think the problem lies with pyrDown and pyrUp: If img_rgb has some odd number of pixels, then the resulting image img_color might have a different shape after Step 1.

Try adding a line right before # -- STEPS 2 and 3 --:

# Make sure blurred image has same dim as input:
img_color = cv2.resize(img_color, img_rgb.shape[:2])

That should fix the problem.

Best, Michael

gastondg commented 5 years ago

Hello, I've had the same problem, even with that line added to the script. Could you make ir run without issues?

mbeyeler commented 5 years ago

What happens when you print(img_color.shape, img_edge.shape)? Do they have the same size?