OpenCV错误:cvAdaptiveThreshold中的断言失败

时间:2013-05-23 10:45:01

标签: c++ opencv osx-mountain-lion libc++

我最近在OSX上开始了一些OpenCV编程(只使用文本编辑器并在终端中编译)。我发现互联网上的程序对我来说非常有用,但似乎无法运行它。 这是代码:

#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;
}

这是我得到的错误:

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

我试图改变这一行

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

ImgPicThres = cvCreateImage(cvGetSize(imgPicInput), IPL_DEPTH_8U, 1);
没有运气。 OpenCV是通过Macports安装的,并且运行的是最新版本。任何帮助,将不胜感激。谢谢!

3 个答案:

答案 0 :(得分:1)

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

确保您阅读的图像实际上是灰度。

答案 1 :(得分:0)

除了来自perfanoff的建议之外,我宁愿克隆图像而不是创建它。

imgPicThres = cvCloneImage(imgPicInput );

答案 2 :(得分:0)

我找到了答案,但忘记提及了。由于错误说imgPicInput和imgPicThres的大小和类型不同。此外,我应该观看我没有的图像频道。