示例#1
0
def MSE_RGB(img0, img1):
    '''
    计算两张图片的MSE,
    img0 和 img1 的大小需要一样
    '''
    I0 = load_img(img0)
    I1 = load_img(img1)
    (width, height) = I0.size
    rgb0 = np.array(I0).transpose(1, 2, 0).astype(float)
    rgb1 = np.array(I1).transpose(1, 2, 0).astype(float)
    mse = sum(sum(sum((rgb0 - rgb1)**2)))
    return mse
示例#2
0
def main():
    # start matlab engine
    print("start a matlab engine")
    eng = matlab.engine.start_matlab()
    eng.addpath(LIB_PATH)
    #  image to mat
    # I = load_img(WHALE_IMG)
    I = load_img(WHALE_IMG_128)
    #  I = load_img(LEAN_IMG)
    #  I = load_img(ORCA_IMG)
    (width, height) = I.size
    mat_r = np.empty((width, height))
    mat_g = np.empty((width, height))
    mat_b = np.empty((width, height))
    for i in range(width):
        for j in range(height):
            [r, g, b] = I.getpixel((i, j))
            mat_r[i, j] = r
            mat_g[i, j] = g
            mat_b[i, j] = b
    #  encode part
    print("load image done, image shape : (", mat_r.shape[0], ",",
          mat_r.shape[1], ")")
    I_W_r = func_DWT(mat_r, LEVEL)
    I_W_g = func_DWT(mat_g, LEVEL)
    I_W_b = func_DWT(mat_b, LEVEL)
    print("image 2d DWT done, level : ", LEVEL)
    out_bits_r = spiht_encode(I_W_r, eng)
    out_bits_g = spiht_encode(I_W_g, eng)
    out_bits_b = spiht_encode(I_W_b, eng)
    print("spiht encode done , out bits len : ", len(out_bits_r))
    out_file_r = code_to_file(out_bits_r, OUT_BIN_R)
    out_file_g = code_to_file(out_bits_g, OUT_BIN_G)
    out_file_b = code_to_file(out_bits_b, OUT_BIN_B)

    #decode part
    read_bits_r = file_to_code(out_file_r)
    read_bits_g = file_to_code(out_file_g)
    read_bits_b = file_to_code(out_file_b)
    print("read bits from file, bits len : ", len(read_bits_r))
    I_W_decode_r = spiht_decode(read_bits_r, eng)
    I_W_decode_g = spiht_decode(read_bits_g, eng)
    I_W_decode_b = spiht_decode(read_bits_b, eng)

    I_W_re_r = func_IDWT(I_W_decode_r, LEVEL)
    I_W_re_g = func_IDWT(I_W_decode_g, LEVEL)
    I_W_re_b = func_IDWT(I_W_decode_b, LEVEL)
    dwt_img = Image.new('RGB', (width, height), (0, 0, 20))
    for i in range(width):
        for j in range(height):
            R = I_W_re_r[i, j]
            G = I_W_re_g[i, j]
            B = I_W_re_b[i, j]
            new_value = (int(R), int(G), int(B))
            dwt_img.putpixel((i, j), new_value)
    dwt_img.save(WHALE_IMG_NEW)
    return I_W_r, out_bits_r, read_bits_r
