Ask Your Question
1

OpenCV 2.4.4 with ZXing 2.1

asked 2013-03-06 09:47:14 -0600

Yannikator gravatar image

Hello,

I'am trying to use ZXing with OpenCV in order to read QR code in C++ technology. I need help about this... If someone have already do that, can you tell me how please?

My first way was to integrate a class name's CVImageSource like this :

CVImageSource::CVImageSource(IplImage* image) {
    image_ = image;
    width =  image->width;
    height = image->height;
}

CVImageSource::~CVImageSource(){}

int CVImageSource::getWidth() const {
    return width;
}

int CVImageSource::getHeight() const {
  return height;
}


unsigned char* CVImageSource::getRow(int r, unsigned char* row) {

    int width = getWidth();
    if(row == NULL){
        row = new unsigned char[width];
    }

    /* Test */
    unsigned char* rowTemp = new unsigned char[width];

    rowTemp = row;
    /* fin Test */
    try{
        for(int c = 0; c < width; c++){
            CvScalar pixel = cvGet2D(&image_, r, c);

            if(image_->depth == IPL_DEPTH_8U || image_->depth == IPL_DEPTH_8S){
                //8 bits depth on each channel
                row[c] = (unsigned char) ((306 * (int)pixel.val[0] + 601 * (int)pixel.val[1] + 
                        117 * (int)pixel.val[2] + 0x200) >> 10);
            }
            else if(image_->depth == IPL_DEPTH_16S){
                //16 bits depth on each channel
                // 0x200 = 1<<9, half an lsb of the result to force rounding
                // I don't know why we do multiplication 306 - 601 - 117 for each channel... I don't find the reason... sorry
                row[c] = (unsigned char) ((306 * ((int)pixel.val[0] >> 8) + 601 * ((int)pixel.val[1] >> 8) +
                        117 * ((int)pixel.val[2] >> 8) + 0x200) >> 10);
            }

        }
    }
    catch(cv::Exception ecv){
        cout << "Error : " << ecv.what() << endl;
    }
    return row;
}

unsigned char* CVImageSource::getMatrix(){
    int width = getWidth();
    int height = getHeight();
    CvScalar pixel; 

    unsigned char* matrix = new unsigned char[width * height];
    unsigned char* m = matrix;

    for(int l = 0; l < height; l++){
        for(int c = 0; c< width; c++){
            pixel = cvGet2D(&image_, l, c);
            if(image_->depth == IPL_DEPTH_8U || image_->depth == IPL_DEPTH_8S){
                //8 bits depth on each channel
                *m = (unsigned char) ((306 * (int)pixel.val[0] + 601 * (int)pixel.val[1] + 
                    117 * (int)pixel.val[2] + 0x200) >> 10);
            }
            else if(image_->depth == IPL_DEPTH_16S){
                //16 bits depth on each channel
                // 0x200 = 1<<9, half an lsb of the result to force rounding
                // I don't know why we do multiplication 306 - 601 - 117 for each channel... I don't find the reason... sorry
                *m = (unsigned char) ((306 * ((int)pixel.val[0] >> 8) + 601 * ((int)pixel.val[1] >> 8) +
                    117 * ((int)pixel.val[2] >> 8) + 0x200) >> 10);
            }
            m++;
        }
    }
    return matrix; /* Warning m is just a pointer */
}

But when I run application using this one, I have exception as :

OpenCV Error: Bad argument (unrecognized or unsupported array type) in unknown function, file ..\..\..\src\opencv\modules\core\src\array.cpp, line 1830
What : ..\..\..\src\opencv\modules\core\src\array.cpp:1830: error: (-5) unrecognized or unsupported array type

The exception is thrown when the application come in the getRow function. It seem there's a trouble with unsigned char *row, but I don't know why...

Have you an idea guys? Thanks.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2013-03-06 10:13:53 -0600

berak gravatar image

updated 2013-03-06 11:45:36 -0600

CvScalar pixel = cvGet2D(&image_, r, c);  
// if image_ is a IplImage*, that would be a IplImage** ?

i think, you want :

CvScalar pixel = cvGet2D(image_, r, c);

(without the &) in both occurences

----------------------------------------------------------

looking twice, found another issue: /* Test */

unsigned char* rowTemp = new unsigned char[width];

rowTemp = row;

now rowTemp is pointing to row, the "new unsigned char[width]" before does not have any effect, also you're leaking memory here!

edit flag offensive delete link more

Comments

You are right, now it work fine... I would post the code in order to help the other who will be interesting but I can for the moment... 2 days waiting before answer my own question... Thank you Berak.

Yannikator gravatar imageYannikator ( 2013-03-06 12:29:36 -0600 )edit

hehe, upvoted your q, so you gain karma a bit faster ;)

berak gravatar imageberak ( 2013-03-06 13:13:14 -0600 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2013-03-06 09:47:14 -0600

Seen: 1,558 times

Last updated: Mar 06 '13