首页 文章

Otsu Thresholding OpenCV Python

提问于
浏览
2

我正在使用图像比例和Otsu阈值处理在2个图像之间进行变化检测 . 代码的最后一行是错误的 .

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

有帮助吗?

1 回答

  • 3

    为什么?

    cv2.threshold() 期望第一个参数(源图像)是灰度图像 .

    怎么样?

    如果您不能控制初始图像生成和流水线处理,只需在输入 cv2.threshold() 之前转换 [src]

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

相关问题