gpt4 book ai didi

python - Otsu 阈值化 OpenCV Python

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:45 25 4
gpt4 key购买 nike

我正在使用图像比率和 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

有什么帮助吗?

最佳答案

为什么?

cv2.threshold() 期望第一个参数(a source image)是灰度图像。

如何?

如果您无法控制初始图像生成和管道处理,只需在输入 cv2.threshold() 之前转换 [src]

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

关于python - Otsu 阈值化 OpenCV Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32350505/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com