2

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.

2
  • my_im.astype(np.uint8) Commented Jun 3, 2014 at 15:10
  • Thank you this also works. I tested this and Ryan's answer below. Commented Jun 3, 2014 at 15:22

1 Answer 1

3

Converting enhanced_im to the appropriate type for threshold will make it work. That can be done with:

enhanced_im  = numpy.array(enhanced_im, numpy.uint8)

Here is your code, modified to work:

import cv2
from scipy.signal import wiener
import numpy
#---------------------------------------------------------------------
# functions
#---------------------------------------------------------------------
def enhance_image(input_image):
    my_im = input_image.copy()
    my_im = wiener(my_im)
    return my_im

#---------------------------------------------------------------------
# Main
#---------------------------------------------------------------------
filename="/home/ryan/OpenCV/opencv-2.3.4.7/samples/cpp/baboon.jpg"

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)
#print type(enhanced_im)
#enhanced_im.astype(numpy.uint8)
enhanced_im  = numpy.array(enhanced_im, numpy.uint8)
#print enhanced_im
# this fails
#ret,thresh = cv2.threshold(imgray,self.t1,225, cv2.THRESH_BINARY)
thresh, bw_im = cv2.threshold(enhanced_im, 128, 255, cv2.THRESH_BINARY)
cv2.imshow('Black and White',bw_im)

key = cv2.waitKey()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.