align_corners=True

对应

# x_ori is the coordinate in original image #原始图像坐标

x_up is the coordinate in the upsampled image:上采样图的坐标

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
import cv2 import numpy as np import torch import torch.nn.functional as F from skimage.segmentation import slic import matplotlib.pyplot as plt from skimage.segmentation import mark_boundaries from skimage import img_as_float # 定义超像素数量 num_segments = 100 # 加载图像 A 和 B img_a = cv2.imread('img_a.jpg') img_b = cv2.imread('img_b.jpg') # 对图像 A 进行超像素分割,并获取每个超像素块的像素范围 segments_a = slic(img_as_float(img_a), n_segments=num_segments, sigma=5) pixel_ranges = [] for i in range(num_segments): mask = (segments_a == i) indices = np.where(mask)[1] pixel_range = (np.min(indices), np.max(indices)) pixel_ranges.append(pixel_range) # 将像素范围应用到图像 B 上实现超像素分割 segments_b = np.zeros_like(segments_a) for i in range(num_segments): pixel_range = pixel_ranges[i] segment_b = img_b[:, pixel_range[0]:pixel_range[1], :] segment_b = torch.from_numpy(segment_b.transpose(2, 0, 1)).unsqueeze(0).float() segment_b = F.interpolate(segment_b, size=(img_b.shape[0], pixel_range[1] - pixel_range[0]), mode='bilinear', align_corners=True) segment_b = segment_b.squeeze(0).numpy().transpose(1, 2, 0).astype(np.uint8) gray = cv2.cvtColor(segment_b, cv2.COLOR_BGR2GRAY) _, mask = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY) segments_b[np.where(mask)] = i # 可视化超像素分割结果 fig = plt.figure('Superpixels') ax = fig.add_subplot(1, 2, 1) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_a, cv2.COLOR_BGR2RGB)), segments_a)) ax = fig.add_subplot(1, 2, 2) ax.imshow(mark_boundaries(img_as_float(cv2.cvtColor(img_b, cv2.COLOR_BGR2RGB)), segments_b)) plt.axis("off") plt.show(),上述代码中segments_a = slic(img_as_float(img_a), n_segments=num_segments, sigma=5)出现错误:ValueError: Cannot convert from object to float64.
05-30
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值