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()

你可能感兴趣的:(图像处理_python,python,opencv,遍历创建)