【问题标题】:matlab colormap in pythonpython中的matlab颜色图
【发布时间】:2018-04-27 17:58:35
【问题描述】:

我再次需要原始图像,但它返回的灰度 我的老师一开始就说做colormap,并说它发生在matlab中。所以我也想在python中做类似的任务 这是我的代码看看 请帮忙

加密:

import numpy as np
from PIL import Image
x = Image.open('1.jpg', 'r')
x = x.convert('L')

y = np.asarray(x.getdata(), dtype=np.int).reshape((x.size[1], x.size[0]))#changed image to matrix getdata for matrix value                                                              # dtype is int type reshape used to break  1d array into 2d array
y = np.asarray(y, dtype=np.uint8)#if values still in range 0-255!
#print(y)
z = y
w = Image.fromarray(y, mode='L')
w.save('grey_scale.bmp')
for i in range(len(z)):
    for j in range(len(z[i])):
        a = z[i][j]
        p = int(bin(a)[2:])
        p = '%08d' % p
        p = p[::-1]
        z[i][j] = int(p, 2)
#print(z)

C = Image.fromarray(z)
C.save('decryption.bmp')
print("DONE")

解密:

import numpy as np
from PIL import Image
x = Image.open('decryption.bmp', 'r')

y = np.asarray(x.getdata(), dtype=np.int).reshape((x.size[1], x.size[0]))#changed image to matrix getdata for matrix value
#print(y)                                                             # dtype is int type reshape used to break  1d array into 2d array
y = np.asarray(y, dtype=np.uint8)#if values still in range 0-255!
#print(y)
z = y
for i in range(len(z)):
    for j in range(len(z[i])):
        a = z[i][j]
        p = int(bin(a)[2:])
        p = '%08d' % p
        p = p[::-1]
        z[i][j] = int(p, 2)
#print(z)

C = Image.fromarray(z)
C.save('Final.bmp')
print("DONE")

【问题讨论】:

  • 我不确定您在这里寻找什么。您的行 x = x.convert('L') 会将您的图像转换为灰度。颜色信息丢失了,再多的处理也无法取回它。除非你问的是色调映射?
  • 你能不能修改上面给定的代码,让它输入一个 RGB 图像,然后输出为 rgb 图像,但是以灰度进行处理

标签: python arrays matlab numpy


【解决方案1】:

我认为(?)您正在寻找的是 Colour Mapping 函数,例如:

def create_colourmap(colour, grey):
    c_map = numpy.zeros((256,4), dtype=numpy.float64)
    for i in range(colour.shape[0]):
        for j in range(colour.shape[1]):
            tone = grey[i,j]
            c_map[tone,3] +=1
            count = c_map[tone, 3]
            c_map[tone,:3]  = (c_map[tone,:3] * (count-1)  + colour[i,j])/count
    return c_map.astype(numpy.uint8)[:,:3]

这将为每个灰度值(或色调)提供一个颜色值。

您的“位反转”过程可以通过另一个函数来简化:

def reverse_bits(arr):
    for i in range(arr.shape[0]):
        for j in range(arr.shape[1]):
            arr[i][j] = int('{0:08b}'.format(arr[i][j])[::-1], 2)
    return arr

综上所述,您的加密功能将是:

def encrypt(infile, outfile):

    with Image.open(infile, 'r') as colour_img:
        colour_arr = numpy.array(colour_img)
        grey_img = colour_img.convert('L')

    grey_arr = numpy.array(grey_img)
    c_map = create_colourmap(colour_arr, grey_arr)

    with Image.fromarray(c_map) as cmap_img:
        cmap_img.save(outfile[:-4]+'_cmap.bmp')

    grey_arr = reverse_bits(grey_arr)

    with Image.fromarray(grey_arr) as c:
        c.save(outfile)
    print("Encryption DONE")

还有你的解密功能:

def decrypt(infile, outfile):
    with Image.open(infile, 'r') as x:
        y = numpy.array(x)
    y = reverse_bits(y)

    colour_arr = numpy.zeros((y.shape[0], y.shape[1], 3), dtype=numpy.uint8)

    with Image.open(infile[:-4]+'_cmap.bmp') as cmap_img:
        cmap = numpy.array(cmap_img)

    for i in range(256):
        colour_arr[y==i] = cmap[i]
    
    with Image.fromarray(colour_arr) as c:
        c.save(outfile)
    print("Decryption DONE")

您会注意到颜色映射不会完全恢复图像颜色,但会给图像一些色调。我不知道您的任务是什么,但您可能希望将颜色图与加密图像一起发送,或者给他们另一个颜色相似的图像。

我希望这对您有所帮助,但您需要了解其工作原理,并在必要时发表评论以解释发生了什么。

祝你好运!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多