-
问题描述
opencv库能够读取4通道的tif图像,但是对于高通道tif图像,超过4个通道,opencv就会报错,所以使用skimage库来处理此类tif图像
-
读取图像并裁剪
from skimage import transform,io pixSize = 10 # 切割图像的大小(长宽),切割图像是正边形 filePath = './img/dim10.tif' DataSavePath = './train/positive/' # 图片加载 im = io.imread(filePath) sp = im.shape height = int(sp[0]/pixSize) # 竖着裁剪图片数 width = int(sp[1]/pixSize) # 横着裁剪图片数 iter = 1 for i in range(height): # 高,纵向 for j in range(width): # 宽,横向 print("processing img No "+str(iter)+" image") ig = im[i*pixSize:(i+1)*pixSize, j*pixSize:(j+1)*pixSize] # 滑动窗口 io.imsave(DataSavePath + str(iter) + '.tif',ig) # 滑动窗口到的地方保存图片 iter += 1
-
将高通道tif图像显示出来,例如10通道tif图
原理:将其转为RGB图像再用opencv显示
from skimage import transform,io import numpy as np import cv2 im = io.imread('./train/negative/30.tif') sp = im.shape im_band = sp[2] # 得到通道数 img = np.array(im) # 转为数组 img = np.transpose(img,(2,0,1)) # 将通道数放在前面,然后是高和宽 # 影像转换处理 def img_processing(im_band,img_data): if im_band == 1: # 单通道图 data_jpg = np.zeros((img_data.shape[0],img_data.shape[1]),dtype='uint8') im_max = np.amax(img_data) im_min = np.amin(img_data) for m in range(0, img_data.shape[0]): for n in range(0, img_data.shape[1]): data_jpg[m,n] = float(255./(im_max-im_min))*(img_data[m,n]-im_min) else: # 多通道图 data_jpg = np.zeros((img_data.shape[1], img_data.shape[2],3),dtype='uint8') for i in range(3): im_max = np.amax(img_data[i,:,:]) im_min = np.amin(img_data[i,:,:]) for m in range(0, img_data.shape[1]): for n in range(0, img_data.shape[2]): data_jpg[m,n,i] = float(255./(im_max-im_min))*(img_data[i,m,n]-im_min) return data_jpg data_jpg = img_processing(im_band,img) # data_jpg = cv2.resize(data_jpg,(224,224)) # 修改图片大小,可注释掉 cv2.imshow('image',data_jpg) cv2.waitKey()
python环境下skimage处理高通道tif图片(10通道)
最新推荐文章于 2024-08-21 17:48:56 发布