Beispiel #1
0
def fvht_image_unpack(frame, display_progress = True, str_pr = "", d = {}, handle = True):
    rgb = []
    for i in range(frame["channels"]):
        pack = {
            "rows":frame["rows"],
            "cols":frame["cols"],
            "channels": 1,
            "wave_type":frame["wave_type"],
            "bpp":frame["bpp"],
            "quant_delta":frame["quant_delta"],
            "decomp_level":frame["decomp_level"],
            "wise_bit":frame["wise_bit"][i],
            "payload":frame["payload"][i],
            "Lbpp": frame["Lbpp"],
            "lbpp": frame["lbpp"],
            "alpha": frame["alpha"],
            "c": frame["c"],
            "gamma": frame["gamma"],
            "fovea_center": frame["fovea_center"]
            }
        wave = fvht_unpack(pack,True,str_pr + "[channel "+str(i)+"]", d, handle)
        if frame["wave_type"] == 'cdf97':
            ch = lwt.icdf97(wave,False)
        elif frame["wave_type"] == 'cdf53':
            ch = lwt.icdf53(wave,False)
        else:
            ch = dwt.dwt2(wave,frame["wave_type"],frame["decomp_level"],frame["mode"])
        ch = ch - ch.min()
        ch = ch / ch.max() * 255
        ch_i = np.zeros((len(ch),len(ch[0])),np.uint8)
        ch_i[:] = ch
        rgb += [cv.fromarray(ch_i)]
    return tuple(rgb)
Beispiel #2
0
def decompress_speck(path, dest_path):
    if path[-1] != "/":
        path += "/"
    info = pickle.load(open(path + "info.dat", "r"))
    if not os.path.exists(dest_path):
        os.makedirs(dest_path)
    codec = sk.speck()
    for c in range(info.frames):
        frame = pickle.load(open(path + str(c) + ".speck","rb"))
        wavelet = codec.expand(frame["payload"], info.wavelet_cols,
                                   info.wavelet_rows, info.wavelet_level,
                                   frame["wise_bit"])
        iframe = lwt.icdf97(wavelet)
        iframe = tools.aquant(iframe, 100000)
        iframe = tools.unpadding(iframe, (info.rows, info.cols))
        if not cv2.imwrite(dest_path + str(c) + ".png",
                            iframe, [cv2.cv.CV_IMWRITE_PNG_COMPRESSION, 0]):
            print "Failed to create: " + dest_path + str(c) + ".png"
    pickle.dump(info, open(dest_path + "info.dat", "w"))
Beispiel #3
0
def compress_motion_speck(path, dest_path, bpp, dec_level=4,
                          macroblock_size=8, fixed_keyframe=0):
    """This method compress a image sequence from a directory using speck and
    motion compensation.

    Args:
        path: The directory where the image sequence to be encoded is stored
        dest_path: A destination directory where the encoded frames will be
                   stored
        bpp: Compression ratio on bits per pixel

    Kwargs:
        dec_level: Level of wavelet decomposition to be used
        macroblock_size: size of the macroblock used for motion compensation
        fixed_keyframe: size of the Group of Pictures

    """
    if path[-1] != "/":
        path += "/"
    info = pickle.load(open(path + "info.dat", "r"))
    is_key = 0
    info.fixed_keyframe = fixed_keyframe
    info.full_size = 1
    if not os.path.exists(dest_path):
        os.makedirs(dest_path)
    info.macroblock_size = macroblock_size
    codec = sk.speck()
    info.wavelet = "cdf97"
    info.wavelet_level = dec_level
    info.full_size = 100
    for c in range(info.frames):
        original_frame = cv2.imread(path + str(c) + ".png",
                                    cv2.CV_LOAD_IMAGE_GRAYSCALE)
        info.cols = original_frame.shape[1]
        info.rows = original_frame.shape[0]
        frame = tools.zero_padding(original_frame)
        info.wavelet_cols = frame.shape[1]
        info.wavelet_rows = frame.shape[0]
        if is_key == 0:
            wavelet = lwt.cdf97(frame, dec_level)
            wavelet = tools.quant(wavelet, 0.0001)
            coded_frame = codec.compress(wavelet, bpp)
            stream = dict()
            stream["wise_bit"] = coded_frame[3]
            stream["payload"] = coded_frame[4]
            try:
                pickle.dump(stream, open(dest_path + str(c) + ".speck", "wb"))
            except:
                print "Failed to create: " + dest_path + str(c) + ".png"
            iwave = codec.expand(coded_frame[4], frame.shape[1],
                                 frame.shape[0], dec_level, coded_frame[3])
            iframe = lwt.icdf97(iwave)
            is_key = fixed_keyframe - 1
            key_frame = iframe
            info.motion_vectors += [0]
        else:
            p_frame, mvs = intraframe.encode_motion_frame(frame,
                                                          key_frame,
                                                          macroblock_size,
                                                          info.full_size)
            info.motion_vectors += [(mvs)]
            is_key -= 1
            wavelet = lwt.cdf97(p_frame, dec_level)
            wavelet = tools.quant(wavelet, 0.0001)
            coded_frame = codec.compress(wavelet, bpp)
            stream = dict()
            stream["wise_bit"] = coded_frame[3]
            stream["payload"] = coded_frame[4]
            try:
                pickle.dump(stream, open(dest_path + str(c) + ".speck", "wb"))
            except:
                print "Failed to create: " + dest_path + str(c) + ".png"
    pickle.dump(info, open(dest_path + "info.dat", "w"))