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.
时间: 2024-02-18 07:01:52 浏览: 141
这个错误的原因可能是因为 `img_a` 的类型不是浮点类型,需要将其转换为浮点类型后再进行超像素分割。可以使用 `img_as_float` 函数将其转换为浮点类型,代码如下:
```
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 转换为浮点类型
img_a = img_as_float(img_a)
# 对图像 A 进行超像素分割,并获取每个超像素块的像素范围
segments_a = slic(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()
```
阅读全文