예제 #1
0
def HM_compress_for_GRAY_image(path):
    img = cv.imread(path, cv.IMREAD_GRAYSCALE)
    list_data_of_img = list(img.ravel())
    start_time = time.time()
    # Histogram
    freq, huffmancode, treeHM = HM_image.Coding(list_data_of_img)
    height = img.shape[0]
    width = img.shape[1]
    input_bits = width * height * 8
    summ = HM_image.Sum_Bit(freq, huffmancode)
    print("Number of bits required to represent the data before compression:",
          input_bits)
    print("Number of bits required to represent the data after compression:",
          summ)
    ratio = (input_bits / summ)
    print("Compression ratio: ", ratio)
    print("WRITE DATA TO FILE")
    filename = HM_image.filename_from_path(path)
    print("File compress will save at folder .\compress_file_HM\\%s" %
          filename)
    filename_compress = os.getcwd(
    ) + '/compressed_file_HM/compressed_file_' + filename
    HM_image.Write_to_file(list_data_of_img, huffmancode, filename_compress)
    print("DONE COMPRESSS")
    end_time = time.time()
    time_es = (end_time - start_time) * 1000
    return time_es, ratio, treeHM, height, width, filename_compress
예제 #2
0
def HM_Color_C(path):
    img = cv.imread(path, cv.IMREAD_COLOR)
    S_T = time.time()
    b, g, r = cv.split(img)

    xb = list(b.ravel())
    xg = list(g.ravel())
    xr = list(r.ravel())

    # hist = np.bincount(im.ravel(), minlength=256)
    freq_B, HMcode_B, Treenode_B = HM_image.Coding(xb)
    freq_G, HMcode_G, Treenode_G = HM_image.Coding(xg)
    freq_R, HMcode_R, Treenode_R = HM_image.Coding(xr)
    height = img.shape[0]
    width = img.shape[1]
    input_bits = img.shape[0] * img.shape[1] * 8 * 3

    summ = HM_image.Sum_Bit(freq_B, HMcode_B) + HM_image.Sum_Bit(freq_G, HMcode_G) + HM_image.Sum_Bit(freq_R, HMcode_R)

    print("Number of bits required to represent the data before compression:", input_bits)
    print("Number of bits required to represent the data after compression by Huffman coding:", summ)
    ratio = (input_bits / summ)
    print("Compression ratio: ", ratio)
    # Write_to_file(xg, h, 'texttttt')
    filename = HM_image.filename_from_path(path)
    filename_compress = os.getcwd() + '/compressed_file_HM/compressed_file_' + filename
    HM_image.Write_to_file(xb, HMcode_B, filename_compress + '_B')
    HM_image.Write_to_file(xg, HMcode_G, filename_compress + '_G')
    HM_image.Write_to_file(xr, HMcode_R, filename_compress + '_R')
    E_T = time.time()
    time_elapsed = (E_T - S_T)
    print("DONE COMPRESSS\n\n\n")
    return time_elapsed, ratio, Treenode_B, Treenode_G, Treenode_R, height, width, filename_compress
예제 #3
0
def HM_Gray_D(root, H, W, filename):
    S_T = time.time()
    encode = HM_image.Decode_from_file(filename)
    image_1 = HM_image.Decode_Image(root, encode)
    temp = np.asarray(image_1)
    image_2 = temp.reshape(H, W)
    E_T = time.time()
    print("DONE HUFFMAN DECOMPRESSS\n\n\n")
    time_elapsed = (E_T - S_T)
    return time_elapsed
예제 #4
0
def LWZ_compress_GRAY_image(path):
    img = cv.imread(path, cv.IMREAD_GRAYSCALE)
    list_data_of_img = np.array(img.ravel())
    S_T = time.time()
    compress = LZW_image.Compress(list_data_of_img)
    height = img.shape[0]
    width = img.shape[1]
    input_bits = width * height * 8
    summ = LZW_image.Sum_bit(compress)
    print("20 element of LWZ array:", compress[0:20])
    print("Number of bits required to represent the data before compression:",
          input_bits)
    print("Number of bits required to represent the data after compression:",
          summ)
    # print("P/s: One number of LWZ array represent by %d to optimal compression ratio." % len_bit)
    ratio = (input_bits / summ)
    print("Compression ratio: ", ratio)
    print("WRITE DATA TO FILE")
    filename = HM_image.filename_from_path(path)
    print("File compress will save at folder .\compress_file_LZW\\%s" %
          filename)
    filename_compress = os.getcwd() + '\compressed_file_LZW\\' + filename
    LZW_image.Write_to_file(compress, filename_compress)
    print("DONE COMPRESSS\n\n\n")
    E_T = time.time()
    time_elapsed = (E_T - S_T) * 1000
    return time_elapsed, ratio, height, width, filename_compress
