语义分割计算classweight

# coding:utf-8
from __future__ import print_function
import os
import numpy as np
import cv2

w, h = 688, 550


def find_pic(img, array_list, n_class, pixs):
    img_sum = np.sum(img == array_list, axis=-1)
    pix_numbers = img_sum.reshape(-1).tolist().count(3)
    if pix_numbers:
        pixs += pix_numbers
        n_class += 1
    return pixs, n_class


def compute_class(pixs, n_class):
    return pixs / (n_class * w * h)


def frequence():
    images_path = r'C:\Users\Administrator\Desktop\test'
    void = np.array([0, 0, 0])
    dirt = np.array([108, 64, 20])
    sand = np.array([255, 229, 204])
    grass = np.array([0, 102, 0])
    tree = np.array([0, 255, 0])
    pole = np.array([0, 153, 153])
    water = np.array([0, 128, 255])
    sky = np.array([0, 0, 255])
    vehicle = np.array([255, 255, 0])
    container = np.array([255, 0, 127])
    asphalt = np.array([64, 64, 64])
    gravel = np.array([255, 128, 0])
    building = np.array([255, 0, 0])
    mulch = np.array([153, 76, 0])
    rock_bed = np.array([102, 102, 0])
    log = np.array([102, 0, 0])
    bicycle = np.array([0, 255, 128])
    person = np.array([204, 153, 255])
    fence = np.array([102, 0, 204])
    bush = np.array([255, 153, 204])
    sign = np.array([0, 102, 102])
    rock = np.array([153, 204, 255])
    bridge = np.array([102, 255, 255])
    concrete = np.array([101, 101, 11])
    picnic_table = np.array([114, 85, 47])

    images_list_path = [os.path.join(images_path, i) for i in os.listdir(images_path)]

    n_void = 0
    void_pixs = 0

    n_dirt = 0
    dirt_pixs = 0

    n_sand = 0
    sand_pixs = 0

    n_grass = 0
    grass_pixs = 0

    n_tree = 0
    tree_pixs = 0

    n_pole = 0
    pole_pixs = 0

    n_water = 0
    water_pixs = 0

    n_sky = 0
    sky_pixs = 0

    n_vehicle = 0
    vehicle_pixs = 0

    n_container = 0
    container_pixs = 0

    n_asphalt = 0
    asphalt_pixs = 0

    n_gravel = 0
    gravel_pixs = 0

    n_building = 0
    building_pixs = 0

    n_mulch = 0
    mulch_pixs = 0

    n_rock_bed = 0
    rock_bed_pixs = 0

    n_log = 0
    log_pixs = 0

    n_bicycle = 0
    bicycle_pixs = 0

    n_person = 0
    person_pixs = 0

    n_fence = 0
    fence_pixs = 0

    n_bush = 0
    bush_pixs = 0

    n_sign = 0
    sign_pixs = 0

    n_rock = 0
    rock_pixs = 0

    n_bridge = 0
    bridge_pixs = 0

    n_concrete = 0
    concrete_pixs = 0

    n_picnic_table = 0
    picnic_table_pixs = 0

    for count, image_path in enumerate(images_list_path):
        print('{}image'.format(count))
        img = cv2.imread(image_path)[..., ::-1]
        void_pixs, n_void = find_pic(img, void, n_void, void_pixs)
        dirt_pixs, n_dirt = find_pic(img, dirt, n_dirt, dirt_pixs)
        sand_pixs, n_sand = find_pic(img, sand, n_sand, sand_pixs)
        grass_pixs, n_grass = find_pic(img, grass, n_grass, grass_pixs)
        tree_pixs, n_tree = find_pic(img, tree, n_tree, tree_pixs)
        pole_pixs, n_pole = find_pic(img, pole, n_pole, pole_pixs)
        water_pixs, n_water = find_pic(img, water, n_water, water_pixs)
        sky_pixs, n_sky = find_pic(img, sky, n_sky, sky_pixs)
        vehicle_pixs, n_vehicle = find_pic(img, vehicle, n_vehicle, vehicle_pixs)
        container_pixs, n_container = find_pic(img, container, n_container, container_pixs)
        asphalt_pixs, n_asphalt = find_pic(img, asphalt, n_asphalt, asphalt_pixs)
        gravel_pixs, n_gravel = find_pic(img, gravel, n_gravel, gravel_pixs)
        building_pixs, n_building = find_pic(img, building, n_building, building_pixs)
        mulch_pixs, n_mulch = find_pic(img, mulch, n_mulch, mulch_pixs)
        rock_bed_pixs, n_rock_bed = find_pic(img, rock_bed, n_rock_bed, rock_bed_pixs)
        log_pixs, n_log = find_pic(img, log, n_log, log_pixs)
        bicycle_pixs, n_bicycle = find_pic(img, bicycle, n_bicycle, bicycle_pixs)
        person_pixs, n_person = find_pic(img, person, n_person, person_pixs)
        fence_pixs, n_fence = find_pic(img, fence, n_fence, fence_pixs)
        bush_pixs, n_bush = find_pic(img, bush, n_bush, bush_pixs)
        sign_pixs, n_sign = find_pic(img, sign, n_sign, sign_pixs)
        rock_pixs, n_rock = find_pic(img, rock, n_rock, rock_pixs)
        bridge_pixs, n_bridge = find_pic(img, bridge, n_bridge, bridge_pixs)
        concrete_pixs, n_concrete = find_pic(img, concrete, n_concrete, concrete_pixs)
        picnic_table_pixs, n_picnic_table = find_pic(img, picnic_table, n_picnic_table, picnic_table_pixs)

    print(void_pixs, n_void)
    print(dirt_pixs, n_dirt)
    print(sand_pixs, n_sand)
    print(grass_pixs, n_grass)
    print(tree_pixs, n_tree)
    print(pole_pixs, n_pole)
    print(water_pixs, n_water)
    print(sky_pixs, n_sky)
    print(vehicle_pixs, n_vehicle)
    print(container_pixs, n_container)
    print(asphalt_pixs, n_asphalt)
    print(gravel_pixs, n_gravel)
    print(building_pixs, n_building)
    print(mulch_pixs, n_mulch)
    print(rock_bed_pixs, n_rock_bed)
    print(log_pixs, n_log)
    print(bicycle_pixs, n_bicycle)
    print(person_pixs, n_person)
    print(fence_pixs, n_fence)
    print(bush_pixs, n_bush)
    print(sign_pixs, n_sign)
    print(rock_pixs, n_rock)
    print(bridge_pixs, n_bridge)
    print(concrete_pixs, n_concrete)
    print(picnic_table_pixs, picnic_table)

    f_class_void = compute_class(void_pixs, n_void)
    f_class_dirt = compute_class(dirt_pixs, n_dirt)
    f_class_sand = compute_class(sand_pixs, n_sand)
    f_class_grass = compute_class(grass_pixs, n_grass)
    f_class_tree = compute_class(tree_pixs, n_tree)
    f_class_pole = compute_class(pole_pixs, n_pole)
    f_class_water = compute_class(water_pixs, n_water)
    f_class_sky = compute_class(sky_pixs, n_sky)
    f_class_vehicle = compute_class(vehicle_pixs, n_vehicle)
    f_class_container = compute_class(container_pixs, n_container)
    f_class_asphalt = compute_class(asphalt_pixs, n_asphalt)
    f_class_gravel = compute_class(gravel_pixs, n_gravel)
    f_class_building = compute_class(building_pixs, n_building)
    f_class_mulch = compute_class(mulch_pixs, n_mulch)
    f_class_rock_bed = compute_class(rock_bed_pixs, n_rock_bed)
    f_class_log = compute_class(log_pixs, n_log)
    f_class_bicycle = compute_class(bicycle_pixs, n_bicycle)
    f_class_person = compute_class(person_pixs, n_person)
    f_class_fence = compute_class(fence_pixs, n_fence)
    f_class_bush = compute_class(bush_pixs, n_bush)
    f_class_sign = compute_class(sign_pixs, n_sign)
    f_class_rock = compute_class(rock_pixs, n_rock)
    f_class_bridge = compute_class(bridge_pixs, n_bridge)
    f_class_concrete = compute_class(concrete_pixs, n_concrete)
    f_class_picnic = compute_class(picnic_table_pixs, picnic_table)

    f_class = [f_class_void, f_class_dirt, f_class_sand, f_class_grass, f_class_tree, f_class_pole,
               f_class_water, f_class_sky, f_class_vehicle, f_class_container,
               f_class_asphalt, f_class_gravel, f_class_building, f_class_mulch,
               f_class_rock_bed, f_class_log, f_class_bicycle, f_class_person, f_class_fence,
               f_class_bush, f_class_sign, f_class_rock, f_class_bridge, f_class_concrete,
               f_class_picnic
               ]

    f_class_median = np.median(np.array(f_class))
    print(f_class_median)
    print(f_class_median / np.array(f_class))


if __name__ == '__main__':
    frequence()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值