Ask Your Question
0

[C++] OpenCV2.4.3, cv::resize produces empty image

asked 2013-02-15 04:44:03 -0600

Akhar gravatar image

updated 2013-02-16 02:00:58 -0600

I have been attempting to use OpenCV together with Tesseract to produce some manner of Label reader. 'resize' under the cv namespace has however refused to cooperate and produces an erroneous image with a red cross much like a union jack. Here is the (most likely) offending piece(s) of code;

cv::VideoCapture capture;
cv::Mat CVImg;
if ( !capture.open(CV_CAP_ANY) ) 
{
    //TODO
    return;
}
if (!IsIconic((HWND)this->Handle.ToInt32()))
{
    if (!capture.read(CVImg)) 
    {
        //TODO
        return;
    }
    cv::Mat CVNewImg (pictureBox1->Height, pictureBox1->Width, CVImg.type());
    cv::resize(CVImg,CVNewImg, CVNewImg.size(), 0, 0, CV_INTER_LINEAR); //Problematic
    displayMat(CVNewImg);
}
void displayMat (cv::Mat DSPIMG)
{
    if (DSPIMG.type() == CV_8UC3)
    {
        this->pictureBox1->Image=(gcnew System::Drawing::Bitmap(
        DSPIMG.size().width,
        DSPIMG.size().height,
        DSPIMG.size().width * 3,
        System::Drawing::Imaging::PixelFormat::Format24bppRgb,
        (System::IntPtr)DSPIMG.data));
    }
}

This is in windows MFC, so forgive me if I do not wish to cover an entire page with all the junk code. If this is something very simple that I have overlooked I would very much appreciate it if you in your superior wisdom could enlighten me, perhaps I have completely misinterpreted the correct use of the function. In short: It is very possible I am being a complete moron, alas my sleep has been rather lacking as I am under a lot of pressure time-wise.

I can parse the original CVImg to displayMat and it will display properly, save for not actually adjusting to the size of the picturebox of course. However not so with CVNewImg, producing the aforementioned symbol of britland.

edit retag flag offensive close merge delete

Comments

that 'britland' image is not an opencv artefact, but a message from GDI, that your image was not acceptable. (width probably must be dividable by 4). also see the answer below, for the right way to calculate the stride

berak gravatar imageberak ( 2013-02-15 15:31:38 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-02-15 08:04:59 -0600

vaaksiainen gravatar image

updated 2013-02-15 08:06:02 -0600

Instead of questioning cv::resize, I would look at your displayMat. I never use MFC, but Gdiplus::Bitmap(INT width,INT height,INT stride,PixelFormat format, BYTE *bits) looks similar to yours. In RGB24, the length of the stride is equal (or larger if "strided" data) 3 * width bytes and not 4 * height. Maybe you should nevertheless check that the CV type is actually CV_8UC3 before expecting it. In OpenCV 2-D images the stride is always given in img.step.p[0].
Best,
-V

edit flag offensive delete link more

Comments

I appreciate the assistance. It was adjusted as per your instructions however I'm sorry to say the same issues are still present, whether I use step/height4/width3. Step being the one I used first before a bunch of melatonin-induced debugging.

Akhar gravatar imageAkhar ( 2013-02-15 16:01:20 -0600 )edit

Mat constructor has height at first argument and width at second, so change it in your CVNewImg declaration.

Daniil Osokin gravatar imageDaniil Osokin ( 2013-02-16 01:46:40 -0600 )edit

Ah yes, thank you, silly mistake: that is now switched. Alas it still did not fix the issue, mayhap I should test the beta library. Though I am hesitant to believe something is off with the stable release of course.

Akhar gravatar imageAkhar ( 2013-02-16 02:06:38 -0600 )edit
-1

answered 2013-02-16 13:42:33 -0600

try deleting the reference count of Mat before graphing (DSPIMG.refcount=0).

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2013-02-15 04:44:03 -0600

Seen: 3,516 times

Last updated: Feb 16 '13