2

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?

1 Answer 1

3

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
               )
Sign up to request clarification or add additional context in comments.

3 Comments

The problem is that I am trying to threshold the array ratio,because that is what I want to do,but cv2.threshold() wants a grayscale image,like you mentioned.I am being incorrect in using cv2.threshold() this way
make it np.array( ( 300, 200 ), dtype = np.int8 ) or run it through cv2.convertScaleAbs( src )
cv2.convertScaleAbs(src) worked.The idea was to make the datatype of ratio to np.int8,right? Because when openCV reads an image through cv2.imread(),the datatype is np.uint8 also

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.