I've created a simple example of what I'm trying to do:
#!/usr/bin/python
#---------------------------------------------------------------------
# imports
# >>> cv2.__version__
# '2.4.6.1'
# >>> scipy.__version__
# '0.12.0'
#---------------------------------------------------------------------
import cv2
from scipy.signal import wiener
#---------------------------------------------------------------------
# functions
#---------------------------------------------------------------------
def enhance_image(input_image):
my_im = input_image.copy()
my_im = wiener(my_im)
return my_im
#---------------------------------------------------------------------
# Main
#---------------------------------------------------------------------
filename="./data/5b2013d0-7939-48a6-94eb-045495b85343.png"
im=cv2.imread(filename)
cv2.imshow('Original',im)
gray_im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow('Gray',gray_im)
enhanced_im=enhance_image(gray_im)
cv2.imshow('Enhanced',enhanced_im)
# this fails
(thresh, bw_im) = cv2.threshold(enhanced_im, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)
cv2.imshow('Black and White',bw_im)
key = cv2.waitKey()
The code loads an image, converts to it gray scale, uses the scipy.signal module to run the wiener algorithm to enhance the image, after that I would like to convert the image to a binary image (black and white). When I run the code above I get the following error:
OpenCV Error: Assertion failed (src.type() == CV_8UC1) in threshold, file /home/mrichey/Downloads/opencv2.4/opencv-2.4.6.1/modules/imgproc/src/thresh.cpp, line 719 Traceback (most recent call last): File "./simple_example.py", line 37, in (thresh, bw_im) = cv2.threshold(enhanced_im, 128, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) cv2.error: /home/mrichey/Downloads/opencv2.4/opencv->2.4.6.1/modules/imgproc/src/thresh.cpp:719: error: (-215) src.type() == CV_8UC1 in function threshold
My research into this error leads me to believe that the image being passed into the threshold function is of the wrong type, but I do not know what it needs to be converted into or how to do that conversion.
When I pass in gray_im to threshold it works correctly. I noticed that gray_im is all integers but enhanced_im has floating point values so I tried modifying my enhance_image function to:
def enhance_image(input_image):
my_im = input_image.copy()
my_im = wiener(my_im)
return my_im.astype(int)
However, I got the same error.