pytorch 图像分割与面积测量

wi3ka0sx  于 2022-12-29  发布在  其他
关注(0)|答案(1)|浏览(211)

我已经使用PyTorch对图像进行了图像分割。我正在尝试获取Boat类的像素计数来测量面积。作为图像中的一个示例,我想获取像素计数来测量船。我该如何做到这一点?从像素计数是否可以测量船的面积?
我很困惑,正试图找到一种方法。如果有人能指导我,我将不胜感激。

    • 编码如下:**
from torchvision import models
fcn = models.segmentation.fcn_resnet101(pretrained=True).eval()

from PIL import Image
import matplotlib.pyplot as plt
import torch

img = Image.open('boat.jpg') 
plt.imshow(img)
plt.show()

# Apply the transformations needed
#Resize the image to (256 x 256)
#CenterCrop it to (224 x 224)

import torchvision.transforms as T
trf = T.Compose([T.Resize(256),
                 T.CenterCrop(224),
                 T.ToTensor(), 
                 T.Normalize(mean = [0.485, 0.456, 0.406], 
                             std = [0.229, 0.224, 0.225])])
inp = trf(img).unsqueeze(0)

out = fcn(inp)['out']
print (out.shape)

#now this 21 channeled output into a 2D image or a 1 channeled image, where each pixel of that image corresponds to a class.

import numpy as np
om = torch.argmax(out.squeeze(), dim=0).detach().cpu().numpy()
print (om.shape)
print (np.unique(om))

# Define the helper function
def decode_segmap(image, nc=21):
  
  label_colors = np.array([(0, 0, 0),  # 0=background
               # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
               (128, 0, 0), (0, 128, 0), (128, 128, 0), (0, 0, 128), (128, 0, 128),
               # 6=bus, 7=car, 8=cat, 9=chair, 10=cow
               (0, 128, 128), (128, 128, 128), (64, 0, 0), (192, 0, 0), (64, 128, 0),
               # 11=dining table, 12=dog, 13=horse, 14=motorbike, 15=person
               (192, 128, 0), (64, 0, 128), (192, 0, 128), (64, 128, 128), (192, 128, 128),
               # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
               (0, 64, 0), (128, 64, 0), (0, 192, 0), (128, 192, 0), (0, 64, 128)])

  r = np.zeros_like(image).astype(np.uint8)
  g = np.zeros_like(image).astype(np.uint8)
  b = np.zeros_like(image).astype(np.uint8)
  
  for l in range(0, nc):
    idx = image == l
    r[idx] = label_colors[l, 0]
    g[idx] = label_colors[l, 1]
    b[idx] = label_colors[l, 2]
    
  rgb = np.stack([r, g, b], axis=2)
  return rgb

rgb = decode_segmap(om)
plt.imshow(rgb); plt.show()

我想找些指导

carvr3hs

carvr3hs1#

您正在寻找skimage.measure.regionprops,一旦您有了预测的标签Map(代码中的om),您就可以对其应用regionprops并获得每个区域的area

相关问题