-
图像读取
- 使用skimage img = io.imread(img_path)
- np.ndarray, (0, 255), RGB
- 使用opencv img = cv2.imread(img_path)
- np.ndarray, (0, 255), BGR
- 使用PIL img = Image.open(img_path)
- PIL.Image.Image对象 (H * W * C), (0, 255), RGB
- 使用skimage img = io.imread(img_path)
-
图像缩放
- 一般说的102*128指:高102,宽128
- 使用skimage(from skimage import transform) img = transform.resize(img, (height, width))
- 使用opencv img = cv2.resize(img, (width, height))
- 使用PIL img =img.resize((width, height), Image.ANTIALIAS)
- 使用torch (from torchvision import transforms)
- resize = transforms.Resize([height, width]) img = resize(img)
-
图像格式转换
- torchvision
- 可以把张量或ndarray转为PIL图像
- 可以将PIL图像或ndarray图像转为张量
-
小操作
- 图像转为ndarray之后可以使用切片进行裁减或者替换对应位置的像素点
- img = np.array(img, dtype=np.uint8)
img = img[26:-2, 11:-9]
- img = np.array(img, dtype=np.uint8)
- 对图像进行增加一个通道的处理,类似于tensor, numpy 的操作,此处引入了新的张量转换包einops
- mask_img = np.expand_dims(mask_img, axis=2)
- img = einops.rearrange(img,“w h -> w h 1”)
- input_tensor = torch.unsqueeze(img_tensor, dim=0) # 增加一个batch维度
- 图像去除维度1
- img = np.squeeze(img)
- 图像维度变换,使用einops包
- img = einops.rearrange(img,“h w c -> w h c”)
- 给图像上下左右填充像素点,以其外侧边缘的像素类型进行填充
- top_size, bottom_size, left_size, right_size = (24, 0, 12, 12)
img = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REPLICATE)
- top_size, bottom_size, left_size, right_size = (24, 0, 12, 12)
- 设定阈值,把图像grayscale_cam变为二值化图像frame_cam
- frame_cam = np.where(grayscale_cam > 0.5, 1.0, 0.0)
- 图像转为ndarray之后可以使用切片进行裁减或者替换对应位置的像素点
python图像处理实验中记录(包括图像读取、图像缩放、图像格式转换等)
最新推荐文章于 2023-04-12 18:42:03 发布