def jpegCompress(image, quantmatrix):
    '''
        Compress(imagefile, quanmatrix simulates the lossy compression of
        baseline JPEG, by quantizing the DCT coefficients in 8x8 blocks
    '''
    # Return compressed image in result

    H = np.size(image, 0)
    W = np.size(image, 1)

    # Convert to gray-scale image
    if np.size(image, 2) > 1:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Number of 8x8 blocks in the height and width directions
    h8 = H / 8
    w8 = W / 8
    iH = H
    iW = W

    # Padding image
    if H % 8 != 0:
        image = np.lib.pad(image, ((0, 8 - H % 8), (0, 0)), 'constant', constant_values=((0, 0), (0, 0)))
        h8 += 1

    if W % 8 != 0:
        image = np.lib.pad(image, ((0, 0), (0, 8 - W % 8)), 'constant', constant_values=((0, 0), (0, 0)))
        w8 += 1

    image = image.astype("float")
    dct_coefficient = image.copy()
    result = image.copy()
    # Calculate DCT coefficients for patches
    for i in xrange(h8):
        for j in xrange(w8):
            h_start, h_end, w_start, w_end = patchRegion(i, j)
            patch = image[h_start : h_end, w_start : w_end]
            cv2.dct(patch, patch)
            # Quantization
            patch = np.round(patch / quantmatrix)
            dct_coefficient[h_start : h_end, w_start : w_end] = patch

    # Convert back
    for i in xrange(h8):
        for j in xrange(w8):
            h_start, h_end, w_start, w_end = patchRegion(i, j)
            patch = dct_coefficient[h_start : h_end, w_start : w_end]
            cv2.idct(patch, patch)
            result[h_start : h_end, w_start : w_end] = patch

    # Remove padding effect
    result = result[0 : iH, 0 : iW]

    return result
def estimate(dct_target, dct_left=False, dct_up=False, dct_right=False, dct_down=False):
    """
    Estimation of DCT coefficients with the help of adjacent blocks.
    :param dct_target: ndarray, DCT of target block
    :param dct_left: ndarray, DCT of left block
    :param dct_up: ndarray, DCT of upper block
    :param dct_right: ndarray, DCT of right block
    :param dct_down: ndarray, DCT of nether block
    :return: dc_optimal: float, optimal value of DC prediction
    """
    mse_min = np.inf
    dc_optimal = 0.
    if dct_left is not False:
        spatial_left = cv2.idct(dct_left * Q) + 128
    if dct_up is not False:
        spatial_up = cv2.idct(dct_up * Q) + 128
    if dct_right is not False:
        spatial_right = cv2.idct(dct_right * Q) + 128
    if dct_down is not False:
        spatial_down = cv2.idct(dct_down * Q) + 128
    for dc in DC_range:
        mse1 = 0.
        mse2 = 0.
        mse3 = 0.
        dct_target[0, 0] = dc
        spatial_target = cv2.idct(dct_target * Q) + 128
        if dct_left is not False:
            mse1 += np.sum(np.square((spatial_left[0:8, 7] - spatial_target[0:8, 0]) -
                                     (spatial_left[0:8, 6] - spatial_left[0:8, 7]))) / 8
            mse2 += np.sum(np.square((spatial_left[0:7, 7] - spatial_target[1:8, 0]) -
                                     (spatial_left[0:7, 6] - spatial_left[1:8, 7]))) / 7
            mse3 += np.sum(np.square((spatial_left[1:8, 7] - spatial_target[0:7, 0]) -
                                     (spatial_left[1:8, 6] - spatial_left[0:7, 7]))) / 7
        if dct_up is not False:
            mse1 += np.sum(np.square((spatial_up[7, 0:8] - spatial_target[0, 0:8]) -
                                     (spatial_up[6, 0:8] - spatial_up[7, 0:8]))) / 8
            mse2 += np.sum(np.square((spatial_up[7, 0:7] - spatial_target[0, 1:8]) -
                                     (spatial_up[6, 0:7] - spatial_up[7, 1:8]))) / 7
            mse3 += np.sum(np.square((spatial_up[7, 1:8] - spatial_target[0, 0:7]) -
                                     (spatial_up[6, 1:8] - spatial_up[7, 0:7]))) / 7
        if dct_right is not False:
            mse1 += np.sum(np.square((spatial_right[0:8, 0] - spatial_target[0:8, 7]) -
                                     (spatial_right[0:8, 1] - spatial_right[0:8, 0]))) / 8
            mse2 += np.sum(np.square((spatial_right[0:7, 0] - spatial_target[1:8, 7]) -
                                     (spatial_right[0:7, 1] - spatial_right[1:8, 0]))) / 7
            mse3 += np.sum(np.square((spatial_right[1:8, 0] - spatial_target[0:7, 7]) -
                                     (spatial_right[1:8, 1] - spatial_right[0:7, 0]))) / 7
        if dct_down is not False:
            mse1 += np.sum(np.square((spatial_down[0, 0:8] - spatial_target[7, 0:8]) -
                                     (spatial_down[1, 0:8] - spatial_down[0, 0:8]))) / 8
            mse2 += np.sum(np.square((spatial_down[0, 0:7] - spatial_target[7, 1:8]) -
                                     (spatial_down[1, 0:7] - spatial_down[0, 1:8]))) / 7
            mse3 += np.sum(np.square((spatial_down[0, 1:8] - spatial_target[7, 0:7]) -
                                     (spatial_down[1, 1:8] - spatial_down[0, 0:7]))) / 7
        mse = np.min([mse1, mse2, mse3])
        if mse < mse_min:
            mse_min = mse
            dc_optimal = dc

    return dc_optimal
Пример #3
0
    def _run_(self):
        '''
        '''
        #print '- Running Mjpeg Decoder...'
        hf = h.HuffCoDec(self.hufftables)
        r, c, chnl = self.R, self.C, self.NCHNL
        Z = self.Z
        
        #hufcd = self.huffcodes#self.fl.readline()[:-1]
        if self.mode == '444':
            for ch in range(chnl):                #hufcd = self.fl.readline()[:-1]            #    print hufcd[0:20]
                nblk, seqrec = hf.invhuff(self.huffcodes[ch], ch)
                for i in range(self.nBlkRows):
                    for j in range(self.nBlkCols):
                        blk = h.zagzig(seqrec[i*self.nBlkCols + j])
                        self.imRaw[r*i:r*i+r, c*j:c*j+c, ch] = np.round_( cv2.idct( blk*Z[:,:,ch] ))
                        
        elif self.mode == '420':
            #import math as m
            if chnl == 1:
                rYmg = self.imRaw
            else:                #Y = self.imRaw[:,:,0]
                Y = np.zeros( (self.M, self.N) )
                dims, CrCb = h.adjImg( downsample(np.zeros( (self.M, self.N, 2) ), self.mode)[1] )
                rYmg = [ Y, CrCb[:,:,0], CrCb[:,:,1] ]
                
            for ch in range(chnl):
                #hufcd = self.fl.readline()[:-1]
                if ch == 0:
                    rBLK = self.nBlkRows
                    cBLK = self.nBlkCols
                else:
                    rBLK, cBLK = int(np.floor(dims[0]/self.R)), int(np.floor(dims[1]/self.C))
            #    print hufcd[0:20]
                nblk, self.seqrec = hf.invhuff(self.huffcodes[ch], ch)
                for i in range(rBLK):
                    for j in range(cBLK):
                        blk = h.zagzig(self.seqrec[i*cBLK + j])
                        #print rYmg[ch][r*i:r*i+r, c*j:c*j+c].shape, ch, i, j
                        rYmg[ch][r*i:r*i+r, c*j:c*j+c] = np.round_( cv2.idct( blk*Z[:,:,ch] ))
            # UPSAMPLE
            if chnl == 1:
                self.imRaw = rYmg #[:self.Mo, : self.No]
            else:
                self.imRaw[:,:,0] = rYmg[0]
                self.imRaw[:,:,1] = upsample(rYmg[1], self.mode)[:self.M, :self.N]
                self.imRaw[:,:,2] = upsample(rYmg[2], self.mode)[:self.M, :self.N]
        
        #self.fl.close()
        
#        imrec = cv2.cvtColor((self.imRaw[:self.Mo, :self.No]+128), cv2.COLOR_YCR_CB2BGR)
#        imrec = self.imRaw[:self.Mo, :self.No]+128
        imrec = self.imRaw+128.0
#        imrec[imrec>255.0]=255.0
#        imrec[imrec<0.0]=0.0
								
        #print 'Mjpeg Decoder Complete...'
        
        return imrec
Пример #4
0
 def _run_(self):
     '''
     '''
     print '- Running Decoder...'
     hf = h.HuffCoDec()
     r, c, chnl = self.R, self.C, self.NCHNL
     Z = self.Z
     
     if self.mode == '444':
         for ch in range(chnl):
             hufcd = self.fl.readline()[:-1]
         #    print hufcd[0:20]
             nblk, seqrec = hf.invhuff(hufcd, ch)
             for i in range(self.nBlkRows):
                 for j in range(self.nBlkCols):
                     blk = h.zagzig(seqrec[i*self.nBlkCols + j])
                     self.imRaw[r*i:r*i+r, c*j:c*j+c, ch] = np.round_( cv2.idct( blk*Z[:,:,ch] ))
                     
     elif self.mode == '420':
         #import math as m
         if chnl == 1:
             rYmg = self.imRaw
         else:                #Y = self.imRaw[:,:,0]
             Y = np.zeros( (self.M, self.N) )
             dims, CrCb = h.adjImg( downsample(np.zeros( (self.M, self.N, 2) ), self.mode)[1] )
             rYmg = [ Y, CrCb[:,:,0], CrCb[:,:,1] ]
             
         for ch in range(chnl):
             hufcd = self.fl.readline()[:-1]
             if ch == 0:
                 rBLK = self.nBlkRows
                 cBLK = self.nBlkCols
             else:
                 rBLK, cBLK = int(np.floor(dims[0]/self.R)), int(np.floor(dims[1]/self.C))
         #    print hufcd[0:20]
             nblk, self.seqrec = hf.invhuff(hufcd, ch)
             for i in range(rBLK):
                 for j in range(cBLK):
                     blk = h.zagzig(self.seqrec[i*cBLK + j])
                     #print rYmg[ch][r*i:r*i+r, c*j:c*j+c].shape, ch, i, j
                     rYmg[ch][r*i:r*i+r, c*j:c*j+c] = np.round_( cv2.idct( blk*Z[:,:,ch] ))
         # UPSAMPLE
         if chnl == 1:
             self.imRaw = rYmg #[:self.Mo, : self.No]
         else:
             self.imRaw[:,:,0] = rYmg[0]
             self.imRaw[:,:,1] = upsample(rYmg[1], self.mode)[:self.M, :self.N]
             self.imRaw[:,:,2] = upsample(rYmg[2], self.mode)[:self.M, :self.N]
     
     self.fl.close()
     
     imrec = cv2.cvtColor((self.imRaw[:self.Mo, :self.No]+128), cv2.COLOR_YCR_CB2BGR)
     imrec[imrec>255]=255
     imrec[imrec<0]=0
     
     print 'Decoder Complete...'
     
     return np.uint8(imrec)