예제 #5
0
def LWZ_Color_C(path):
    img = cv.imread(path, cv.IMREAD_COLOR)
    S_T = time.time()
    # list_data_of_img = np.array(img.ravel())
    b, g, r = cv.split(img)

    xb = list(b.ravel())
    xg = list(g.ravel())
    xr = list(r.ravel())
    b_compress = LZW_image.Compress(xb)
    g_compress = LZW_image.Compress(xg)
    r_compress = LZW_image.Compress(xr)

    height = img.shape[0]
    width = img.shape[1]
    input_bits = width * height * 8 * 3
    summ = LZW_image.Sum_bit(b_compress) + LZW_image.Sum_bit(g_compress) + LZW_image.Sum_bit(r_compress)
    print("Number of bits required to represent the data before compression:", input_bits)
    print("Number of bits required to represent the data after compression by LZW:", summ)
    ratio = (input_bits / summ)
    print("Compression ratio: ", ratio)
    print("WRITE DATA TO FILE")
    filename = HM_image.filename_from_path(path)
    filename_compress = os.getcwd() + '\compressed_file_LZW\\' + filename
    LZW_image.Write_to_file(b_compress, filename_compress+"_B")
    LZW_image.Write_to_file(g_compress, filename_compress + "_G")
    LZW_image.Write_to_file(r_compress, filename_compress + "_R")
    E_T = time.time()
    time_elapsed = (E_T - S_T)
    print("DONE LZW COMPRESSS\n\n\n")
    return time_elapsed, ratio, height, width, filename_compress
예제 #6
0
def Huffman_GRAY_image_decompress(root, H, W, filename):
    S_T = time.time()

    encode = HM_image.Decode_from_file(filename)
    image_1 = HM_image.Decode_Image(root, encode)
    size = H * W
    if len(image_1) < size:
        while (len(image_1) != size):
            image_1.append(0)
    else:
        image_1 = image_1[0:size]
    temp = np.asarray(image_1)
    image_2 = temp.reshape(H, W)
    E_T = time.time()
    cv.imshow('DECOMPRESS FROM HUFFMAN DECODE', image_2)
    k = cv.waitKey(0)
    if k == 27:  # wait for ESC key to exit
        cv.destroyAllWindows()
    print("DONE DECOMPRESSS\n\n\n")
    time_elapsed = (E_T - S_T) * 1000
    return time_elapsed
예제 #7
0
def Huffman_COLOR_image_decompress(rootB, rootG, rootR, H, W, filepath):
    print("Read file compressed from folder: %s" % filepath)
    if (not os.path.isfile(filepath + "_B")):
        print("Loi khong tim thay file %s" % filepath)
    else:
        print("File hop le")
    S_T = time.time()
    encode_B = HM_image.Decode_from_file(filepath + '_B')
    encode_G = HM_image.Decode_from_file(filepath + '_G')
    encode_R = HM_image.Decode_from_file(filepath + '_R')

    data_of_B = HM_image.Decode_Image(rootB, encode_B)
    data_of_R = HM_image.Decode_Image(rootR, encode_R)
    data_of_G = HM_image.Decode_Image(rootG, encode_G)

    size = H * W
    if len(data_of_B) < size:
        while (len(data_of_B) != size):
            data_of_B.append(0)
    else:
        data_of_B = data_of_B[0:size]

    if len(data_of_R) < size:
        while (len(data_of_R) != size):
            data_of_R.append(0)
    else:
        data_of_R = data_of_R[0:size]

    if len(data_of_G) < size:
        while (len(data_of_G) != size):
            data_of_G.append(0)
    else:
        data_of_G = data_of_G[0:size]
    temp1 = np.asarray(data_of_B)
    temp2 = np.asarray(data_of_G)
    temp3 = np.asarray(data_of_R)

    img_B = temp1.reshape(H, W)
    img_G = temp2.reshape(H, W)
    img_R = temp3.reshape(H, W)
    img = cv.merge((img_B, img_G, img_R))

    E_T = time.time()
    time_elapsed = (E_T - S_T) * 1000
    cv.imshow('DECOMPRESSS BY HUFFMAN DECODE', img)
    k = cv.waitKey(0)
    if k == 27:  # wait for ESC key to exit
        cv.destroyAllWindows()
    print("DONEEEEEEEEE\n\n\n")

    return time_elapsed
예제 #8
0
def HM_Color_D(rootB, rootG, rootR, H, W, filepath):
    print("Read file compressed from folder: %s" % filepath)
    if (not os.path.isfile(filepath + "_B")):
        print("Loi khong tim thay file %s" % filepath)
    else:
        print("File hop le")
    S_T = time.time()
    encode_B = HM_image.Decode_from_file(filepath + '_B')
    encode_G = HM_image.Decode_from_file(filepath + '_G')
    encode_R = HM_image.Decode_from_file(filepath + '_R')

    data_of_B = HM_image.Decode_Image(rootB, encode_B)
    data_of_R = HM_image.Decode_Image(rootR, encode_R)
    data_of_G = HM_image.Decode_Image(rootG, encode_G)
    size = H * W
    if len(data_of_B) <size:
        while(len(data_of_B)!=size):
            data_of_B.append(0)
    else:
        data_of_B= data_of_B[0:size]

    if len(data_of_R) <size:
        while(len(data_of_R)!=size):
            data_of_R.append(0)
    else:
        data_of_R= data_of_R[0:size]

    if len(data_of_G) <size:
        while(len(data_of_G)!=size):
            data_of_G.append(0)
    else:
        data_of_G= data_of_G[0:size]
    temp1 = np.asarray(data_of_B)
    temp2 = np.asarray(data_of_G)
    temp3 = np.asarray(data_of_R)
    size = H*W
    if len(temp1) <size:
        while(len(temp1)!=size):
            temp1
    img_B = temp1.reshape(H, W)
    img_G = temp2.reshape(H, W)
    img_R = temp3.reshape(H, W)
    img = cv.merge((img_B, img_G, img_R))

    E_T = time.time()
    time_elapsed = (E_T - S_T)
    print("DONEEEEEEEEE\n\n\n")

    return time_elapsed