Python cv2.dct() Examples

The following are 9 code examples of cv2.dct(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module cv2 , or try the search function .
Example #1
Source File: BlindWatermark.py    From BlindWatermark with GNU General Public License v3.0 7 votes vote down vote up
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) 
Example #2
Source File: analyze.py    From rep0st with MIT License 7 votes vote down vote up
def analyze(self, images):
        image = images['gray']
        scaled = cv2.resize(image, (self.img_size, self.img_size), interpolation=cv2.INTER_AREA)
        scaled = numpy.float32(scaled)
        dct = cv2.dct(scaled)
        dctlowfreq = dct[:self.hash_size, :self.hash_size]
        med = numpy.median(dctlowfreq)
        diff = dctlowfreq > med
        return numpy.packbits(numpy.uint8(diff.reshape(-1, 1))) 
Example #3
Source File: lbp.py    From CGvsPhoto with MIT License 7 votes vote down vote up
def compute_jpeg_coef(image): 

	height = image.shape[1]
	width = image.shape[0]
	nb_channels = image.shape[2]
	result = np.zeros([8*int(width/8), 8*int(height/8), nb_channels], dtype = np.float32)

	for c in range(nb_channels):
		for i in range(int(width/8)):
			for j in range(int(height/8)): 
				result[8*i:8*(i+1), 8*j:8*(j+1), c] = np.round(cv2.dct(np.float32((image[8*i:8*(i+1), 8*j:8*(j+1), c])-128)))
				# print(np.max(result[8*i:8*(i+1), 8*j:8*(j+1), c]))
	return(result) 
Example #4
Source File: dct.py    From pyVSR with GNU General Public License v3.0 6 votes vote down vote up
def _compute_frame_dct(data):

    num_frames = data.shape[0]
    num_channels = data.shape[-1]

    dct = np.zeros(shape=data.shape[:-1])

    for i in range(num_frames):
        frame = data[i, :]
        if num_channels == 3:
            gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        else:
            gray = np.squeeze(frame)

        dct[i, :, :] = cv2.dct(gray)

    return dct 
Example #5
Source File: blur_detector.py    From image_utility with MIT License 6 votes vote down vote up
def get_blurness(self, image, block_size=8):
        """Estimate the blurness of an image.
        Args:
            image: image as a numpy array of shape [height, width, channels].
            block_size: the size of the minimal DCT block size.
        Returns:
            a float value represents the blurness.
        """
        # A 2D histogram.
        hist = np.zeros((8, 8), dtype=int)

        # Only the illumination is considered in blur.
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # Split the image into patches and do DCT on the image patch.
        height, width = image.shape
        round_v = int(height / block_size)
        round_h = int(width / block_size)
        for v in range(round_v):
            for h in range(round_h):
                v_start = v * block_size
                v_end = v_start + block_size
                h_start = h * block_size
                h_end = h_start + block_size

                image_patch = image[v_start:v_end, h_start:h_end]
                image_patch = np.float32(image_patch)
                patch_spectrum = cv2.dct(image_patch)
                patch_none_zero = np.abs(patch_spectrum) > self.dct_threshold
                hist += patch_none_zero.astype(int)

        _blur = hist < self.max_hist * hist[0, 0]
        _blur = (np.multiply(_blur.astype(int), self.hist_weight)).sum()
        return _blur/self.weight_total 
Example #6
Source File: image_processing.py    From DeepMosaics with GNU General Public License v3.0 6 votes vote down vote up
def block_dct_and_idct(g,QQF):
    T = cv2.dct(g)
    IT = np.round(cv2.idct(np.round(np.round(16.0*T/QQF)*QQF/16)))
    return IT 
Example #7
Source File: BlindWatermark.py    From BlindWatermark with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #8
Source File: BlindWatermark.py    From BlindWatermark with GNU General Public License v3.0 6 votes vote down vote up
def block_get_wm(self,block,index):
        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]
        wm_1 = 255 if max_s%self.mod >self.mod/2 else 0
        if self.mod2:
            max_s = s[1]
            wm_2 = 255 if max_s%self.mod2 >self.mod2/2 else 0
            wm = (wm_1*3+wm_2*1)/4
        else:
            wm = wm_1
        return wm 
Example #9
Source File: BlindWatermark.py    From BlindWatermark with GNU General Public License v3.0 6 votes vote down vote up
def block_get_wm(self,block,index):
        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]
        wm_1 = 255 if max_s%self.mod >self.mod/2 else 0
        if self.mod2:
            max_s = s[1]
            wm_2 = 255 if max_s%self.mod2 >self.mod2/2 else 0
            wm = (wm_1*3+wm_2*1)/4
        else:
            wm = wm_1
        return wm