Example #1
0
def compress_speck(path, dest_path, dec_level, bpp):
    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()
    info.wavelet = "cdf97"
    info.wavelet_level = dec_level
    info.frames = 3
    info.bpp = bpp
    for c in range(info.frames):
        frame = cv2.imread(path + str(c) + ".png",
                                    cv2.CV_LOAD_IMAGE_GRAYSCALE)
        info.cols = frame.shape[1]
        info.rows = frame.shape[0]
        frame = tools.zero_padding(frame)
        info.wavelet_cols = frame.shape[1]
        info.wavelet_rows = frame.shape[0]
        wavelet = lwt.cdf97(frame, dec_level)
        wavelet = tools.quant(wavelet, 0.00001)
        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) + ".speck"
    pickle.dump(info, open(dest_path + "info.dat", "w"))
Example #2
0
def spiht_image_pack(img, wavename, level, bpp, mode = "bi.orth", delta = 0.01, display_progress = True, str_pr = "", d = {}, handle = True):
    if not isinstance(img,tuple):
        img = (img)
        bpp = (bpp)
    ch_stream = {
            "size": 0,
            "channels":len(img),
            "bpp":bpp,
            "quant_delta":delta,
            "wave_type": wavename,
            "mode":mode,
            "decomp_level":level,
            "wise_bit":[],
            "payload":[]
            }
    for i in range(len(img)):
        m = np.asarray(img[i])
        if mode == "bi.orth":
            if wavename == "cdf97": 
                w = lwt.cdf97(m,level,False)
            elif wavename == "cdf53":
                w = lwt.cdf53(m,level,False)
        else:
            w = dwt.dwt2(m,wavename,mode, level)
        stream = spiht_pack(w,bpp[i],delta,display_progress,str_pr + "[channel "+str(i)+"]",d,handle)
        ch_stream["payload"] += stream["payload"]
        ch_stream["wave_type"] = w.name
        ch_stream["size"] += stream["size"]
        ch_stream["wise_bit"] += [stream["wise_bit"]]
        ch_stream["rows"] = stream["rows"]
        ch_stream["cols"] = stream["cols"]
        ch_stream["test_data"] = stream["test_data"]
    return ch_stream
Example #3
0
def fvht_image_pack(img, wavename, level, f_center, Lbpp, lbpp, alpha, c, gamma, mode = "bi.orth", delta = 0.01, display_progress = True, str_pr = "", d = {}, handle = True):
    if not isinstance(img,tuple):
        img = (img)
    ch_stream = {
            "size": 0,
            "channels":len(img),
            "bpp":Lbpp,
            "quant_delta":delta,
            "wave_type": wavename,
            "mode":mode,
            "decomp_level":level,
            "wise_bit":[],
            "Lbpp": Lbpp,
            "lbpp": lbpp,
            "alpha": alpha,
            "c": c,
            "gamma": gamma,
            "fovea_center": f_center,
            "payload":[]
            }
    for i in range(len(img)):
        m = np.asarray(img[i])
        if mode == "bi.orth":
            if wavename == "cdf97":
                w = lwt.cdf97(m,level,False)
            elif wavename == "cdf53":
                w = lwt.cdf53(m,level,False)
        else:
            w = dwt.dwt2(m,wavename,level,mode)
        stream = fvht_pack(w, f_center, Lbpp, lbpp, alpha, c, gamma, delta, display_progress,str_pr + "[channel "+str(i)+"]",d,handle)
        ch_stream["payload"] += stream["payload"]
        ch_stream["wave_type"] = w.name
        ch_stream["size"] += stream["size"]
        ch_stream["wise_bit"] += [stream["wise_bit"]]
        ch_stream["rows"] = stream["rows"]
        ch_stream["cols"] = stream["cols"]
        ch_stream["bits"] = stream["bits"]
        ch_stream["test_data"] = stream["test_data"]
    return ch_stream
Example #4
0
 def zip_from_cvimage(self, img):
     mtx = np.asarray(img)
     wave = lwt.cdf97(mtx)
     return frame_compress(wave)
Example #5
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"))