2017-11-14 4 views
-3

필자는 다시 한번 orignal 이미지가 필요하지만 반환하는 그레이 스케일 선생님은 처음에 컬러 맵을 사용하여 matlab에서 일어난다 고 말했습니다. 그래서 내가 도와주세요 내 코드는 모양이 너무 다음은 파이썬에서 비슷한 작업을 수행 할matlab colormap in 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") 
+0

나는 당신이 여기 무엇을 찾고 있는지 잘 모르겠습니다. x = x.convert ('L') 라인은 이미지를 그레이 스케일로 변환합니다. 색상 정보가 손실되고 처리량이 줄어들어 색상 정보가 복원되지 않습니다. 톤 매핑에 대해 묻지 않는 한? –

+0

위의 주어진 코드를 수정하여 RGB 이미지를 입력 한 다음 출력을 rgb 이미지로 제공하지만 그레이 스케일로 처리 할 수 ​​있습니까? – prashant

답변

0

내가 무슨 생각을 (?) 찾고있는 것은 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") 

색상 매핑을 사용해도 색상 표가 완전히 복원되지는 않습니다. 이미지 색상이지만 이미지에 약간의 색조가 부여됩니다. 귀하의 과제가 무엇인지 모르겠지만 암호화 된 이미지와 함께 컬러 맵을 보내거나 유사한 색상으로 다른 이미지를 제공 할 수 있습니다.

이 정보가 도움이되기를 바랍니다. 그러나 어떻게 작동하는지 알아야하고, 필요한 부분을 설명해야합니다.

행운을 빈다.