You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于PyTorch的图像分割:Boat类像素统计与面积测算求助

图像分割后Boat类像素统计与实际面积测算指导

问题描述

我已经用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()

一、统计Boat类的像素数

根据你的代码逻辑,om是经过torch.argmax处理后的分割结果数组,每个元素对应像素的类别编号。从label_colors的注释可知,Boat对应的类别编号是4,只需统计数组中等于4的元素数量即可:

在现有代码末尾添加以下代码:

# 统计Boat类(类别编号4)的像素数量
boat_pixel_count = np.sum(om == 4)
print(f"Boat类像素数量: {boat_pixel_count}")

注意:你在预处理阶段对图像做了Resize(256)和CenterCrop(224)操作,上述统计的是裁剪后224×224图像中的船只像素数。如果需要统计原始图像的像素数,可选择:

  • 去掉Resize和CenterCrop步骤,直接用原始尺寸图像进行分割统计
  • 记录缩放比例,将裁剪后的像素数换算回原始图像的像素数(比如原始图像宽高为W×H,裁剪后为224×224,换算比例为(W/224)*(H/224))

二、通过像素数测算实际面积的方法

可以通过像素数估算船只的实际面积,但必须满足已知图像像素比例尺的前提:

  1. 获取像素比例尺:即明确图像中1个像素对应现实世界的实际长度(例如1像素=0.1米),获取方式包括:
    • 图像中存在已知实际尺寸的参考物体(如已知长度的码头、标准尺寸的浮标)
    • 已知拍摄参数(如无人机飞行高度、相机焦距、传感器尺寸),通过摄影测量公式计算比例尺
  2. 确保分割准确性:分割出的船只区域越精准,像素统计的误差越小,最终面积估算结果越可靠。

具体计算方式:

  • 单个像素对应的实际面积 = 比例尺²(单位:平方米/像素)
  • 船只实际面积 ≈ 船只像素数 × 比例尺²

如果没有参考物体或拍摄参数,无法直接从像素数得到真实的实际面积——不同拍摄距离、焦距下,相同实际大小的物体在图像中的像素占比差异极大。


内容的提问来源于stack exchange,提问作者AHR99

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 16:55:26