GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Jun-2012, 08:32
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Implementation of dct in opencv


i"m trying to implement dct in a jpeg image.

CPP / C++ / C Code:
Mat hostImage = imread(argv[1], 1); 
Mat output;

dct(hostImage, output, 0); 
namedWindow("DCT Image", CV_WINDOW_AUTOSIZE); 
cvMoveWindow("DCT Image", 60, 50); 

imshow("DCT Image", output); 
waitKey(0); 
destroyWindow( "DCT Image" ); 
output.release(); 

Unfortunately it gives an error

Code:
OpenCV Error: Assertion failed (type == CV_32FC1 || type == CV_64FC1) in dct, file /build/buildd/opencv-2.3.1/modules/core/src/dxt.cpp, line 2247

I tried googling to solve the problem and found cvConvert. So I tried using cvConvert but still no luck

Can anyone give comments?
Last edited by LuciWiz : 15-Jun-2012 at 01:59. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 14-Jun-2012, 13:01
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 6,147
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Implementation of dct in opencv


Quote:
Originally Posted by stbb24
...So I tried using cvConvert...comments

First, here's a question: Did any of the googlees have success using cvConvert to get a DCT from an OpenCV image of any type? If not, then why did you try to emulate them? If they were successful but you weren't, what did you do that is different from the success stories?

Next, here's a suggestion:

Stop mixing old-style cvXXX functions and structures with modern ones! (So, instead of using cvConvert(), use convertTo().)

