whatdoisay
whatdoisay

Reputation: 325

Otsu Thresholding OpenCV Python

I am performing change detection between 2 images using image ratioing and Otsu thresholding. The last line of the code is giving error.

import cv2
import numpy as np

image1 = cv2.imread( 'E:\\alada.jpg',  0 )
image2 = cv2.imread( 'E:\\alada2.jpg', 0 )
dest   = 'E:\\Ratio\\'
print image1.shape

image1 = cv2.resize( image1, ( 300, 200 ) )
image2 = cv2.resize( image2, ( 300, 200 ) )
img1   = image1.ravel()
img2   = image2.ravel()
sd1    = np.std( img1 )
sd2    = np.std( img2 )
img2   = ( ( img2 - np.mean( img2 ) ) * sd1 / sd2 ) + np.mean( img1 )
ratio  = img1 / img2
ratio  = np.arctan(  ratio ) - np.pi / 4
ratio  = np.reshape( ratio, ( image1.shape[0], image1.shape[1] ) )
print ratio.shape

thresh, th2 = cv2.threshold( ratio, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU )


error: ..\..\..\..\opencv\modules\imgproc\src\thresh.cpp:718: error: (-215) 
src.type() == CV_8UC1 in function cv::threshold

Any help?

Upvotes: 2

Views: 4974

Answers (1)

user3666197
user3666197

Reputation: 1

Why?

cv2.threshold() expects the first argument ( a source image) to be a grayscale image.

How?

In case you are not in a control of initial image production & pipeline processing, just convert the [src] before entering the cv2.threshold()

cv2.threshold( cv2.cvtColor( aSrcIMG, cv.CV_BGR2GRAY ), #<-ratio
               aThresholdVALUE,                         #<-    0
               aMaxVALUE,                               #<-  255
               aThresholdProcessTYPE                    #<-cv2.THRESH_BINARY + cv2.THRESH_OTSU
               )

Upvotes: 3

Related Questions