Question

I've been trying to figure out a way of getting a DCT of image. After getting the image and doing a bunch of filtering, I want to calculate DCT. Following is the code sniplet:

imgcv1 = cv2.split(imgcv)[0] 
cv2.boxFilter(imgcv1, 0, (7,7), imgcv1, (-1,-1), False, cv2.BORDER_DEFAULT)
#resize image to 32x32
cv2.resize( imgcv1, (32, 32 ) ,imgcv1)

I've been trying to play around with the description given here but no success. When I tried something like:

dst = cv2.dct(imgcv1)

I get an error like:

cv2.error: /build/buildd/opencv-2.3.1/modules/core/src/dxt.cpp:2247: error: (-215) type == CV_32FC1 || type == CV_64FC1 in function dct

From what I can understand from the error is that the input is not 32 bit float but 8 bit. How may I convert it and pass it as 32 bit float? I am not sure of the problem. I would be grateful for your help. How can I get the DCT? I am pretty new to openCV and python. I searched through other 2/3 threads on same topic but it did not work.

Was it helpful?

Solution

I found the solution. As I said we need to convert to float that too within range 0 to 1. Then we can convert it back after computing DCT:

imf = np.float32(imgcv1)/255.0  # float conversion/scale
dct = cv2.dct(imf)              # the dct
imgcv1 = np.uint8(dct*255.0)    # convert back to int

OTHER TIPS

Assuming that your image is greyscale, this function works well in computing the DCT.

import cv2 as cv2
import numpy as np

def doDct(inputMatrix):
    fltMatrix = np.float32(inputMatrix)/255.0
    fltDct = cv2.dct(fltMatrix)
    return np.uint8(fltMatrix * 255)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top