Finally, here's some information that isn't obvious from the OpenCV documentation (at least it doesn't exactly jump off the page at you). Whether it is sprinkled around the Google landscape, well I don't know. (I am assuming that if you found a complete working example, your questions would be somewhat more specific, right?)
  1. cv.convertTo() can change the data type of an image (unsigned char to floating point, for example), but can not change the depth. The "default" image type from imread() is CV_8UC3 (Three planes of unsigned char pixel values).

  2. The cv.dct() function works on a 1-D array. (It's just at straight mathematical calculation of the Discrete Cosine Transform applied to an array of floating point values.)

  3. As a result of items 1 and 2, you can not directly apply cv.dct() to the Mat image that you read from the file. (You knew that already; now you know why. Or at least that's my point here.)

  4. What you can do is:
    Use cv.split() to separate the color planes of the original image into separate arrays.
    Apply cv.dct() to each plane
    Use cv.merge() to put the planes back into a single image.

Here's an example that does that. Then it starts with the merged DCT image and works back (using the Inverse DCT function cv.idct()) to an image that looks a lot like the original:
CPP / C++ / C Code:
// OpenCV version 2 C++ API functions
//
// Split color source image into its color planes.
// Perform Discrete Cosine Transform on each plane and merge.
//
// Then split the merged image into its color planes.
// Perform inverse DCT on each plane and merge.
//
// Reconstituted image should look a lot like the original.
// (That's kind of the point of this little exercise.)
//
//
//   davekw7x
//
#include <iostream>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main(int argc, char ** argv)
{
    const char *fileName = "baboon.jpg";

    if (argc > 1)
    {
        fileName = argv[1];
    }

    //
    // For your reference: Some image types of interest:
    //
    cout << "CV_8UC1  = " << CV_8UC1  << endl;
    cout << "CV_8UC3  = " << CV_8UC3  << endl;
    cout << "CV_32FC1 = " << CV_32FC1 << endl;
    cout << "CV_32FC3 = " << CV_32FC3 << endl;
    cout << "CV_64FC1 = " << CV_64FC1 << endl;
    cout << "CV_64FC3 = " << CV_64FC3 << endl;

    Mat originalImage = imread(fileName, 1); 

    if (originalImage.empty() || (originalImage.data == NULL))
    {
        cout << "Can't load image from " << fileName << "!" << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Working on image from " << fileName << endl;
    cout << "Original image type        = " << originalImage.type() << endl;

    // Split the image into its three planes
    vector<Mat> planes;
    split(originalImage, planes);

    // Convert each plane to a type suitable for cv.dct(),
    // and do the transform on each one.
    vector<Mat> outplanes(planes.size());
    cout << "  Individual input planes have type = "
         << planes[0].type() << endl;

    for (size_t i = 0; i < planes.size(); i++)
    {
        planes[i].convertTo(planes[i], CV_32FC1);
        dct(planes[i], outplanes[i]);
    }
    cout << "  Individual  DCT  planes have type = "
        << outplanes[0].type() << endl;

    // Now put the planes together into a single image
    Mat merged;
    merge(outplanes, merged);
    cout << "Merged DCT image has type  = " << merged.type() << endl;

    //
    // Show what we have so far
    //
    namedWindow("Original", CV_WINDOW_AUTOSIZE);
    int x = 0;
    int y = 0;
    moveWindow("Original", x, y);
    imshow("Original", originalImage);


    // To display the DCT of the individual planes, uncomment the
    // following #define directive.
// #define SHOW_DCT_PLANES 1
#ifdef SHOW_DCT_PLANES
    x += 100; y += 100;
    namedWindow("DCT Image[0]", CV_WINDOW_AUTOSIZE); 
    moveWindow("DCT Image[0]", x, y);
    imshow("DCT Image[0]", outplanes[0]); 

    x += 100; y += 100;
    namedWindow("DCT Image[1]", CV_WINDOW_AUTOSIZE); 
    moveWindow("DCT Image[1]", x, y);
    imshow("DCT Image[1]", outplanes[1]); 

    x += 100; y += 100;
    namedWindow("DCT Image[2]", CV_WINDOW_AUTOSIZE); 
    moveWindow("DCT Image[2]", x, y);
    imshow("DCT Image[2]", outplanes[2]); 
#endif

    x += 100; y += 100;
    namedWindow("Merged DCT", CV_WINDOW_AUTOSIZE);
    moveWindow("Merged DCT", x, y);
    imshow("Merged DCT", merged);

    // Start with the merged image and go the other way:
    // Split into planes and do inverse DCT on each.
    split(merged, planes);

    for (size_t i = 0; i < planes.size(); i++)
    {
        idct(planes[i], outplanes[i]);
        outplanes[i].convertTo(outplanes[i], CV_8UC1);
    }

    Mat remerged;
    merge(outplanes, remerged);
    cout << "Reconstituted image type   = " << remerged.type() << endl;

    x += 100; y += 100;
    namedWindow("Reconstituted Image", CV_WINDOW_AUTOSIZE);
    moveWindow("Reconstituted Image", x, y);
    imshow("Reconstituted Image", remerged);
    
    waitKey(0); 

    destroyAllWindows();
}


Regards,

Dave
__________________
Sometimes I just can't help myself...
  #3  
Old 14-Jun-2012, 16:48
stbb24 stbb24 is offline
New Member
 
Join Date: May 2012
Posts: 14
stbb24 is on a distinguished road

Re: Implementation of dct in opencv


Ok thank you for your time.

I have another question. I can't use old use old cvXXX functions with modern ones (just want to clear this)? Like if i start my code with old functions/modern ones i should throughout the code just use old ones/modern ones?

And I thought i could still use cvConvert() by omitting the cv part of the code?

Really sorry still new and still exploring opencv.
 
 

Recent GIDBlogConfiguring iptables for Webmin Servers Index Module by gidnetwork

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenCv C to C++ stbb24 C++ Forum 8 30-May-2012 21:18
OpenCV and problems with matrices ipunished C++ Forum 2 19-Jul-2011 10:58
Implementation of Chord DHT in ANSI C. przemnet C Programming Language 0 17-Jan-2011 05:08
Encryption implementation issue bigbangman C Programming Language 6 02-Sep-2004 11:21
tapeplayer implementation ozzytx C++ Forum 0 23-Aug-2004 13:14

Network Sites: GIDNetwork · GIDApp · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 19:58.


vBulletin, Copyright © 2000 - 2013, Jelsoft Enterprises Ltd.