Ask Your Question
0

How to get dct of an image in python using opencv

asked 2013-03-18 18:08:20 -0600

shobhitpuri gravatar image

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

How can I get the dct? I would be grateful for your help.

edit retag flag offensive close merge delete

Comments

2

you need a 1 channel image ( well, your split did that already ) float img for dct.

having a hard time to translate the c++ convertTo() method to python, so not an answer yet ;)

the c++ call would look like: mat.convertTo( target,CV_32F, 1.0/255.0 ); // note that dct needs float values in the [0..1] range, what you've got currently is unsigned char data in the [0..255] range

berak gravatar imageberak ( 2013-03-18 18:20:23 -0600 )edit

Thanks for your comment. You're right. Actually I've been trying to look around for the same thing. Have a look at this post: http://stackoverflow.com/questions/7931382/calculate-discrete-cosine-transformation-of-image-with-opencv . Please let me know if you get this working. I'll post the solution if I am able to get it running .

shobhitpuri gravatar imageshobhitpuri ( 2013-03-18 19:14:03 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-03-19 04:13:00 -0600

berak gravatar image

updated 2013-03-19 04:16:10 -0600

this is stolen from the cv2 deconvolution sample ;)

img = cv2.imread(fn, 0)      # 1 chan, grayscale!
imf = np.float32(img)/255.0  # float conversion/scale
dst = cv2.dct(imf)           # the dct
img = np.uint8(dst)*255.0    # convert back
edit flag offensive delete link more

Comments

Thanks for the answer. When I am checking the values of the 'dst' variable, they are coming out to be more than 1. As a result the 'img' has values > 255? Can you please explain this? It would be really helpful.

shobhitpuri gravatar imageshobhitpuri ( 2013-03-24 03:17:56 -0600 )edit

oh, that's true. ( looking at the formula in the refman )

the 255 was just some "ad hoc" upscaling factor for visualisation, maybe you need 50 only, (it was more to show, that you can actually multiply the numpy array with a constant)

berak gravatar imageberak ( 2013-03-24 03:58:57 -0600 )edit

okay. I get it. Thanks. Also I'm curious to know that even if I am not converting 'img' in the range of [0..1], it seems to work fine. How is that? imf = np.float32(img) # float conversion dst = cv2.dct(imf) # the dct img = np.uint8(dst) # convert back

shobhitpuri gravatar imageshobhitpuri ( 2013-03-27 14:51:53 -0600 )edit

Question Tools

Stats

Asked: 2013-03-18 18:08:20 -0600

Seen: 19,919 times

Last updated: Mar 19 '13