def load_images(input_dir, batch_shape_clean, batch_shape_adv):
    image_clean = np.zeros(batch_shape_clean)
    image_adv = np.zeros(batch_shape_adv)
    gt_4 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 14, 14])
    gt_3 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 7, 7])
    gt_2 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 4, 4])
    gt_1 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 2, 2])
    labels = np.zeros(batch_shape_clean[0])
    idx = 0
    batch_size = batch_shape_adv[0]

    with open(input_dir, "rb") as f:
        data_dict = pickle.load(f)

    images_clean_list = data_dict["images_clean"]
    images_adv_list = data_dict["images_adv"]
    labels_list = data_dict["labels"]

    for j in range(len(images_clean_list)):
        image_clean[idx, 0, :, :] = images_clean_list[j]
        image_adv[idx, 0, :, :] = images_adv_list[j]
        image_clean_j = np.array(images_clean_list[j])
        dct_gt = np.zeros([28, 28, 1])
        dct_gt[:, :, 0] = image_clean_j[0, :, :]
        # cv2.imshow("gray", dct_gt)
        dct_gt = cv2.dct(dct_gt.astype(np.float32))
        # cv2.imshow("dct", dct_gt)
        idct_gt = cv2.idct(dct_gt)
        # cv2.imshow("idct", idct_gt)
        gt_4_j = cv2.idct(dct_gt[0:14, 0:14])
        # cv2.imshow("idct", gt_4_j)
        # cv2.waitKey(0)
        gt_3_j = cv2.idct(dct_gt[0:7, 0:7])
        gt_2_j = cv2.idct(dct_gt[0:4, 0:4])
        gt_1_j = cv2.idct(dct_gt[0:2, 0:2])
        gt_4[idx, 0, :, :] = gt_4_j
        gt_3[idx, 0, :, :] = gt_3_j
        gt_2[idx, 0, :, :] = gt_2_j
        gt_1[idx, 0, :, :] = gt_1_j

        labels[idx] = labels_list[j]

        idx += 1
        if idx == batch_size:
            yield idx, gt_1, gt_2, gt_3, gt_4, image_clean, image_adv, labels
            image_clean = np.zeros(batch_shape_clean)
            image_adv = np.zeros(batch_shape_adv)
            gt_4 = np.zeros(
                [batch_shape_clean[0], batch_shape_clean[1], 14, 14])
            gt_3 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 7, 7])
            gt_2 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 4, 4])
            gt_1 = np.zeros([batch_shape_clean[0], batch_shape_clean[1], 2, 2])
            labels = np.zeros(batch_shape_clean[0])
            idx = 0

    if idx > 0:
        yield idx, gt_1, gt_2, gt_3, gt_4, image_clean, image_adv, labels
Пример #6
0
def analysis_pipeline(imgpath):
    """
    :param imgpath: path to input image
    :param block_size: block size
    :return: img_size: the image size in kilobytes
    imgs_dict: dict of quantization type and the corresponding (de)compression images
            i.e. {Quantization type: [dct image(compressed), idct image (decompressed)]

        size_dict: dict {Quantization type: [dct file (compressed) size, idct file (decompressed) size]
    """
    block_size = 8

    if not os.path.isdir("output"):
        os.makedirs("output")

    img_array = cv.imread(imgpath, cv.IMREAD_GRAYSCALE)
    img_size = get_img_size(img_array)
    if img_array is None:
        raise Exception("Image not found")
    # Dividing the image into blocks
    blocks = img2blocks(img_array, block_size)

    # Applying DCT
    dct_blocks = {
        block_idx: cv.dct(block_data)
        for block_idx, block_data in blocks.items()
    }
    dct_img = blocks2img(dct_blocks)

    # Decompression (no quantization)
    idct_img = blocks2img(
        {idx: cv.idct(block)
         for idx, block in dct_blocks.items()})

    # Quantization
    imgs_dict = {"None": [idct_img, dct_img]}

    for q in ["Q10", "Q50", "Q90"]:
        q_blocks = quantize(dct_blocks, globals()[q])
        q_dct_img = blocks2img(q_blocks)

        # Applying Inverse DCT (Decompression)
        q_idct_blocks = {
            idx: cv.idct(block)
            for idx, block in q_blocks.items()
        }
        q_idct_img = blocks2img(q_idct_blocks)

        imgs_dict[q] = (q_idct_img, q_dct_img)

    sizes_dict = get_sizes(copy.deepcopy(imgs_dict))
    return img_size, imgs_dict, sizes_dict
Пример #7
0
def DCTSplit(in_img, α):

    d_in_img = in_img.astype(float)
    d_in_img_t = cv2.dct(d_in_img)

    row = in_img.shape[0]
    cutoff = round(α * row)
    ### fliplr: flip array in left/right direction
    tr_high_t = np.fliplr(np.tril(np.fliplr(d_in_img_t), cutoff))
    tr_low_t = d_in_img_t - tr_high_t
    high = cv2.idct(tr_high_t)
    low = cv2.idct(tr_low_t)
    return low, high
Пример #8
0
def test_dct_transpose():
    image_fname = os.path.join(TEST_DATA_DIR, "Cover", "00002.jpg")
    image_0 = cv2.imread(image_fname, cv2.IMREAD_GRAYSCALE) / 255.0

    # f, ax = plt.subplots(1, 2)
    # ax[0].imshow(image_0, cmap="gray")
    # ax[1].imshow(idct8(dct2channels_last(dct2spatial(dct8(image_0)))), cmap="gray")
    # plt.show()

    dct_cv = dct2spatial(dct8(image_0))
    dct_cv = np.dstack([dct_cv, dct_cv, dct_cv])
    # dct_cv = compute_features(None, image_fname, [INPUT_FEATURES_DCT_KEY])[INPUT_FEATURES_DCT_KEY]

    image_0 = image_0[256:256 + 8, 256:256 + 8]
    image_1 = np.transpose(image_0)

    expected_dct88_0 = cv2.dct(image_0)
    expected_dct88_1 = cv2.dct(image_1)

    actual_dct88_1 = dct_transpose_block(expected_dct88_0)
    f, ax = plt.subplots(3, 2)
    ax[0, 0].imshow(image_0)
    ax[0, 1].imshow(image_1)

    ax[1, 0].imshow(cv2.idct(expected_dct88_0))
    ax[1, 1].imshow(cv2.idct(expected_dct88_1))

    ax[2, 0].imshow(cv2.idct(expected_dct88_0))
    ax[2, 1].imshow(cv2.idct(actual_dct88_1))
    f.show()

    # dct_000 = dct_rot90(dct, 1)
    dct_1 = dct_cv
    dct_2 = dct_transpose(dct_cv)

    f, ax = plt.subplots(1, 2, figsize=(16, 8))
    ax[0].imshow(idct8(dct2channels_last(dct_1[..., 0])), cmap="gray")
    ax[0].axis("off")
    ax[1].imshow(idct8(dct2channels_last(dct_2[..., 0])), cmap="gray")
    ax[1].axis("off")
    f.show()

    dct_1 = dct_cv
    dct_2 = dct_transpose_fast(dct_cv)

    f, ax = plt.subplots(1, 2, figsize=(16, 8))
    ax[0].imshow(idct8(dct2channels_last(dct_1[..., 0])), cmap="gray")
    ax[0].axis("off")
    ax[1].imshow(idct8(dct2channels_last(dct_2[..., 0])), cmap="gray")
    ax[1].axis("off")
    f.show()
Пример #9
0
def image_to_feature_maps(images_info, shape=None, codec='jpg'):
    '''
    images: array of jpeg images and it's matadata in batch
    shape:  rows and columns of feature maps allocation in jpeg image, 
            (a, b): a rows and b columns feature maps in one jpeg image
    '''
    batch_size = len(images_info)
    if not shape:
        print("Shape is not provided")
    rows, cols = shape
    feature_maps = np.zeros(shape=(batch_size, 78, 78, 128))
    cnt = 0
    decoded_img = 0
    for batch in range(batch_size):
        if codec == 'jpg':
            decoded_img = cv2.imdecode(images_info[batch][0], 0)
        elif codec == 'dct':
            decoded_img = cv2.idct(images_info[batch][0])
        elif codec == 'webp':
            decoded_img = cv2.imdecode(images_info[batch][0], 0)
        minmax_info = images_info[batch][1]
        tmp = np.zeros(shape=decoded_img.shape)
        decoded_img = cv2.normalize(decoded_img, tmp, minmax_info[0],
                                    minmax_info[1], cv2.NORM_MINMAX,
                                    cv2.CV_32FC1)
        tmp = np.vsplit(decoded_img, rows)
        for row_data in tmp:
            row_splitted = np.hsplit(row_data, cols)
            for f_map in row_splitted:
                feature_maps[batch, :, :, cnt] = f_map
                cnt += 1
        # assert cnt == 128
        cnt = 0
    return feature_maps
Пример #10
0
def DWT_DCT_SVD(coverImage, watermarkImage):
    coverImage = cv2.resize(coverImage, (512, 512))
    cv2.imshow('Cover Image', coverImage)
    watermarkImage = cv2.resize(watermarkImage, (256, 256))
    cv2.imshow('Watermark Image', watermarkImage)
    coverImage = np.float32(coverImage)

    coverImage /= 255
    coeff = pywt.dwt2(coverImage, 'haar')
    cA, (cH, cV, cD) = coeff

    watermarkImage = np.float32(watermarkImage)
    watermarkImage_dct = cv2.dct(watermarkImage)

    cA_dct = cv2.dct(cA)

    ua, sa, va = np.linalg.svd(cA_dct, full_matrices=1, compute_uv=1)
    uw, sw, vw = np.linalg.svd(watermarkImage, full_matrices=1, compute_uv=1)

    #Embedding
    alpha = 10
    sA = np.zeros((256, 256), np.uint8)
    sA[:256, :256] = np.diag(sa)
    sW = np.zeros((256, 256), np.uint8)
    sW[:256, :256] = np.diag(sW)
    W = sA + alpha * sW

    u1, w1, v1 = np.linalg.svd(W, full_matrices=1, compute_uv=1)
    ww = np.zeros((256, 256), np.uint8)
    ww[:256, :256] = np.diag(w1)
    Wmodi = np.matmul(ua, np.matmul(ww, va))

    widct = cv2.idct(Wmodi)
    watermarkedImage = pywt.idwt2((widct, (cH, cV, cD)), 'haar')
    cv2.imshow('watermarkedImage', watermarkedImage)
Пример #11
0
    def block_add_wm(self, block, index, i):

        i = i % (self.wm_shape[0] * self.wm_shape[1])

        wm_1 = self.wm_flatten[i]
        block_dct = cv2.dct(block)
        block_dct_flatten = block_dct.flatten().copy()

        block_dct_flatten = block_dct_flatten[index]
        block_dct_shuffled = block_dct_flatten.reshape(self.block_shape)
        U, s, V = np.linalg.svd(block_dct_shuffled)
        max_s = s[0]
        s[0] = (max_s - max_s % self.mod +
                3 / 4 * self.mod) if wm_1 >= 128 else (max_s -
                                                       max_s % self.mod +
                                                       1 / 4 * self.mod)
        if self.mod2:
            max_s = s[1]
            s[1] = (max_s - max_s % self.mod2 +
                    3 / 4 * self.mod2) if wm_1 >= 128 else (max_s -
                                                            max_s % self.mod2 +
                                                            1 / 4 * self.mod2)
        # s[1] = (max_s-max_s%self.mod2+3/4*self.mod2) if wm_1<128 else (max_s-max_s%self.mod2+1/4*self.mod2)

        # np.dot(U[:, :k], np.dot(np.diag(sigma[:k]), v[:k, :]))
        block_dct_shuffled = np.dot(U, np.dot(np.diag(s), V))

        block_dct_flatten = block_dct_shuffled.flatten()

        block_dct_flatten[index] = block_dct_flatten.copy()
        block_dct = block_dct_flatten.reshape(self.block_shape)

        return cv2.idct(block_dct)
