THRESH_OTSU mode: ‘src_type == CV_8UC1 || src_type == CV_16UC1‘ where ‘src_type‘ is 6 (CV_

问题:THRESH_OTSU mode: ‘src_type == CV_8UC1 || src_type == CV_16UC1’ where ‘src_type’ is 6(CV_64FC1)

def readTif(src):
    dataset = gdal.Open(src)
    if dataset ==None:
        print(src+"文件无法打开")
    im_width =dataset.RasterXSize
    im_height = dataset.RasterYSize
    im_bands = dataset.RasterCount
    im_geotrans = dataset.GetGeoTransform()
    im_pro = dataset.GetProjection()
    im_data = dataset.ReadAsArray(0,0,im_width,im_height)
    return im_data,im_width,im_height,im_bands,im_geotrans,im_pro
    im_data,im_width,im_height,im_bands,im_geotrans,im_pro =readTif(path)
    
# img_ = cv2.imread(path,0) #灰度图输出
if im_bands > 3:
    raise str("通道数过多无法转换")
elif im_bands == 3:
    img = im_data[0,:,:] * 0.8 +im_data[1,:,:]*0.1 + im_data[2,:,:]*0.1
    # SaveRaster(outPath, img, im_width, im_height, im_pro, im_geotrans,  band=1)

blur = cv2.GaussianBlur(im_data, (5,5), 0)    # 高斯滤波
ret1, th1 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)  #方法选择为THRESH_OTSU

在这里插入图片描述

原因

从异常中发现,可能是数据的类型不一致导致的,分析算法原理

在这里插入图片描述
算法计算灰度级时,按照整数统计,且在0-255之间,但读取的数据为float类型,最终导致错误。

解决方案

img =img.astype("uint8")
ret1, th1 = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)  #方法选择为THRESH_OTSU
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值