Question

I recently started some OpenCV programming on OSX (just using text editor and compiling in terminal). I found program on the internet that is very useful to me but can't seem to run it. This is the code:

#include <stdio.h>
#include "cv.h"
#include <highgui.h>
#include <iostream>
#include <cstdio>
using namespace std;
int widthU;
int heightU;
int xU = 0;
int yU = 0;
int main(int argc, char *argv[])
{
    IplImage *imgPicThres, *imgPicInput;
    imgPicInput = cvLoadImage("bitmap.png", -1);
    imgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);
    cvNamedWindow("Input picture", 0);
    cvNamedWindow("Thres picture", 0);
    //Picture
    //cvThreshold(imgPicInput,imgPicThres,100,255,CV_THRESH_BINARY);
    cvAdaptiveThreshold(imgPicInput, imgPicThres,255,CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY,75,10);
    cvShowImage("Input picture", imgPicInput);
    cvShowImage("Thres picture", imgPicThres);
    while (true)
    {
        int c = cvWaitKey(10);
        if(c==27)
            break;
    }
    cvDestroyWindow("Input picture");
    cvDestroyWindow("Thres picture");
    return 0;
}

And this is the error that I get:

OpenCV Error: Assertion failed (src.size == dst.size && src.type() == dst.type()) in cvAdaptiveThreshold, file /opt/local/var/macports/build/_opt_mports_dports_graphics_opencv/opencv/work/opencv-2.4.5/modules/imgproc/src/thresh.cpp, line 873
libc++abi.dylib: terminate called throwing an exception
Abort trap: 6

I tried to change this line

ImgPicThres = cvCreateImage(cvSize(imgPicInput->width, imgPicInput->height), IPL_DEPTH_8U, 1);

into

ImgPicThres = cvCreateImage(cvGetSize(imgPicInput), IPL_DEPTH_8U, 1);

with no luck. OpenCV is installed via Macports and is running the latest version. Any help would be appreciated. Thanks!

Was it helpful?

Solution

imgPicInput = cvLoadImage("bitmap.png",CV_LOAD_IMAGE_GRAYSCALE);

to ensure that the image you read is actually grayscale.

OTHER TIPS

Additionally to suggestion from perfanoff, I'd rather clone image rather then creating it.

imgPicThres = cvCloneImage(imgPicInput );

I have found the answer but forgot to mention it. As the error says imgPicInput and imgPicThres are not of the same size and type. Also I was supposed to watch after the image channels which I didn't.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top