Пример #12
0
    def inner_embed(self,B,signature):
        sig_size=np.int(np.sqrt(len(signature)))
        size = self.size
        
        #四个拐角最多嵌入四份,可用于检测边缘切割和旋转
        #  (0,0)    (0,w-32)
        #  (h-32,0)    (h-32,w-32)
        
        w,h = B.shape[:2]
        embed_pos = [(0,0)]
        if w > 2* sig_size*size  :
            embed_pos.append((w-sig_size*size,0))
        if h > 2* sig_size*size  :
            embed_pos.append((0,h-sig_size*size))
        if len(embed_pos) ==3:
            embed_pos.append((w-sig_size*size,h-sig_size*size))

        for x,y in embed_pos:
            for i in range(x,x+sig_size*size,size):
                for j in range(y,y+sig_size * size,size):
                    v = np.float32(B[i:i+size,j:j+size])
                    v = cv2.dct(v)
                    v[size-1,size-1] = self.Q*signature[((i-x)//size)*sig_size+(j-y)//size]
                    v = cv2.idct(v)
                    maxium = max(v.flatten())
                    minium = min(v.flatten())
                    if maxium > 255:
                        v = v - (maxium - 255)
                    if minium < 0:
                        v = v - minium
                    B[i:i+size,j:j+size]= v
        return B
Пример #13
0
    def _process_single_image(self, q_table):
        y, u, v = self.curr_image.convert("RGB").resize(
            (224, 224)).convert("YCbCr").split()

        channel = np.asarray(y)
        # split and shift
        blocks = split_88(channel) - 128
        # DCT
        dct_blocks = np.array([cv2.dct(block) for block in blocks])

        # --- These blocks are for storage and transmission--- #
        div_blocks = np.array(
            [np.round(dct_block / q_table) for dct_block in dct_blocks])

        scale_blocks = np.array(
            [div_block * q_table for div_block in div_blocks])
        idct_blocks = np.array(
            [cv2.idct(rec_block) for rec_block in scale_blocks])
        rec_blocks = np.clip(idct_blocks + 128, 0, 255)
        rec_channel = merge_88(rec_blocks.astype(np.uint8))

        # _u = u.resize((112, 112)).resize((224, 224))
        # _v = v.resize((112, 112)).resize((224, 224))

        _u = u
        _v = v

        rec_image = Image.merge("YCbCr",
                                [Image.fromarray(rec_channel), _u, _v])
        rec_image = rec_image.convert("RGB").resize(model_input_size)

        return np.asarray(rec_image).astype(np.float32)
Пример #14
0
def insert(path, txt):
    img = cv2.imread(path, cv2.IMREAD_ANYCOLOR)
    txt = "{}{}{}".format(len(txt), FLAG, txt)
    row, col = img.shape[:2]
    max_bytes = (row // 8) * (col // 8) // 8
    assert max_bytes >= len(txt), "Message overflow the capacity:{}".format(
        max_bytes)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
    # Just use the Y plane to store message, you can use all plane
    y, u, v = cv2.split(img)
    y = y.astype(np.float32)
    blocks = []
    # Quantize blocks
    for r_idx in range(0, 8 * (row // 8), 8):
        for c_idx in range(0, 8 * (col // 8), 8):
            quantized = cv2.dct(y[r_idx:r_idx + 8, c_idx:c_idx + 8]) / TABLE
            blocks.append(quantized)
    for idx in range(len(txt)):
        encode(blocks[idx * 8:(idx + 1) * 8], txt[idx])

    idx = 0
    # Restore Y plane
    for r_idx in range(0, 8 * (row // 8), 8):
        for c_idx in range(0, 8 * (col // 8), 8):
            y[r_idx:r_idx + 8, c_idx:c_idx + 8] = cv2.idct(blocks[idx] * TABLE)
            idx += 1
    y = y.astype(np.uint8)
    img = cv2.cvtColor(cv2.merge((y, u, v)), cv2.COLOR_YUV2BGR)
    filename, _ = osp.splitext(path)
    # DCT algorithm can save message even if jpg
    filename += '_dct_embeded' + '.jpg'
    cv2.imwrite(filename, img)
    return filename
Пример #15
0
 def idct_decoder(self, TransAllQuant_list, Q, blocksize=8):
     h, w = TransAllQuant_list[0].shape
     B = blocksize
     DecAll = np.zeros((h, w, 3), np.uint8)
     for idx, channel in enumerate(TransAllQuant_list):
         channelrows = channel.shape[0]
         channelcols = channel.shape[1]
         blocksV = int(channelrows / B)
         blocksH = int(channelcols / B)
         back0 = np.zeros((channelrows, channelcols), np.uint8)
         for row in range(blocksV):
             for col in range(blocksH):
                 dequantblock = channel[row * B:(row + 1) * B,
                                        col * B:(col + 1) * B] * Q[idx]
                 currentblock = np.round(
                     cv2.idct(dequantblock)
                 ) + 128  # inverse shiftign of the shift of the pixel values, sucht that their value range is [0,...,255].
                 currentblock[currentblock > 255] = 255
                 currentblock[currentblock < 0] = 0
                 back0[row * B:(row + 1) * B,
                       col * B:(col + 1) * B] = currentblock
         back1 = cv2.resize(
             back0, (w, h)
         )  # the subsampled chrominance channels are interpolated, using cv2.INTER_LINEAR in default.
         DecAll[:, :, idx] = np.round(back1)
     return DecAll
Пример #16
0
def get_dct(img):
    dct = cv2.dct(img)
    s = img.shape
    idct = np.zeros(s, dtype='float32')
    idct[:s[0] * 3 / 4, :s[1] * 3 / 4] = dct[:s[0] * 3 / 4, :s[1] * 3 / 4]
    idct = cv2.idct(idct)
    return np.square(idct - img).mean()
Пример #17
0
def jpegCompress(image, quantmatrix):
    '''
        Compress(imagefile, quanmatrix simulates the lossy compression of 
        baseline JPEG, by quantizing the DCT coefficients in 8x8 blocks
    '''
    # Return compressed image in result
    
    H = np.size(image, 0)
    W = np.size(image, 1)
    
    # Number of 8x8 blocks in the height and width directions
    h8, w8 = np.ceil((H / 8, W / 8)).astype('int')

    # TODO If not an integer number of blocks, pad it with zeros
    result = np.pad(image.astype('float'), ((0, h8 * 8 - H), (0, w8 * 8 - W), (0, 0)), 'constant', constant_values=(0, 0))

    # TODO Separate the result into blocks, and compress the blocks via
    # quantization DCT coefficients
    for i in range(0, h8 * 8, 8):
        for j in range(0, w8 * 8, 8):
            mat = np.round(np.divide(cv2.dct(result[i:i + 8, j:j + 8, 0] - 128), quantmatrix))
            result[i:i+8, j:j+8, :] = np.reshape(mat,(8,8,1))

    # TODO Convert back from DCT domain to RGB result
    for i in range(0, h8 * 8, 8):
        for j in range(0, w8 * 8, 8):
            mat = cv2.idct(np.multiply(result[i:i + 8, j:j + 8, 0], quantmatrix)) + 128
            result[i:i+8, j:j+8, :] = np.reshape(mat,(8,8,1))
    
    return result[:H, :W, :].astype('uint8')
Пример #18
0
def visualize_phash(cls, h):
    """可视化感知散列到微型二值图像。
    
    cls是cv2.img_hash中的类,如cv2.img_hash.averageHash。
    散列的计算方法参照OpenCV C++代码,本函数执行相反的操作。
    https://github.com/opencv/opencv_contrib/tree/4.x/modules/img_hash/src
    """
    if type(h) is str: h = bytes.fromhex(h)
    if type(h) is bytes: h = np.uint8(list(h))
    if cls is cv2.img_hash.averageHash:
        return np.unpackbits(h, bitorder="little").reshape(8, 8) * 255
    elif cls is cv2.img_hash.blockMeanHash:
        if len(h) == 32:
            # OpenCV实现的块均值散列省略了文档引用的论文中算法的加密,且所谓块“中位数”实为均值,使得它除了大一点以外,和均值散列没什么两样。
            return np.unpackbits(h, bitorder="little").reshape(16, 16) * 255
        else:
            return np.unpackbits(h, bitorder="little")[:-7].reshape(31,
                                                                    31) * 255
    elif cls is cv2.img_hash.pHash:
        return cv2.normalize(
            cv2.idct(
                np.pad(
                    np.unpackbits(h, bitorder="little").reshape(8, 8).astype(
                        np.float32) - np.float32(0.5), ((0, 24), ))), None, 0,
            255, cv2.NORM_MINMAX, cv2.CV_8U)
    raise ValueError("我没用过这种感知散列")
Пример #19
0
	def IDCTnDequantize(prop):
		#To dequantize
		img3 = prop.image
		iHeight, iWidth = img3.shape[:2]
		img2 = np.zeros((iHeight,iWidth,3), np.uint8)
		#print img2.dtype

		for startY in range(0, iHeight, 8):
			for startX in range(0, iWidth, 8):
				for c in range(0, 3):
					block = img3[startY:startY+8, startX:startX+8, c:c+1].reshape(8,8)
			   
					blockf = np.float32(block)	 # float conversion
					dst = cv2.idct(blockf)		 # inverse dct
					np.place(dst, dst>255.0, 255.0)	 # saturation
					np.place(dst, dst<0.0, 0.0)		 # grounding 
					block = np.uint8(np.around(dst)) 

					# store the results
					for y in range(8):
						for x in range(8):
							img2[startY+y, startX+x, c] = block[y, x]
		
		# convert to BGR
		img2 = cv2.cvtColor(img2, cv2.COLOR_YCR_CB2BGR)

		prop.image = img2
Пример #20
0
 def DCT_3(self, img):
     # basically the same as DCT2, but returns Y values from DCT coefs!
     return np.array([
         np.array([
             np.array([cv2.idct(block) for block in row]) for row in channel
         ]) for channel in img
     ])
Пример #21
0
def Step2_3DFiltering(_Similar_Bscs, _Similar_Imgs):
    '''
    *3D维纳变换的协同滤波
    *_similar_blocks:相似的一组block,这里是频域的表示
    *要将_similar_blocks第三维依次取出,然后作dct,在频域进行维纳滤波之后,再作反变换
    *返回的Wiener_wight用于后面Aggregation
    '''
    m_Shape = _Similar_Bscs.shape
    Wiener_wight = numpy.zeros((m_Shape[1], m_Shape[2]), dtype=float)

    for i in range(m_Shape[1]):
        for j in range(m_Shape[2]):
            tem_vector = _Similar_Bscs[:, i, j]
            tem_Vct_Trans = numpy.matrix(cv2.dct(tem_vector))
            Norm_2 = numpy.float64(tem_Vct_Trans.T * tem_Vct_Trans)
            m_weight = Norm_2/(Norm_2 + sigma**2)
            if m_weight != 0:
                Wiener_wight[i, j] = 1./(m_weight**2 * sigma**2)
            # else:
            #     Wiener_wight[i, j] = 10000
            tem_vector = _Similar_Imgs[:, i, j]
            tem_Vct_Trans = m_weight * cv2.dct(tem_vector)
            _Similar_Bscs[:, i, j] = cv2.idct(tem_Vct_Trans)[0]

    return _Similar_Bscs, Wiener_wight
Пример #22
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--root",
                        type=str,
                        required=False,
                        default="/home/debadri/Pictures")
    parser.add_argument("--path", type=str, required=True, default=None)
    args = parser.parse_args()
    path = os.path.join(args.root, args.path)
    try:
        img = cv2.imread(path, 0)
    except Error:
        print("Could not read", Error)
        quit()
    print("img shape = ", img.shape)
    #print("imf shape = ",imf.shape)
    # RGB to YCbCr

    # img_ycc = img.convert("YCbCr")
    # img_ycc = np.ndarray((img.size[1],img.size[0],3),'u1',img_ycc.tobytes())

    # DCT

    img1 = img.astype(float)  #-128.0
    imf = np.zeros(img.shape)
    for i in range(img.shape[0] // 8):
        for j in range(0, img.shape[1] // 8, 1):
            imf[i * 8:(i + 1) * 8,
                j * 8:(j + 1) * 8] = cv2.dct(img1[i * 8:(i + 1) * 8,
                                                  j * 8:(j + 1) * 8])

    # Quantization

    m = np.max(imf) * 0.01
    mask = abs(imf) >= m
    imf1 = imf * mask

    # Inverse DCT

    img2 = np.zeros(img.shape)
    for i in range(0, img.shape[0] // 8, 1):
        for j in range(0, img.shape[1] // 8, 1):
            img2[i * 8:(i + 1) * 8,
                 j * 8:(j + 1) * 8] = cv2.idct(imf1[i * 8:(i + 1) * 8,
                                                    j * 8:(j + 1) * 8])

    #img1+=128.0

    # cv2.imshow("Original",img)
    # cv2.imshow("DCT",imf)
    # cv2.imshow("Quantized",imf1)
    # cv2.imshow("Compressed",img2.astype(int))
    # #cv2.waitKey()
    # print("img1 = ",img1)
    # print("imf1 = ",imf1)
    # print("img2 = ",img2.astype(int).astype(float))
    cv2.imwrite(os.path.join(args.root, "img2.png"), img2.astype(int))
    cv2.imwrite(os.path.join(args.root, "imf.png"), imf1.astype(int))
    plt.imshow(np.hstack((img, imf, imf1, img2)), cmap='gray')
    plt.show()
def embed_watermark(image_path, watermark_string, embeded_image_path):
    image = cv2.imread(image_path, cv2.IMREAD_COLOR)

    image = cv2.cvtColor(image, cv2.COLOR_BGR2YCR_CB)
    img = image[:, :, 0]

    watermark = watermark_encode(watermark_string)
    iHeight, iWidth = img.shape

    countHeight = int(iHeight / 8)
    countWidth = int(iWidth / 8)

    # 初始化空矩阵保存量化结果
    img2 = np.empty(shape=(iHeight, iWidth))
    index = 0
    # 分块DCT
    for startY in range(0, countHeight * 8, 8):
        for startX in range(0, countWidth * 8, 8):
            block = img[startY:startY + 8, startX:startX + 8].reshape((8, 8))
            # 进行DCT
            blockf = np.float32(block)
            block_dct = cv2.dct(blockf)
            if index < len(watermark):
                embed_bit(int(watermark[index]), block_dct, STRENGTH)
                index += 1

            block_idct = cv2.idct(block_dct)

            for y in range(8):
                for x in range(8):
                    img[startY + y, startX + x] = block_idct[y, x]

    image = cv2.cvtColor(image, cv2.COLOR_YCR_CB2BGR)
    cv2.imwrite(embeded_image_path, image)
    def computeIDCT_Vid(self, dequantised_coef):
        st = time.time()
        rgb_frames = np.zeros((self.totalFrames, 3, 540, 960))
        iIndices = list(range(0, 540, 8))
        jIndices = list(range(0, 960, 8))
        iIndices[-1] = 540 - 8
        jIndices[-1] = 960 - 8

        print('Performing IDCT....')
        for frame in range(self.totalFrames):
            block_cntr = 0
            for i in iIndices:
                for j in jIndices:
                    #dequantised_block = dequantised_coef[8160*frame+block_cntr, 1:].reshape((3,8,8))
                    #rgb_frames[frame,:,i:i+8,j:j+8] = self.computeIDCT(dequantised_block)
                    for channel in range(3):
                        # rgb_frames[frame,channel,i:i+8,j:j+8] = dequantised_coef[8160*frame+block_cntr, 1:193].reshape(3,8,8)
                        rgb_frames[frame, channel, i:i + 8, j:j + 8] = np.clip(
                            cv2.idct(
                                dequantised_coef[8160 * frame + block_cntr,
                                                 (channel * 64) +
                                                 1:((channel + 1) * 64) +
                                                 1].reshape(8, 8)), 0, 255)
                    block_cntr = block_cntr + 1
        print('IDCT time ', time.time() - st)

        return rgb_frames
Пример #25
0
def create_block_frames(trans, path, blocksize=8, debug=False):
    """ This function creates a series of frames whereas each frame only contains one field
        from the DCT matrix.
    """
    B = blocksize
    channelrows, channelcols = trans.shape
    blocksV = channelrows / B
    blocksH = channelcols / B
    count = 1
    if debug:
        B = 2

    # create the mask. There is only one white pixel within each block.
    # serialize the block using a wave pattern
    for (x, y) in wave(B):
        blockmask = np.zeros((blocksize, blocksize), np.float32)
        blockmask[y][x] = 1.0
        mask = np.tile(blockmask, (blocksV, blocksH))
        # apply the mask to the image:
        trans_masked = np.multiply(trans, mask)
        # decode the 'image':
        back = np.zeros((channelrows, channelcols), np.uint8)
        for row in range(blocksV):
            for col in range(blocksH):
                dequantblock = trans_masked[row*blocksize:(row+1)*blocksize, col*blocksize:(col+1)*blocksize]
                currentblock = np.round(cv2.idct(dequantblock))+128
                currentblock[currentblock > 255] = 255
                currentblock[currentblock < 0] = 0
                back[row*blocksize:(row+1)*blocksize, col*blocksize:(col+1)*blocksize] = currentblock

        cv2.cv.SaveImage('%s/dct_%05d.png' % (path, count), cv2.cv.fromarray(back))
        count += 1
 def computeIDCT(self, block):
     dct_coef = self.CMPData[block, 1:].reshape((3, 8, 8))
     rgbBlock = np.zeros((3, 8, 8))
     for channel in range(3):
         rgbBlock[channel, :, :] = np.clip(
             cv2.idct(dct_coef[channel, :, :]), 0, 255)
     return rgbBlock
Пример #27
0
def layer_decode(byte_stream,
                 Q=Qy,
                 dc_func=huffman_to_ldc,
                 ac_func=huffman_to_lac,
                 alpha=1.0):
    # 解析比特流,获取bit编码
    bit_lists = sequence.sequence_decode(byte_stream,
                                         dc_func=dc_func,
                                         ac_func=ac_func)
    array_list = []
    # 计算量化系数
    Qa = np.maximum(Q * alpha, 1.0)
    for bit_list in bit_lists:
        # 对bit编码解码,获取rle编码
        rle_list = bit.bit_decode(bit_list)
        # 对rle编码进行解码,获取zigzag编码
        order = rle.rle_decode(rle_list)
        # 将zigzag型向量转为8*8矩阵
        array = zigzag.zigzag_decode(order)
        # 量化的逆操作
        inverse = quantization.inverse(array, Q=Qa)
        # 进行idct变换
        idct = cv2.idct(inverse)
        # 保存当前矩阵的计算结果
        array_list.append(idct)
    return array_list
Пример #28
0
def dct(data,block_size):
    m,n = data.shape  
    # block_size*block_size 的块
    hdata = np.vsplit(data,n//block_size) # 垂直分成高度度为block_size的块(按行分割)
    # 第i行
    dct = None
    idct = None
    for i in range(0, n//block_size):
        block_rows = None
        iblock_row = None
        blockdata = np.hsplit(hdata[i],m//block_size)  #(按列分割)
        for j in range(0, m//block_size): # 第i行,第j列个块
            block = cv2.dct(blockdata[j].astype(np.float))
            iblock = cv2.idct(block)
            if j == 0:
                block_rows = block
                iblock_rows = iblock
            else:
                block_rows = np.hstack((block_rows,block))  # 按列合并
                iblock_rows = np.hstack((iblock_rows,iblock))
        if i == 0:
            dct = block_rows
            idct = iblock_rows
        else:
            dct = np.vstack((dct,block_rows))  # 按行合并
            idct = np.vstack((idct,iblock_rows))
    return np.array(dct,dtype=np.uint8),np.array(idct,dtype=np.uint8)
Пример #29
0
    def decompress_algo(self, compressed_img):
        padded_img = np.zeros((self.image_h, self.image_w))
        # cv2.imshow("decompression padded image", compressed_img)
        # reshaped = np.reshape(compressed_img,(self.image_h, self.image_w))
        # cv2.imshow("reshaped", reshaped)
        for i in range(self.nbh):
            # Compute start and end row index of the block
            row_ind_1 = i * self.block_size
            row_ind_2 = row_ind_1 + self.block_size

            for j in range(self.nbw):
                # Compute start & end column index of the block
                col_ind_1 = j * self.block_size
                col_ind_2 = col_ind_1 + self.block_size
                block = (compressed_img[row_ind_1:row_ind_2,
                                        col_ind_1:col_ind_2])
                # apply 2D discrete cosine transform to the selected block

                block = self.inverse_zigzag(np.int8(block.flatten()),
                                            self.block_size, self.block_size)
                # print block
                de_quantize = np.multiply(block, self.quant)
                inverse_DCT = cv2.idct(de_quantize)
                # reshape the reorderd array back to (block size by block size) (here: 8-by-8)
                reshaped = np.reshape(inverse_DCT,
                                      (self.block_size, self.block_size))
                # cv2.imshow("img"+ str(i), reordered )
                # copy reshaped matrix into padded_img on current block corresponding indices
                padded_img[row_ind_1:row_ind_2, col_ind_1:col_ind_2] = reshaped

        # # clamping to  8-bit max-min values
        # padded_img[padded_img > 255] = 255
        # padded_img[padded_img < 0] = 0
        cv2.imshow("uncompressed", np.uint8(padded_img))
def decoder():
    padded_img = np.loadtxt('./encoded.txt')
    [h, w, block_size] = np.loadtxt('./size.txt')
    [H, W] = padded_img.shape

    nbh = math.ceil(h/block_size)
    nbh = np.int32(nbh)
    nbw = math.ceil(w/block_size)
    nbw = np.int32(nbw)

    for i in range(nbh):
        row_ind_1 = i*int(block_size)
        row_ind_2 = row_ind_1+int(block_size)
        for j in range(nbw):
            col_ind_1 = j*int(block_size)
            col_ind_2 = col_ind_1+int(block_size)
            block = padded_img[ row_ind_1 : row_ind_2 , col_ind_1 : col_ind_2 ]
            reshaped = np.reshape(block,(int(block_size)*int(block_size)))
            reordered = inverse_zigzag(reshaped, int(block_size), int(block_size))
            IDCT = cv2.idct(reordered)##### your code #####
            padded_img[ row_ind_1 : row_ind_2 , col_ind_1 : col_ind_2 ] = IDCT

    padded_img = np.uint8(padded_img)
    #cv2.imshow('decoded padded image', padded_img)

    decoded_img = padded_img[0:int(h),0:int(w)]
    #io.imshow(decoded_img)
    #io.show()
    plt.imshow(decoded_img,cmap='gray')
    plt.title('decode')
    plt.show()
Пример #31
0
def embed_watermark(image_array, watermark_string):

    watermark = watermark_encode(watermark_string)

    iHeight, iWidth = image_array.shape

    # img2 = np.empty(shape=(iHeight, iWidth))

    index = 0
    # 分块DCT
    for startY in range(0, iHeight, 8):
        for startX in range(0, iWidth, 8):
            block = image_array[startY:startY + 8, startX:startX + 8]

            # 进行DCT
            blockf = np.float32(block)
            block_dct = cv2.dct(blockf)

            if index < len(watermark):
                embed_bit(int(watermark[index]), block_dct, STRENGTH)
                index += 1

            block_idct = cv2.idct(block_dct)

            # store the result
            for y in range(8):
                for x in range(8):
                    image_array[startY + y, startX + x] = block_idct[y, x]
Пример #32
0
    def inner_embed(self, B: np.ndarray, signature):
        sig_size = self.sig_size
        size = self.size

        w, h = B.shape[:2]
        embed_pos = [(0, 0)]
        if w > 2 * sig_size * size:
            embed_pos.append((w - sig_size * size, 0))
        if h > 2 * sig_size * size:
            embed_pos.append((0, h - sig_size * size))
        if len(embed_pos) == 3:
            embed_pos.append((w - sig_size * size, h - sig_size * size))

        for x, y in embed_pos:
            for i in range(x, x + sig_size * size, size):
                for j in range(y, y + sig_size * size, size):
                    v = np.float32(B[i:i + size, j:j + size])
                    v = cv2.dct(v)
                    v[size-1, size-1] = self.Q * \
                        signature[((i-x)//size) * sig_size + (j-y)//size]
                    v = cv2.idct(v)
                    maximum = max(v.flatten())
                    minimum = min(v.flatten())
                    if maximum > 255:
                        v = v - (maximum - 255)
                    if minimum < 0:
                        v = v - minimum
                    B[i:i + size, j:j + size] = v
        return B
def enhance_image(data_tensor, ennet):
    n, c, h, w = data_tensor.size()
    data_numpy = data_tensor.cpu().numpy()
    dct_numpy = np.zeros_like(data_numpy)
    # dct
    for batch in range(n):
        for channel in range(c):
            for i in range(0, h, 10):
                for j in range(0, w, 10):
                    dct_numpy[batch, channel, i:i + 10,
                              j:j + 10] = cv2.dct(data_numpy[batch, channel,
                                                             i:i + 10,
                                                             j:j + 10])
    dct_tensor = torch.from_numpy(dct_numpy)
    dct_tensor = dct_tensor.cuda()
    enhance_dct_tensor = ennet(dct_tensor)
    enhance_dct_numpy = enhance_dct_tensor.cpu().detach().numpy()
    ret_numpy = np.zeros_like(data_numpy)
    # idct

    for batch in range(n):
        for channel in range(c):
            for i in range(0, h, 10):
                for j in range(0, w, 10):
                    ret_numpy[batch, channel, i:i + 10,
                              j:j + 10] = cv2.idct(enhance_dct_numpy[batch,
                                                                     channel,
                                                                     i:i + 10,
                                                                     j:j + 10])
    ret_tensor = torch.from_numpy(ret_numpy)
    return ret_tensor.cuda()
Пример #34
0
    def evaluate(self, x, g):
        """An in-memory evaluation callback."""
        # we want to return two things:
        # (1) the norm squared of the residuals, sum((Ax-b).^2), and
        # (2) the gradient 2*A'(Ax-b)

        # expand x columns-first
        x2 = x.reshape((self.nx, self.ny)).T

        # Ax is just the inverse 2D dct of x2
        Ax2 = idct(x2)

        # stack columns and extract samples
        Ax = Ax2.T.flat[self.ri].reshape(self.b.shape)

        # calculate the residual Ax-b and its 2-norm squared
        Axb = Ax - self.b
        fx = np.sum(np.power(Axb, 2))

        # project residual vector (k x 1) onto blank image (ny x nx)
        Axb2 = np.zeros(x2.shape)
        Axb2.T.flat[self.ri] = Axb  # fill columns-first

        # A'(Ax-b) is just the 2D dct of Axb2
        AtAxb2 = 2 * dct(Axb2)
        AtAxb = AtAxb2.T.reshape(x.shape)  # stack columns

        # copy over the gradient vector
        np.copyto(g, AtAxb)

        return fx
Пример #35
0
def idct2_nxn(i):
    out = np.empty_like(i)
    for x in range(out.shape[1]):
        for y in range(out.shape[0]):
            # out[y, x] = idct(idct(i[y, x], axis=1, norm='ortho'), axis=0, norm='ortho')
            out[y, x] = cv2.idct(i[y, x])
    return out
Пример #36
0
def jpeg_rate_distortion(dir_img, lamda):
    img = cv2.imread(dir_img, cv2.IMREAD_GRAYSCALE).astype(np.float) - 128.0
    img_shape = img.shape
    img_dct = np.zeros((int(img_shape[0] * img_shape[1] / (8 * 8)), 8, 8),
                       dtype=np.float)
    img_dct_all = np.zeros(img_shape)
    img_pitch = np.zeros((int(img_shape[0] * img_shape[1] / (8 * 8)), 8, 8),
                         dtype=np.float)
    img_recon = np.zeros(img_shape)
    t = 0
    lam = lamda
    num_bit = 0
    for i in range(0, img_shape[0], 8):
        for j in range(0, img_shape[1], 8):
            #         print(i,j)
            dct_1 = cv2.dct(img[i:i + 8, j:j + 8])
            #             print(rate_calculation(dct_1,lam)/64)
            num_bit += rate_calculation(dct_1, lam)
            img_pitch[t, 0:8, 0:8] = img[i:i + 8, j:j + 8]
            img_dct_all[i:i + 8, j:j + 8] = dct_1
            img_dct[t, 0:8, 0:8] = dct_1
            img_recon[i:i + 8, j:j + 8] = cv2.idct(
                np.round(dct_1 / (lam * quantization)) * (lam * quantization))
            t += 1
    bit_component = num_bit / (img.shape[0] * img.shape[1])
    rmse_1 = rmse(img, np.round(img_recon))
    return img, img_recon, bit_component, rmse_1
Пример #37
0
def idctC(img, size):
    # Do 8x8 IDCT on image
    i = 0
    IDCT = np.zeros((size[0], size[1]), np.float32)
    while i < size[0]:
        j = 0
        while j < size[1]:
            IDCT[i:i+8, j:j+8] = cv2.idct(np.float32(img[i:i+8, j:j+8]))
            j += 8
        i += 8
    IDCT = np.uint8(IDCT)
    return IDCT
Пример #38
0
def inverse_discrete_cosine_transform(dct_mtrx):
    rows = len(dct_mtrx)
    columns = len(dct_mtrx[0])

    divided_mtrx =[ [ [[]] for i in range(rows) ] for j in range(columns) ]
    for i in range(0, rows):
        for j in range(0, columns):
            idst = cv2.idct(dct_mtrx[i][j])*255
            img = np.uint8(idst)
            divided_mtrx[i][j] = img
    
    return divided_mtrx
Пример #39
0
def demo_quantize(full, colormap='flag'):
    FACTOR = 2  # will be squared
    trans = dct_demo(full, colormap)
    h, w = trans.shape
    plots = []

    #
    for i, (name, matrix) in enumerate(demo_matrices(h, w)):
        current = []
        mask = np.round(np.multiply(matrix, 255.0))
        mask[mask > 255] = 255
        mask[mask < 0] = 0
        current.append(mask)

        # get the dct image:
        trans_quantized = np.multiply(trans, matrix)
        current.append(trans_quantized)

        # recreate the original image
        gray = np.round(cv2.idct(trans_quantized)) + 128
        current.append(gray)
        current.append(name)

        plots.append(current)

    plt.figure(2)

    rows = len(plots)
    # import ipdb; ipdb.set_trace()
    for i, plot in enumerate(plots):

        # draw the matrix
        plt.subplot(rows, 4, ((4*i)+1))
        plt.imshow(plot[0], cmap='gray')
        plt.title(plot[3])

        # draw the dct
        plt.subplot(rows, 4, ((4*i)+2))
        plt.imshow(plot[1], cmap='RdGy')
        plt.colorbar()
        plt.subplot(rows, 4, ((4*i)+3))
        plt.imshow(plot[1], cmap=colormap)
        plt.colorbar()

        # draw the recreated image
        plt.subplot(rows, 4, ((4*i)+4))
        plt.imshow(plot[2], cmap='gray')

    plt.show()

    plt.close()
Пример #40
0
    def _idct_img(self, mat):
        """
        return inverse dct of specified matrix
        :param mat: np.ndarray of self.DCT_CONTAINER_TYPE
        :return: np.ndarray of self.GRAYSCALE_CONTAINER_TYPE
        """
        ret = np.zeros((self.size_h, self.size_w),
                       dtype=np.int16) # if np.uint8 was used, white dots will appear
        mat = mat.reshape((self.size_b_h, self.size_b_w, 8, 8))

        for i in range(self.size_b_h):
            for j in range(self.size_b_w):
                ret[i * 8:i * 8 + 8,
                j * 8:j * 8 + 8] = cv2.idct(mat[i, j]).round()

        return ret
Пример #41
0
def create_single_frame(path, number):
    """ This creates the animation frames using a single block.
        The frames are RGBA pngs.
    """
    trans = dct_demo(crop=False, asset='assets/zhaw-gray.png')
    blocksize = 64
    count = 1
    for (x, y) in wave(blocksize)[:number]:
        mask = np.zeros((blocksize, blocksize), np.float32)
        mask[y][x] = 1.0
        trans_masked = np.multiply(trans, mask)
        frame = np.round(cv2.idct(trans_masked))+128
        frame[frame > 255] = 255
        frame[frame < 0] = 0

        cv2.cv.SaveImage('%s/dct_%05d.png' % (path, count), cv2.cv.fromarray(frame))
        count += 1
def jpegCompress(image, quantmatrix):
    '''
        Compress(imagefile, quanmatrix simulates the lossy compression of 
        baseline JPEG, by quantizing the DCT coefficients in 8x8 blocks
    '''
    # Return compressed image in result
    
    H = np.size(image, 0)
    W = np.size(image, 1)

    # Number of 8x8 blocks in the height and width directions
    h8 = H / 8
    w8 = W / 8
    
    # TODO If not an integer number of blocks, pad it with zeros
    h8_t = h8 if H%8 is 0 else h8+1
    w8_t = w8 if W%8 is 0 else w8+1    
    im_t = np.zeros((h8_t*8,w8_t*8,1),dtype=np.float32)
    im_t[:H,:W,0] = im[:,:,0]*0.114+im[:,:,1]*0.587+im[:,:,2]*0.299
    # TODO Separate the image into blocks, and compress the blocks via quantization DCT coefficients
    DCT_arr = []
    for i in xrange(h8_t):
        list = []
        for j in xrange(w8_t):
            tmp = (cv2.dct(im_t[i*8:(i+1)*8,j*8:(j+1)*8,:])/quantmatrix+0.5)
            tmp = tmp.astype(np.uint8)
            tmp = tmp.astype(np.float32)
            list.append( tmp )
        DCT_arr.append(list)

    # TODO Convert back from DCT domain to RGB image
    for i in xrange(h8_t):
        for j in xrange(w8_t):
            im_t[i*8:(i+1)*8,j*8:(j+1)*8,0] = cv2.idct( DCT_arr[i][j] )
    
    result =np.zeros((h8_t*8,w8_t*8,3),dtype=np.float32)
    result[:,:,0] = result[:,:,1] = result[:,:,2] = im_t[:,:,0]
    result=result.astype(np.uint8)
    return result
Пример #43
0
def signature_saliency(img):
    """
    Signature Saliency.


    X. Hou, J. Harel, and C. Koch, "Image Signature: Highlighting Sparse Salient
    Regions." IEEE Trans. Pattern Anal. Mach. Intell. 34(1): 194-201 (2012)
    """
    old_shape = (img.shape[0],img.shape[1])
    img = img_padded_for_dct(img)
    img = img/255.0
    sal = []

    for c in range(img.shape[2]):
        channel = img[:,:,c].astype(np.dtype('float32'))
        channel_dct = np.sign(cv2.dct(channel))
        s = cv2.idct(channel_dct)
        s = np.multiply(s,s)
        sal.append(s)
    sal = sum(sal)/3.0
    sal = cv2.GaussianBlur(sal, (11,11), 0)
    sal = sal[:old_shape[0], :old_shape[1]]
    return sal
Пример #44
0
def dct_demo(filename, blocksize=8):
    B = blocksize
    fn3 = filename

    # The height and width of the blocks is B=8.
    # An image is imported and transformed to a grayscale image.
    # Moreover, the image is cropped, such that its height and width
    # is a multiple of the blocksize B.:
    img1 = cv2.imread(fn3, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    h, w = np.array(img1.shape[:2])/B * B
    print h
    print w
    img1 = img1[:h,:w]

    # For each block in the image the DCT is applied.
    # The transformed image is stored in the variable Trans
    # and it is saved to the file Transformed.jpg:

    blocksV = h/B
    blocksH = w/B
    vis0 = np.zeros((h, w), np.float32)
    Trans = np.zeros((h, w), np.float32)
    vis0[:h, :w] = img1
    for row in range(blocksV):
        for col in range(blocksH):
            currentblock = cv2.dct(vis0[row*B:(row+1)*B, col*B:(col+1)*B])
            Trans[row*B:(row+1)*B, col*B:(col+1)*B] = currentblock
    cv2.cv.SaveImage('Transformed.jpg', cv2.cv.fromarray(Trans))

    # Next, the user is asked to mark a region in the displayed image,
    # by clicking into the image. The block around the clicked position
    # is highlighted by a blue box:

    plt.imshow(img1, cmap="gray")
    point = plt.ginput(1)
    block = np.floor(np.array(point)/B) #first component is col, second component is row
    print block
    col = block[0,0]
    row = block[0,1]
    plt.plot([B*col, B*col+B, B*col+B, B*col, B*col],
             [B*row, B*row, B*row+B, B*row+B, B*row])
    plt.axis([0, w, h, 0])
    plt.title("Original Image")

    # The selected block and its DCT-transform are then plotted
    # into a second Matplotlib-figure:

    plt.figure()
    plt.subplot(1, 2, 1)
    selectedImg = img1[row*B:(row+1)*B, col*B:(col+1)*B]
    N255 = Normalize(0, 255) #Normalization object, used by imshow()
    plt.imshow(selectedImg, cmap="gray", norm=N255, interpolation='nearest')
    plt.title("Image in selected Region")

    plt.subplot(1, 2, 2)
    selectedTrans = Trans[row*B:(row+1)*B, col*B:(col+1)*B]
    plt.imshow(selectedTrans, cmap=cm.jet, interpolation='nearest')
    plt.colorbar(shrink=0.5)
    plt.title("DCT transform of selected Region")

    # Then the IDCT is applied to reconstruct the original image
    # from the transformed representation.
    # The reconstructed image is stored in the variable back0
    # and it is saved to the file BackTransformed.jpg:

    back0 = np.zeros((h, w), np.float32)
    for row in range(blocksV):
        for col in range(blocksH):
            currentblock = cv2.idct(Trans[row*B:(row+1)*B, col*B:(col+1)*B])
            back0[row*B:(row+1)*B, col*B:(col+1)*B] = currentblock
    cv2.cv.SaveImage('BackTransformed.jpg', cv2.cv.fromarray(back0))

    # In order to verify that DCT and IDCT are lossless,
    # the Mean Absolute Difference (MAD) between the original
    # and the reconstructed image is calculated and printed to the console.
    # The reconstructed image is plotted into a Matplotlib-figure.:

    diff = back0-img1
    print diff.max()
    print diff.min()
    print "Mean Absolute Difference: ", np.sum(np.abs(diff))/float(h*w)
    plt.figure()
    plt.imshow(back0, cmap="gray")
    plt.title("Backtransformed Image")
    plt.show()
Пример #45
0
orig=gray.copy()
gray = gray.astype('float32')
gray/=255
dct=cv2.dct(gray)

dctvis=cv2.normalize(np.log(dct.copy()),-1,0,1,cv2.NORM_MINMAX)
cv2.imshow('dct before',dctvis)

vr=1.#vertical ratio, how much percentage of vertical freq components should be thrown away
hr=.95#horizontal
dct[0:vr*dct.shape[0],0:hr*dct.shape[1]]=0

dctvis=cv2.normalize(np.sqrt(dct.copy()),-1,0,1,cv2.NORM_MINMAX)
cv2.imshow('dct after',dctvis)
gray=cv2.idct(dct)
gray=cv2.normalize(gray,-1,0,1,cv2.NORM_MINMAX)
gray*=255
gray=gray.astype('uint8')

cv2.imshow('low freq suppressed',gray)
gray=cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT,#smp.disk(7)
    cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,15)),
    iterations=1)
cv2.imshow('bring out black gaps',gray)
gray=cv2.morphologyEx(gray, cv2.MORPH_DILATE,#smp.disk(5), 
    cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(11,11)),
    iterations=1)
cv2.imshow('connect them together',gray)
gray=cv2.threshold(gray,0,255,cv2.THRESH_OTSU)[1]
cv2.imshow('auto thresh',gray)
Пример #46
0
def get_idct(*args, **kwargs):
    """ Performs the inverse discrete cosine
    transform """
    return cv2.idct(*args, **kwargs)
import cv2
import numpy as np

img = cv2.imread("D:\installer\opencv\sources\samples\data\lena.jpg")
o_img = img
yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV)
y,u,v = cv2.split(yuv_img)
#dct
block_size = 8
y = np.float32(y)
u = np.float32(u)
v = np.float32(v)
dct_y = cv2.dct(y)
dct_u = cv2.dct(u)
dct_v = cv2.dct(v)
#idct
y = cv2.idct(dct_y)
u = cv2.idct(dct_u)
v = cv2.idct(dct_v)
y = np.uint8(y)
u = np.uint8(u)
v = np.uint8(v)

yuv_img = cv2.merge([y,u,v])
img = cv2.cvtColor(yuv_img, cv2.COLOR_YUV2BGR)
cv2.imshow("lena", img-o_img)
cv2.waitKey(0)
Пример #48
0
img = cv2.imread(sys.argv[1])

print img.shape
rows, cols, d = img.shape
crow, ccol = rows/2 , cols/2
N = 4

dct_arr = np.zeros((rows,cols,3), np.uint8)
a = []
for i in xrange(3):
    imf = np.float32(img[:,:,i]) /255.0  # float conversion/scale
    dst = cv2.dct(imf)# the dct
    msk = np.zeros((rows, cols), np.uint8)
    msk[:N,:N] = 1
    dst = msk * dst
    print "Mask: ", dst[:N, :N]
    tmp = cv2.idct(dst);
    cv2.normalize(tmp,tmp,0,255,cv2.NORM_MINMAX)
    img2 = np.uint8(tmp)
    print tmp[299, 199], img2[299, 199]
    a.append(img2)
dct_arr = (np.dstack((a[0],a[1],a[2])) )

print "Byte spent: ", N * N * 3 * 4

plt.subplot(121)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.subplot(122)
plt.imshow(cv2.cvtColor(dct_arr, cv2.COLOR_BGR2RGB))
plt.show()
Пример #49
0
def decode_jpeg(code_list):
    img = np.zeros((8, 8, len(code_list)))
    z = []
    channel = 0
    # 解游程编码
    for code in code_list:
        temp = []
        for i in range(0, len(code), 2):
            for j in range(code[i]):
                temp.append(code[i + 1])
        z.append(temp)

    # 还原Z字形扫描
    for channel in range(len(code_list)):
        count = 0
        i = 0
        j = 0
        last_i = 0
        last_j = 0
        while True:
            img[i, j, channel] = z[channel][count]
            count += 1
            # 左上顶点
            if i == 0 and j == 0:
                last_i = i
                last_j = j
                j += 1
                continue
            # 右下顶点结束
            if i == img.shape[0] - 1 and j == img.shape[1] - 1:
                break
            # 上边界
            if i == 0:
                # 往左下
                if last_i == 0:
                    last_i = i
                    last_j = j
                    i += 1
                    j -= 1
                # 横向
                else:
                    last_i = i
                    last_j = j
                    j += 1
                continue
            # 下边界
            if i == img.shape[0] - 1:
                # 往右上
                if last_i == i:
                    last_i = i
                    last_j = j
                    j += 1
                    i -= 1
                # 横向
                else:
                    last_i = i
                    last_j = j
                    j += 1
                continue
            # 左边界
            if j == 0:
                # 往右上
                if last_j == 0:
                    last_i = i
                    last_j = j
                    j += 1
                    i -= 1
                # 纵向
                else:
                    last_i = i
                    last_j = j
                    i += 1
                continue
            # 右边界
            if j == img.shape[1] - 1:
                # 往左下
                if last_j == j:
                    last_i = i
                    last_j = j
                    i += 1
                    j -= 1
                # 纵向
                else:
                    last_i = i
                    last_j = j
                    i += 1
                continue

            # 左下
            if last_i == i - 1 and last_j == j + 1:
                last_i = i
                last_j = j
                i += 1
                j -= 1
                continue
            # 右上
            if last_i == i + 1 and last_j == j - 1:
                last_i = i
                last_j = j
                i -= 1
                j += 1

    # 逆DCT变换
    img[:, :, 0] = np.int32(img[:, :, 0] * lighttable)
    img[:, :, 1] = np.int32(img[:, :, 1] * colortable)
    img[:, :, 2] = np.int32(img[:, :, 2] * colortable)
    img = np.float64(img)
    for i in range(img.shape[2]):
        img[:, :, i] = np.uint8(cv2.idct(img[:, :, i]) + 128)
    return img
Пример #50
0
    def _run_(self):
        '''
        '''
        #print '- Running Mjpeg Decoder...'
        hf = h.HuffCoDec(self.hufftables)
        r, c, chnl = self.R, self.C, self.NCHNL


        if self.mode == '444':
            for ch in range(chnl):                #hufcd = self.fl.readline()[:-1]            #    print hufcd[0:20]
                nblk, seqrec = hf.invhuff(self.huffcodes[ch], ch)
                a, b = [0, 0]
                for i in range(self.nBlkRows):
                    for j in range(self.nBlkCols):
                
                        #    SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO
                        vec_index = int(a*(self.nBlkCols/2)+b)
                        # Quantization table
                        Z = 0
                        if len(self.motionVec[vec_index]) == 2:
                            Z = self.hvstables[abs(self.motionVec[vec_index][0])][abs(self.motionVec[vec_index][1])]
                        elif len(self.motionVec[vec_index]) == 3:
                            Z = self.hvstables[abs(self.motionVec[vec_index][1])][abs(self.motionVec[vec_index][2])]
                        else:
                            Z = self.hvstables[int((abs(self.motionVec[vec_index][1])+abs(self.motionVec[vec_index][3]))/2.)][int((abs(self.motionVec[vec_index][2])+abs(self.motionVec[vec_index][4]))/2.)]
      
                        blk = h.zagzig(seqrec[i*self.nBlkCols + j])
                        self.imRaw[r*i:r*i+r, c*j:c*j+c, ch] = np.round_( cv2.idct( blk*Z ))
                        if j%2 != 0:
                            b += 1
                    if i%2 != 0:																												
                        a += 1
                        b = 0
                    else:
                        b = 0
                        
        elif self.mode == '420':
            #import math as m
            if chnl == 1:
                rYmg = self.imRaw
            else:                #Y = self.imRaw[:,:,0]
                Y = np.zeros( (self.M, self.N) )
                dims, CrCb = h.adjImg( downsample(np.zeros( (self.M, self.N, 2) ), self.mode)[1] )
                rYmg = [ Y, CrCb[:,:,0], CrCb[:,:,1] ]
                
            for ch in range(chnl):
                a, b = [0, 0]
                if ch == 0:
                    rBLK = self.nBlkRows
                    cBLK = self.nBlkCols
                else:
                    rBLK, cBLK = int(np.floor(dims[0]/self.R)), int(np.floor(dims[1]/self.C))
            #    print hufcd[0:20]
                nblk, self.seqrec = hf.invhuff(self.huffcodes[ch], ch)
                for i in range(rBLK):
                    for j in range(cBLK):
                        
                
                        #    SELEÇÃO DA MATRIZ DE QUANTIZAÇÃO
                        vec_index = 0
                        if ch == 0: #LUMINANCIA
                            vec_index = int(a*(self.nBlkCols/2)+b)
                        else:       #CROMINANCIA
                            vec_index = int(a*(self.nBlkCols/2)+b)
                        # Quantization table
                        Z = 0
                        if len(self.motionVec[vec_index]) == 2:
                            Z = self.hvstables[abs(self.motionVec[vec_index][0])][abs(self.motionVec[vec_index][1])]
                        elif len(self.motionVec[vec_index]) == 3:
                            Z = self.hvstables[abs(self.motionVec[vec_index][1])][abs(self.motionVec[vec_index][2])]
                        else:
                            Z = self.hvstables[int((abs(self.motionVec[vec_index][1])+abs(self.motionVec[vec_index][3]))/2.)][int((abs(self.motionVec[vec_index][2])+abs(self.motionVec[vec_index][4]))/2.)]
                            
                        blk = h.zagzig(self.seqrec[i*cBLK + j])
                        #print rYmg[ch][r*i:r*i+r, c*j:c*j+c].shape, ch, i, j
                        rYmg[ch][r*i:r*i+r, c*j:c*j+c] = np.round_( cv2.idct( blk*Z ))
                        if ch == 0:
                            if j%2 != 0:
                                b += 1
                            else:
                                pass
                        else:
                            b += 1
                    if ch == 0:
                        if i%2 != 0:
                            a += 1
                            b = 0
                        else:
                            b = 0
                    else:
                        a += 1
                        b = 0
            # UPSAMPLE
            if chnl == 1:
                self.imRaw = rYmg #[:self.Mo, : self.No]
            else:
                self.imRaw[:,:,0] = rYmg[0]
                self.imRaw[:,:,1] = upsample(rYmg[1], self.mode)[:self.M, :self.N]
                self.imRaw[:,:,2] = upsample(rYmg[2], self.mode)[:self.M, :self.N]
        
        #self.fl.close()
        
#        imrec = cv2.cvtColor((self.imRaw[:self.Mo, :self.No]+128), cv2.COLOR_YCR_CB2BGR)
#        imrec = self.imRaw[:self.Mo, :self.No]+128
        imrec = self.imRaw+128.0
#        imrec[imrec>255.0]=255.0
#        imrec[imrec<0.0]=0.0
								
        #print 'Mjpeg Decoder Complete...'
        
        return imrec
def main():
	global p
	global mean_kernel
	# Preprocessing
	print "Preprocessing..."
	# Reading Core Images
	cap_image = cv2.imread("cap.bmp")
	lego_image = cv2.imread("lego.tif")

	# Grayscaling Images
	gray_cap_image = grayscale(cap_image)
	gray_lego_image = grayscale(lego_image)

	while True:
		print_choices()
		choice = raw_input()
		if choice == "1":
			cv2.imshow("Cap", cap_image)
			cv2.imshow("Lego", lego_image)
			print "waiting"
			wait()
			cv2.destroyAllWindows()
		elif choice == "2":
			cv2.imshow("Custom: Grayscale Cap", gray_cap_image)
			cv2.imshow("Custom: Grayscale Lego", gray_lego_image)
			cv2.imshow("OpenCV: Grayscale Cap", opencv_grayscale(cap_image))
			cv2.imshow("OpenCV: Grayscale Lego", opencv_grayscale(lego_image))
			wait()
			cv2.destroyAllWindows()
		elif choice == "3":
			print "Enter the value of probability p in decimal"
			p = float(raw_input())
			salt_pepper_cap_image = saltpepper_noise(gray_cap_image, p)
			cv2.imshow("Cap with salt pepper noise", salt_pepper_cap_image)
			# mean filtering
			mean_filtered_cap_image = convolute(salt_pepper_cap_image, mean_kernel)
			cv2.imshow("Custom: Mean Filtered Cap", mean_filtered_cap_image)
			# median filtering
			median_filtered_cap_image = median_filter(salt_pepper_cap_image)
			cv2.imshow("Custom: Median Filtered Cap", median_filtered_cap_image)
			
			# OpenCV
			# mean filtering
			mean_filtered_cap_image_opencv = cv2.blur(salt_pepper_cap_image, (3,3))
			cv2.imshow("OpenCV: Mean Filtered Cap", mean_filtered_cap_image_opencv)
			# median filtering
			median_filtered_cap_image_opencv = cv2.medianBlur(salt_pepper_cap_image, 3)
			cv2.imshow("OpenCV: Median Filtered Cap", median_filtered_cap_image_opencv)
			wait()
			cv2.destroyAllWindows()
		elif choice == "4":
			# Plot PSNR values
			# Custom
			psnr_plot = custom_psnr_plot_data(gray_cap_image)
			print psnr_plot
			# OpenCV
			psnr_plot_opencv = opencv_psnr_plot_data(gray_cap_image)
			print psnr_plot_opencv

			plt.plot(psnr_plot[:, 0], psnr_plot[:, 1], 'rs')
			plt.plot(psnr_plot[:, 0], psnr_plot[:, 2], 'bs')
			plt.plot(psnr_plot_opencv[:, 0], psnr_plot_opencv[:, 1], 'ro')
			plt.plot(psnr_plot_opencv[:, 0], psnr_plot_opencv[:, 2], 'bo')
			# Adjusting axis for better visibility
			plt.axis([0.09, 0.51, 15,35])
			plt.show()
		elif choice == "5":
			# Sobel	
			# Custom
			sobel_x_image, sobel_y_image, grad_image, edges_image = sobel_filter(gray_lego_image)
			cv2.imshow("Custom: X Gradient", sobel_x_image)
			cv2.imshow("Custom: Y Gradient", sobel_y_image)
			cv2.imshow("Custom: Gradient Magnitude", grad_image)
			cv2.imshow("Custom: Thresholded Edges", edges_image)
			# OpenCV
			sobel_x_image_opencv, sobel_y_image_opencv, grad_image_opencv, edges_image_opencv = opencv_sobel_filter(gray_lego_image)
			cv2.imshow("OpenCV: X Gradient", sobel_x_image_opencv)
			cv2.imshow("OpenCV: Y Gradient", sobel_y_image_opencv)
			cv2.imshow("OpenCV: Gradient Magnitude", grad_image_opencv)
			cv2.imshow("OpenCV: Thresholded Edges", edges_image_opencv)
			wait()
			cv2.destroyAllWindows()
		elif choice == "6":
			# DCT and Inverse DCT
			# Custom
			dct_matrix = dct(gray_cap_image)
			idct_image = idct(dct_matrix)
			cv2.imshow("Custom: DCT", dct_matrix.astype(np.uint8))
			cv2.imshow("Custom: IDCT", idct_image)
			save(idct_image, "inverse.png")
			# OpenCV
			floating_point_img = np.float32(gray_cap_image)/255.0
			dct_matrix_opencv = cv2.dct(floating_point_img)
			idct_image_opencv = cv2.idct(dct_matrix_opencv)
			cv2.imshow("OpenCV: DCT", dct_matrix_opencv.astype(np.uint8))
			cv2.imshow("OpenCV: IDCT", idct_image_opencv)
			wait()
			cv2.destroyAllWindows()
		elif choice == "7":
			# Harris Corner
			# Custom
			harris_corners_image = harris_corner(gray_lego_image)
			cv2.imshow("Custom: Harris Corner", harris_corners_image)
			# OpenCV
			harris_corners_image_opencv = opencv_harris_corner(gray_lego_image)
			cv2.imshow("OpenCV: Harris Corner", harris_corners_image_opencv)
			wait()
			cv2.destroyAllWindows()
		elif choice == "8":
			# Cleanup all windows
			cv2.destroyAllWindows()
Пример #52
0
def menu():
	print "-------------------------------------------------------------------"
	print "Image Processing Primer"
	print "-------------------------------------------------------------------"
	print "Choose an option:"
	print "1. Read and display both images"
	print "2. Convert both images to grayscale and display"
	print "3. Add salt and pepper noise and perform median/mean filtering"
	print "4. Plot PSNR vs. P for median and mean filters"
	print "5. Sobel on lego.tif"
	print "6. Get Edges on lego.tif and Threshold (First run 5 to get sobel gradients and save to file)"
	print "7. DCT and iDCT on cap.bmp"
	print "8. Corner Harris on lego.tif"
	print "9. Find Corners with Thresholding on lego.tif (First run 8 to get R matrix and save to file)"
	print "10. Grayscale with standard library function and compare"
	print "11. Median/Mean filtering with standard library function and compare"
	print "12. Plot PSNR vs. P for median and mean filters with opencv functions"
	print "13. PSNR vs. P plot with standard library function and compare"
	print "14. Sobel Edges with standard library function and compare"
	print "15. DCT and iDCT with standard library function and compare"
	print "16. Corner Harris with standard library function and compare"
	print "-------------------------------------------------------------------"
	print("Enter your choice:"),
	choice=raw_input()
	font = cv2.FONT_HERSHEY_SIMPLEX

	if choice=="1":
		read_and_display()
	elif choice=="2":
		convert_to_grayscale()
	elif choice=="3":
		add_and_remove_salt_and_pepper()
	elif choice=="4":
		psnr_p_plot()
	elif choice=="5":
		sobel()
	elif choice=="6":
		get_edges()
	elif choice=="7":
		dct_and_idct()
	elif choice=="8":
		corner_harris()
	elif choice=="9":
		find_corners()
	elif choice=="10":
		image1=cv2.imread("lego.tif", cv2.IMREAD_UNCHANGED)
		image2=cv2.imread("cap.bmp")
		image1_gray=gray(image1)
		image2_gray=gray(image2)

		image1_gray_opencv=cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY)
		image2_gray_opencv=cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY)

		print "PSNR For Lego.tif : "+str(get_psnr(image1_gray, image1_gray_opencv))
		print "PSNR For cap.bmp : "+str(get_psnr(image2_gray, image2_gray_opencv))

		cv2.imshow("Lego.tif", image1_gray)
		cv2.imshow("Cap.bmp", image2_gray)
		cv2.imshow("Lego.tif - OpenCV", image1_gray_opencv)
		cv2.imshow("Cap.bmp - OpenCV", image2_gray_opencv)
		cv2.waitKey()

	elif choice=="11":
		image=cv2.imread("cap.bmp")
		image=gray(image)
		h, w=image.shape
		noisy_image=image.copy()
		print "Enter the probability p used for adding noise:",
		p=float(raw_input())

		if p>1:
			print "Probability should be less than 1. Program exiting......."
		else :

			noisy_image=salt_and_pepper(image, p)
			median_filtered_image, mean_filtered_image=median_mean_filter(noisy_image)
			median_filtered_image_opencv=cv2.medianBlur(noisy_image, 3)
			mean_filtered_image_opencv=cv2.blur(noisy_image, (3,3))
			# print "PSNR for Median Filter : "+str(get_psnr(median_filtered_image, median_filtered_image_opencv))
			# print "PSNR for Mean Filter : "+str(get_psnr(mean_filtered_image, mean_filtered_image_opencv)) 
			cv2.putText(image,'Original',(10,500), font, 2,255,2)
			cv2.putText(noisy_image,'Original with Salt and Pepper Noise',(10,500), font, 2,255,2)
			cv2.putText(median_filtered_image,'Median Filter',(10,500), font, 2,255,2)
			cv2.putText(mean_filtered_image,'Mean Filter',(10,500), font, 2,255,2)
			cv2.putText(median_filtered_image_opencv,'Median Filter Opencv',(10,500), font, 2,255,2)
			cv2.putText(mean_filtered_image_opencv,'Mean Filter Opencv',(10,500), font, 2,255,2)

			merged=np.zeros((h*2, w*3), np.uint8)
			merged[0:h, 0:w]=image
			merged[h:2*h, 0:w]=noisy_image
			merged[0:h, w:2*w]=median_filtered_image
			merged[h:2*h, w:2*w]=mean_filtered_image
			merged[0:h, 2*w:3*w]=median_filtered_image_opencv
			merged[h:2*h, 2*w:3*w]=mean_filtered_image_opencv
			merged=cv2.resize(merged, (w*3/2, h))
			cv2.imshow("Output", merged)
			cv2.waitKey()

	elif choice=="12":
		psnr_p_plot_opencv()

	elif choice=="13":
		psnr_p=np.load("PSNR.npy")
		psnr_p_opencv=np.load("PSNR_Opencv.npy")

		plt.plot(psnr_p[:, 0], psnr_p[:, 1], 'ro')
		plt.plot(psnr_p[:, 0], psnr_p[:, 2], 'bo')
		plt.plot(psnr_p_opencv[:, 0], psnr_p_opencv[:, 1], 'rs')
		plt.plot(psnr_p_opencv[:, 0], psnr_p_opencv[:, 2], 'bs')
		plt.axis([0.09, 0.21, 15,35])
		plt.show()

	elif choice=="14":
		image=cv2.imread("lego.tif", cv2.IMREAD_UNCHANGED)
		image=gray(image)
		gradient_mag=np.load("Gradient Magnitude.npy")
		sobelx = cv2.Sobel(image,cv2.CV_64F,1,0,ksize=3)
		sobely = cv2.Sobel(image,cv2.CV_64F,0,1,ksize=3)
		gradient_mag_opencv=(sobelx**2 + sobely**2)**0.5
		h, w=gradient_mag.shape
		cv2.namedWindow("Edges")
		cv2.createTrackbar("Threshold", "Edges", 0, 255, nothing)
		merged=np.zeros((h, 2*w))
		c=0
		while c!=27:
			thresh=cv2.getTrackbarPos("Threshold", "Edges")
			edges=np.greater(gradient_mag, thresh).astype(np.uint8)*255
			edges_opencv=np.greater(gradient_mag_opencv, thresh).astype(np.uint8)*255
			cv2.putText(edges,'Edges',(10,500), font, 2, 255,2)
			cv2.putText(edges_opencv,'Edges Opencv',(10,500), font, 2, 255,2)
			merged[:, 0:w]=edges
			merged[:, w:2*w]=edges_opencv
			cv2.imshow("Edges", cv2.resize(merged, (w, h/2)))
			c=cv2.waitKey(33)
			if c==27:
				break

	elif choice=="15":
		image=cv2.imread("cap.bmp", cv2.IMREAD_UNCHANGED)
		height, width=image.shape
		N=8
		idct_image=np.load("iDCT_IMAGE.npy")
		idct_image_norm=(idct_image-np.amin(idct_image))/(np.amax(idct_image)-np.amin(idct_image))
		idct_image_norm*=255
		idct_image_norm=idct_image_norm.astype(np.uint8)
		imf = np.float32(image)/255.0
		dct_opencv = imf.copy()
		idct_opencv = imf.copy()
		# dct_opencv=cv2.dct(imf)
		# idct_opencv=cv2.idct(dct_opencv)
		i=0
		while i <= height - N:
			j = 0
			while j <= width - N:
				dct_opencv[i:i+N, j:j+N]=cv2.dct(imf[i:i+N, j:j+N])
				j += N
			i += N
		print dct_opencv.dtype
		
		i=0
		while i <= height - N:
			j = 0
			while j <= width - N:
				idct_opencv[i:i+N, j:j+N]=cv2.idct(dct_opencv[i:i+N, j:j+N])
				j += N
			i += N

		idct_opencv = (idct_opencv-np.amin(idct_opencv))*255/(np.amax(idct_opencv)-np.amin(idct_opencv))

		idct_opencv = idct_opencv.astype(np.uint8)

		cv2.imshow("IDCT", idct_image_norm)
		cv2.imshow("IDCT Opencv", idct_opencv)
		cv2.waitKey()

	elif choice=="16":
		image=cv2.imread("lego.tif", cv2.IMREAD_UNCHANGED)
		image=gray(image)
		h,w=image.shape
		r_opencv=np.zeros(image.shape)
		r=np.load("R.npy")
		c=0
		r = local(r, np.amax(r)/500)
		corners = draw(image, r)

		grayscale = np.float32(image)
		corners_opencv = cv2.cornerHarris(grayscale,2,3,0.06)

		corners_opencv = cv2.dilate(corners_opencv,None)

		r_opencv[corners_opencv>0.01*corners_opencv.max()]=1
		corners_opencv=draw(image, r_opencv)

		cv2.putText(corners,'Corners',(10,500), font, 2, (255,0,0),2)
		cv2.putText(corners_opencv,'Corners Opencv',(10,500), font, 2, (255,0,0),2)
		merged=np.zeros((h,2*w,3))
		merged[0:h, 0:w, :]=corners
		merged[0:h, w:2*w, :]=corners_opencv
		merged=cv2.resize(merged, (w, h/2)).astype(np.uint8)
		cv2.imshow("Corners", merged)
		cv2.waitKey()

	else :
		print "Wrong Input. Program Exiting...."
Пример #53
0
def jpeg_demo(filename, blocksize=8):
    # if you change the blocksize you have to supply different quantization matrices.
    # Step 1: Read and display image:
    # -------------------------------
    # In OpenCV color images are imported as BGR.
    # In order to display the image with pyplot.imshow() the channels must be switched
    # such that the order is RGB.:

    B = blocksize # blocksize (In Jpeg the blocksize is 8.
    img1 = cv2.imread(filename, cv2.CV_LOAD_IMAGE_UNCHANGED)
    h, w = np.array(img1.shape[:2])/B * B
    img1 = img1[:h, :w]
    #Convert BGR to RGB
    img2 = np.zeros(img1.shape, np.uint8)
    img2[:,:,0] = img1[:,:,2]
    img2[:,:,1] = img1[:,:,1]
    img2[:,:,2] = img1[:,:,0]
    plt.imshow(img2)

    # The user is asked to click into the image.
    # The clicked block is highlighted in the image (blue rectangle).
    # For the clicked block the DCT coefficients will be displayed later:

    point = plt.ginput(1)
    block = np.floor(np.array(point)/B) #first component is col, second component is row
    print "Coordinates of selected block: ", block
    scol = block[0, 0]
    srow = block[0, 1]
    plt.plot([B*scol, B*scol+B, B*scol+B, B*scol, B*scol],
             [B*srow, B*srow, B*srow+B, B*srow+B, B*srow])
    plt.axis([0, w, h, 0])

    # Step 2: Transform BGR to YCrCb and Subsample Chrominance Channels
    # -----------------------------------------------------------------
    # Using OpenCV the color space transform is implemented by:
    transcol = cv2.cvtColor(img1, cv2.cv.CV_BGR2YCrCb)

    # Next, the chrominance channels Cr and Cb are subsampled.
    # The constant SSH defines the subsampling factor in horicontal direction,
    # SSV defines the vertical subsampling factor.
    # Before subsampling the chrominance channels are filtered using a (2x2) box filter
    # (=average filter). After subsampling all 3 channels are stored in the list imSub:
    SSV = 2
    SSH = 2
    crf = cv2.boxFilter(transcol[:,:,1], ddepth=-1, ksize=(2, 2))
    cbf = cv2.boxFilter(transcol[:,:,2], ddepth=-1, ksize=(2, 2))
    crsub = crf[::SSV, ::SSH]
    cbsub = cbf[::SSV, ::SSH]
    imSub = [transcol[:,:,0], crsub, cbsub]

    # Step 3 and 4: Discrete Cosinus Transform and Quantisation
    # ---------------------------------------------------------


    # In order to provide different quality levels a quality factor QF can be selected.
    # From the quality factor the scale parameter is calculated.
    # The matrices defined above are multiplied by the scale parameter.
    # A low quality factor implies are large scale parameter.
    # A large scale parameter yields a coarse quantisation, a low quality but a
    # high compression rate. The list Q contains the 3 scaled quantisation matrices,
    # which will be applied to the DCT coefficients:

    QF = 99.0

    scale = get_scale(QF)

    Q = [QY*scale, QC*scale, QC*scale]

    # In the following loop DCT and quantisation is performed channel by channel.
    # As defined in the standard, before DCT the pixel values of all channels are
    # shifted by -128, such that the new value range is [-128,...127].
    # The loop iterates over the imSub-list, which contains the 3 channels.
    # The result of the DCTs of the 3 channels are stored in 2-dimensional numpy arrays,
    # which are put into the python list TransAll.
    # Similarly the quantized DCT coefficients are stored in 2-dimensional numpy arrays,
    # which are assigned to the python list TransAllQuant.
    # Note that the channels have different shapes due to chrominance subsampling.
    # In the last part of the loop the DCT coefficients of the previously selected
    # and highlighted block are displayed:

    TransAll = []
    TransAllQuant = []
    ch = ['Y', 'Cr', 'Cb']
    plt.figure()
    for idx, channel in enumerate(imSub):
        plt.subplot(1, 3, idx+1)
        channelrows = channel.shape[0]
        channelcols = channel.shape[1]
        Trans = np.zeros((channelrows, channelcols), np.float32)
        TransQuant = np.zeros((channelrows, channelcols), np.float32)
        blocksV = channelrows/B
        blocksH = channelcols/B
        vis0 = np.zeros((channelrows, channelcols), np.float32)
        vis0[:channelrows, :channelcols] = channel
        vis0 = vis0-128
        for row in range(blocksV):
            for col in range(blocksH):
                currentblock = cv2.dct(vis0[row*B:(row+1)*B, col*B:(col+1)*B])
                Trans[row*B:(row+1)*B, col*B:(col+1)*B] = currentblock
                TransQuant[row*B:(row+1)*B, col*B:(col+1)*B] = np.round(currentblock/Q[idx])
        TransAll.append(Trans)
        TransAllQuant.append(TransQuant)
        if idx == 0:
            selectedTrans = Trans[srow*B:(srow+1)*B, scol*B:(scol+1)*B]
        else:
            sr = np.floor(srow/SSV)
            sc = np.floor(scol/SSV)
            selectedTrans = Trans[sr*B:(sr+1)*B,sc*B:(sc+1)*B]
        plt.imshow(selectedTrans, cmap=cm.jet, interpolation='nearest')
        plt.colorbar(shrink=0.5)
        plt.title('DCT of '+ch[idx])

    # Decoding
    # --------
    # In order to show the quality decrease caused by the Jpeg coding the 4 steps
    # performed above are inverted in order to decode the image.
    # In the following loop for each channel first the inverse quantisation is performed,
    # followed by the inverse dct and the inverse shifting of the shift
    # of the pixel values, sucht that their value range is [0,...,255].
    # Moreover the subsampled chrominance channels are interpolated,
    # using the opencv method resize() such that their original size is reconstructed.:

    DecAll = np.zeros((h, w, 3), np.uint8)
    for idx, channel in enumerate(TransAllQuant):
        channelrows = channel.shape[0]  # 320 / 160
        channelcols = channel.shape[1]  # 240 / 120
        blocksV = channelrows/B         # 40
        blocksH = channelcols/B         # 30
        back0 = np.zeros((channelrows, channelcols), np.uint8)
        for row in range(blocksV):
            for col in range(blocksH):
                dequantblock = channel[row*B:(row+1)*B, col*B:(col+1)*B]*Q[idx]
                currentblock = np.round(cv2.idct(dequantblock))+128
                currentblock[currentblock > 255] = 255
                currentblock[currentblock < 0] = 0
                back0[row*B:(row+1)*B, col*B:(col+1)*B] = currentblock
        back1 = cv2.resize(back0, (w, h))
        DecAll[:,:,idx] = np.round(back1)

    # The 3-dimensional numpy-array DecAll contains the decoded YCrCb image.
    # This image is backtransformed to BGR, stored to a file and displayed in a pyplot
    # figure. Finally the Sum of squared error (SSE) between the original image
    # and the decoded image is calculated:

    reImg = cv2.cvtColor(DecAll, cv2.cv.CV_YCrCb2BGR)
    cv2.cv.SaveImage('BackTransformedQuant.jpg', cv2.cv.fromarray(reImg))
    plt.figure()
    img3 = np.zeros(img1.shape, np.uint8)
    img3[:,:,0] = reImg[:,:,2]
    img3[:,:,1] = reImg[:,:,1]
    img3[:,:,2] = reImg[:,:,0]
    plt.imshow(img3)
    SSE = np.sqrt(np.sum((img2-img3)**2))
    print "Sum of squared error: ", SSE
    plt.show()
def DCT(imgfile):
    
    img = cv2.imread(imgfile, 0)
    wndName = "original grayscale"
    cv2.namedWindow(wndName, cv2.WINDOW_AUTOSIZE)
    cv2.imshow(wndName, img)

    iHeight, iWidth = img.shape[:2]
    print "size:", iWidth, "x", iHeight

    # set size to multiply of 8
    if (iWidth % 8) != 0:
        filler = img[:,iWidth-1:]
        #print "width filler size", filler.shape
        for i in range(8 - (iWidth % 8)):
            img = np.append(img, filler, 1)

    if (iHeight % 8) != 0:
        filler = img[iHeight-1:,:]
        #print "height filler size", filler.shape
        for i in range(8 - (iHeight % 8)):
            img = np.append(img, filler, 0)

    iHeight, iWidth = img.shape[:2]
    print "new size:", iWidth, "x", iHeight

    # array as storage for DCT + quant.result
    img2 = np.empty(shape=(iHeight, iWidth))

    # FORWARD ----------------
    # do calc. for each 8x8 non-overlapping blocks
    for startY in range(0, iHeight, 8):
        for startX in range(0, iWidth, 8):
            block = img[startY:startY+8, startX:startX+8]

            # apply DCT for a block
            blockf = np.float32(block)     # float conversion
            
            dst = cv2.dct(blockf)          # dct
                
            # quantization of the DCT coefficients
            blockq = np.around(np.divide(dst, std_luminance_quant_tbl))
            blockq = np.multiply(blockq, std_luminance_quant_tbl)

            # store the result
            for y in range(8):
                for x in range(8):
                    img2[startY+y, startX+x] = blockq[y, x]

    
    # INVERSE ----------------
    for startY in range(0, iHeight, 8):
        for startX in range(0, iWidth, 8):
            block = img2[startY:startY+8, startX:startX+8]
            
            blockf = np.float32(block)     # float conversion
            dst = cv2.idct(blockf)         # inverse dct
            np.place(dst, dst>255.0, 255.0)     # saturation
            np.place(dst, dst<0.0, 0.0)         # grounding 
            block = np.uint8(np.around(dst))

            # store the results
            for y in range(8):
                for x in range(8):
                    img[startY+y, startX+x] = block[y, x]


    wndName1 = "DCT + quantization + inverseDCT"
    cv2.namedWindow(wndName1, cv2.WINDOW_AUTOSIZE)
    cv2.imshow(wndName1, img)
    cv2.imshow('DCT transformed Image',img2)
    cv2.waitKey(0) 
    cv2.destroyWindow('DCT transformed Image')    
    cv2.destroyWindow(wndName1)
    cv2.destroyWindow(wndName)