示例#3
0
    def __init__(self, port,
                 baudrate,
                 timeout,
                 img_path=LENA_IMG,
                 level=3,
                 wavelet='bior4.4',
                 mode='periodization',

                 fountain_chunk_size=3000,          # 应能被数据长度30000整除
                 fountain_type='normal',
                 drop_interval=1,

                 w1_size=0.1,
                 w1_pro=0.084,
                 seed=None):

        self.port = serial.Serial(port, baudrate)
        self.drop_id = 0
        self.eng = matlab.engine.start_matlab()
        self.eng.addpath(LIB_PATH)
        self.img_path = img_path
        self.fountain_chunk_size = fountain_chunk_size
        self.fountain_type = fountain_type
        self.drop_interval = drop_interval
        self.w1_p = w1_size
        self.w1_pro = w1_pro
        # self.port = port
        self.seed = seed
        self.recvdone_ack = False
        self.detect_ack = False
        self.rs_send = False
        self.lt_send = False
        self.detect_start_time = 0

        # if self.port.is_open:
        #     print("串口{}打开成功".format(self.port.portstr))
        # else:
        #     print("串口打开失败")

        temp_file = self.img_path  # .replace(self.img_path.split('/')[-1], 'tmp')
        rgb_list = ['r', 'g', 'b']
        temp_file_list = [temp_file + '_' + ii for ii in rgb_list]
        if self.is_need_img_process():
            print('processing image: {:s}'.format(self.img_path))
            img = load_img(self.img_path)
            (width, height) = img.size
            mat_r = np.empty((width, height))
            mat_g = np.empty((width, height))
            mat_b = np.empty((width, height))
            for i in range(width):
                for j in range(height):
                    [r, g, b] = img.getpixel((i, j))
                    mat_r[i, j] = r
                    mat_g[i, j] = g
                    mat_b[i, j] = b
            self.img_mat = [mat_r, mat_g, mat_b]
            self.dwt_coeff = [func_DWT(ii) for ii in self.img_mat]                         # r,g,b小波变换得到系数
            self.spiht_bits = [spiht_encode(ii, self.eng) for ii in self.dwt_coeff]        # r,g,b的spiht编码

            a = [code_to_file(self.spiht_bits[ii],
                              temp_file_list[ii],
                              add_to=self.fountain_chunk_size / 3 * 8)
                 for ii in range(len(rgb_list))]
        else:
            print('temp file found : {:s}'.format(self.img_path))

        self.m, _chunk_size = self.compose_rgb(temp_file_list)

        self.fountain = self.fountain_builder()

        #  a = [os.remove(ii) for ii in temp_file_list]
        self.show_info()
示例#4
0
    def __init__(self,
                 img_path,
                 level=3,
                 wavelet='bior4.4',
                 mode='periodization',
                 fountain_chunk_size=1000,
                 fountain_type='normal',
                 drop_interval=5,
                 port=PORT,
                 w1_size=0.1,
                 w1_pro=0.084,
                 seed=None):
        self.drop_id = 0
        self.eng = matlab.engine.start_matlab()
        self.eng.addpath(LIB_PATH)
        self.img_path = img_path
        self.fountain_chunk_size = fountain_chunk_size
        self.fountain_type = fountain_type
        self.drop_interval = drop_interval
        self.w1_p = w1_size
        self.w1_pro = w1_pro
        self.port = port
        self.seed = seed

        #  三通道一起处理
        temp_file = self.img_path  #  .replace(self.img_path.split('/')[-1], 'tmp')
        rgb_list = ['r', 'g', 'b']
        temp_file_list = [temp_file + '_' + ii for ii in rgb_list]
        if self.is_need_img_process():
            print('process imgage: {:s}'.format(self.img_path))
            img = load_img(self.img_path)
            (width, height) = img.size
            mat_r = np.empty((width, height))
            mat_g = np.empty((width, height))
            mat_b = np.empty((width, height))
            for i in range(width):
                for j in range(height):
                    [r, g, b] = img.getpixel((i, j))
                    mat_r[i, j] = r
                    mat_g[i, j] = g
                    mat_b[i, j] = b
            self.img_mat = [mat_r, mat_g, mat_b]
            self.dwt_coeff = [func_DWT(ii) for ii in self.img_mat]
            self.spiht_bits = [
                spiht_encode(ii, self.eng) for ii in self.dwt_coeff
            ]

            #  trick
            a = [
                code_to_file(self.spiht_bits[ii],
                             temp_file_list[ii],
                             add_to=self.fountain_chunk_size / 3 * 8)
                for ii in range(len(rgb_list))
            ]
        else:
            print('temp file found : {:s}'.format(self.img_path))
        self.m, _chunk_size = self._321(
            temp_file_list, each_chunk_size=self.fountain_chunk_size / 3)

        #  暂时只有 r, 没有 g b
        #  code_to_file(self.spiht_bits[0], temp_file)
        #  m = open(temp_file, 'r').read()
        self.write_fd = self.socket_builder()

        self.fountain = self.fountain_builder()

        #  a = [os.remove(ii) for ii in temp_file_list]
        self.show_info()