cv2.rectangle报错TypeError: an integer is required (got type tuple)

本文介绍如何使用GDAL读取地理空间数据,并利用OpenCV进行图像处理。具体包括读取栅格数据、获取图像属性、转换颜色通道及绘制矩形等操作。针对内存占用问题提出了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

报错 TypeError: an integer is required (got type tuple)

 exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
TypeError: an integer is required (got type tuple)
    im_width = image_data.RasterXSize  # 栅格矩阵的列数
    im_height = image_data.RasterYSize  # 栅格矩阵的行数
    im_bands = image_data.RasterCount  # 波段数
    band1 = image_data.GetRasterBand(1)
    print(band1)
    print('Band Type=', gdal.GetDataTypeName(band1.DataType))
    im_data = image_data.ReadAsArray(0, 0, im_width, im_height)  # 获取数据
    im_geotrans = image_data.GetGeoTransform()  # 获取仿射矩阵信息
    im_proj = image_data.GetProjection()  # 获取投影信息
    dtype = im_data.dtype
    rgb_image = im_data[:3, :, :]  # (3, 3035,2723)
    rgb_image = rgb_image.transpose((1, 2, 0))  # (3035,2723,3)
    del image_data
    bgr_image = rgb_image[:, :, ::-1].astype(np.uint8)  # bgr
    cv2.rectangle(bgr_image , (23, 45), (34, 56), (0, 0, 255), 1)  # TypeError: an integer is required (got type tuple)
    ***bgr_image = bgr_image.copy()***  #   必须copy才行
    cv2.rectangle(bgr_image , (23, 45), (34, 56), (0, 0, 255), 1)
    del rgb_image

上面的操作,会占用过多的内存, 用np.stack((im_b, im_g, im_r), axis=2)进行操作

	im_data = image_data.ReadAsArray(0, 0, im_width, im_height)  # 获取数据
    dtype = im_data.dtype
    im_geotrans = image_data.GetGeoTransform()  # 获取仿射矩阵信息
    im_proj = image_data.GetProjection()  # 获取投影信息
    im_r = im_data[0, 0:im_height, 0:im_width]  # 获取蓝波段
    im_g = im_data[1, 0:im_height, 0:im_width]  # 获取绿波段
    im_b = im_data[2, 0:im_height, 0:im_width]  # 获取红波段
    # im_nir = im_data[3, 0:im_height, 0:im_width]  # 获取近红外波段
    # bgr_img = np.stack((im_b, im_g, im_r), axis=2)
    bgr_img = np.stack(
        (im_data[2, 0:im_height, 0:im_width],
         im_data[1, 0:im_height, 0:im_width],
         im_data[0, 0:im_height, 0:im_width]),
        axis=2)
    cv2.rectangle(bgr_img, (23, 45), (34, 56), (0, 0, 255), 1)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值