报错 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)