python opencv 遍历图像创建新的图像

python opencv 遍历图像创建新的图像

代码功能:
主要是给原图,图上一层颜色,不同的类别对应不同的颜色
1、有 ori.jpg原图 和 label.png对应的灰度图,其中灰度图中像素值从 0-9不等
2、通过读取 label.png中的像素值,生成对应的彩色图
3、将生成的彩色图和原图融合

下面代码实现了,创建新的彩色图的功能。但是没有图像融合操作,因为测试这,python做这事实在太慢了,要几十秒遍历生成一张图,所以就改用c++了。
见此连接:
https://blog.csdn.net/xjtdw/article/details/99745334

import cv2
import  numpy as np

img_in = cv2.imread('C:/Users/xujun/Desktop/front_img/002_003003_102837851.png',0)

shape = img_in.shape
img_out = np.zeros((shape[0],shape[1],3),dtype=np.uint8)

print ('img_shape',shape)
print ('list_shape:',img_out.shape)
switcher = {
    0:[0, 0, 0],
    1:[165, 42, 42],
    2:[0, 192, 0],
    3:[190, 153, 153],
    4:[90, 120, 150],
    5:[102, 102, 156],
    6:[128, 64, 255],
    7:[250, 170, 160],
    8:[150, 120, 90],
    9:[244, 35, 232]
}

# print (switcher[8])
for row in range(shape[0]):
    for col in range(shape[1]):
        if img_in[row, col] == 0:
            img_out[row, col] = switcher[0]
        if img_in[row, col] == 1:
            img_out[row, col] = switcher[1]
        if img_in[row, col] == 2:
            img_out[row, col] = switcher[2]
        if img_in[row, col] == 3:
            img_out[row, col] = switcher[3]
        if img_in[row, col] == 4:
            img_out[row, col] = switcher[4]
        if img_in[row, col] == 5:
            img_out[row, col] = switcher[5]
        if img_in[row, col] == 6:
            img_out[row, col] = switcher[6]
        if img_in[row, col] == 7:
            img_out[row, row] = switcher[7]
        if img_in[row, col] == 8:
            img_out[row, col] = switcher[8]
        if img_in[row, col] == 9:
            img_out[row, col] = switcher[9]

# img_out = cv2.cvtColor(img_out,)
# cv2.imshow('img',img_out)
cv2.imwrite('img1.png',img_out)
# cv2.waitKey()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值