遥感影像变化检测--标注实现

变化检测结果标注

标注结果如下:其中红色表示错检,蓝色表示漏检
色彩标注示例

实现代码如下:

Predict = '图片路径'
Rootdict = '根路径'


###############################################################################
# 基础函数
###############################################################################

def load_img_name_list(dataset_path):
    img_name_list = np.loadtxt(dataset_path, dtype=np.str_)
    if img_name_list.ndim == 2:
        return img_name_list[:, 0]
    return img_name_list


def get_img_path(root_dir, img_name):
    return os.path.join(root_dir, 'label', img_name)


def get_changeimg_path(root_dir, img_name):
    return os.path.join(root_dir, Predict, img_name)


def color_label(img1, img2):
    w, h, _ = img1.shape
    # 需要重新赋值,因为图片只读
    img = np.array(img2)

    fp = np.array([255, 255, 255])
    fn = np.array([1, 1, 1])
    for i in range(0, w):
        for j in range(0, h):
            p1 = img1[i][j]
            p2 = img2[i][j]
            # false positive,根据自己需求修改对应颜色
            if ((p2 - p1) == fp).all():
                img[i][j] = [255, 0, 0]
            # false negative
            if ((p2 - p1) == fn).all():
                img[i][j] = [0, 0, 255]
    return img


def save_image(image_numpy, image_path):
    image_pil = Image.fromarray(np.array(image_numpy, dtype=np.uint8))
    image_pil.save(image_path)

###############################################################################
# 颜色标注变化区域
###############################################################################

def change_pics():
    root_dir = Rootdict
    list_path = os.path.join(root_dir, 'list', 'demo.txt')
    save_path = os.path.join(root_dir, 'color_label', Predict)
    os.makedirs(save_path, exist_ok=True)
    img_name_list = load_img_name_list(list_path)
    size = len(img_name_list)
    for index in range(0, size):
        name = img_name_list[index]
        print('process:' + name)
        A_path = get_img_path(root_dir, img_name_list[index % size])
        B_path = get_changeimg_path(root_dir, img_name_list[index % size])
        a = Image.open(A_path)
        b = Image.open(B_path)

        #  灰度值转rgb
        img = np.asarray(a.convert('RGB'))
        img_B = np.asarray(b.convert('RGB'))

        #  颜色转化
        color_img = color_label(img, img_B)
        filename = os.path.join(save_path, name.replace('.jpg', '.png'))
        
        # 图片保存
        save_image(color_img, filename)

if __name__ == '__main__':
    change_pics()
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值