img = imread(r'I:\\18Breakageratecalculation\\mask-slic use\\maskSLIC-master\\1\\056.jpg') # The ROI is also stored as an image for viewing convenience # But the roi input input maskSLIC should be a binary image with the same spatial # Dimensions as the image (in this case 300x451) roi = imread(r'I:\\18Breakageratecalculation\\mask-slic use\\maskSLIC-master\\1\\0562.png') # The alpha channel is used to store the ROI in this case and is converted into a logical array of 0s and 1s roi = roi[:, :, 3] > 0 # Alternatively a mask could be created manually with for example a disk: # roi = np.zeros((img.shape[0], img.shape[1])) # a, b = 150, 150 # r = 100 # y,x = np.ogrid[-a:img.shape[0]-a, -b:img.shape[1]-b] # mask = x*x + y*y <= r*r # roi[mask] = 1 # ~~~~~~~~~~~~ Example 1: maskSLIC ~~~~~~~~~~~~~ t1 = time.time() # Note that compactness is defined differently because a grid is not used. Lower compactness for maskSLIC is equivalent segments = seg.slic(img, compactness=10, seed_type='nplace', mask=roi, n_segments=120, recompute_seeds=True, plot_examples=True, enforce_connectivity=True) print("Time: {:.2f} s".format(time.time() - t1)) plt.figure() plt.imshow(mark_boundaries(img, segments)) plt.contour(roi, contours=1, colors='red', linewidths=0.5) plt.axis('off') # ~~~~~~~~~~~ Example 2: SLIC ~~~~~~~~~~~~~~~~~ t1 = time.time() segments = seg.slic(img, compactness=10, seed_type='grid', n_segments=100, plot_examples=False, enforce_connectivity=True) # segments[roi==0] = -1 print("Time: {:.2f} s".format(time.time() - t1)) plt.figure() plt.imshow(mark_boundaries(img, segments)) plt.contour(roi, contours=1, colors='red', linewidths=1) plt.axis('off') plt.show() plt.show()怎么保存结果
时间: 2024-03-07 13:49:22 浏览: 170
如果想要保存分割结果,可以使用matplotlib库中的imsave函数来保存图像。
例如,可以在代码中添加以下语句来保存Example 1中的分割结果:
```
plt.imshow(mark_boundaries(img, segments))
plt.contour(roi, contours=1, colors='red', linewidths=0.5)
plt.axis('off')
plt.savefig('example1.png') # 保存图片
plt.show()
```
这样就会在当前路径下生成一个名为example1.png的图片文件,其中包含分割结果和ROI边界。需要保存Example 2的分割结果时,也可以类似地添加保存语句。
相关问题
class_labels = { 0: r'I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\result6\slic_pellucid\non-broken roofing', 1: r'I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\result6\slic_pellucid\broken roofing', 2: r'I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\result6\slic_pellucid\courtyard' }计算类被为1的像素点/(类别为+类别为的像素点)
根据您提供的代码,我可以看出这是一个Python字典类别标签和它们对应的路径。要计算类别为1的像素点占总像素点的比例,需要知道类别为1的像素点数和所有类别为1或类别为2的像素点数。代码如下:
```
import cv2
import numpy as np
# 读取图像
img = cv2.imread('your_image_path', cv2.IMREAD_GRAYSCALE)
# 类别为1的像素点数
class_1_pixels = np.sum(img == 1)
# 类别为1或类别为2的像素点数
total_pixels = np.sum(np.isin(img, [1, 2]))
# 计算比例
ratio = class_1_pixels / total_pixels
print('类别1像素点占比:', ratio)
```
请将 `your_image_path` 替换为您的图像路径。
图像放在”I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\result6\slic“路径里
如果你的子块图像在路径 "I:\18Breakageratecalculation\mask-slic use\maskSLIC-master\result\result6\slic" 中,你可以使用以下代码来自动合并多个子块图像:
```python
import cv2
import numpy as np
import os
# 读取子块图像
imgs = []
for i in range(1, 92):
filename = os.path.join("I:/18Breakageratecalculation/mask-slic use/maskSLIC-master/result/result6/slic", '{:03d}.png'.format(i))
img = cv2.imread(filename)
imgs.append(img)
# 合并子块图像
merged_img = np.concatenate(imgs, axis=0)
# 保存合并后的图像
cv2.imwrite('merged_image.png', merged_img)
```
这个代码示例使用一个 for 循环来读取每个子块图像,并将它们添加到一个列表中。我们使用了 os.path.join 函数来生成每个子块图像的完整路径。最后,我们使用 numpy 的 `concatenate` 函数将所有子块图像合并成一个完整的图像。请根据您的具体情况进行修改。
阅读全文
相关推荐

















