0

我最近在 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 安装并运行最新版本。任何帮助,将不胜感激。谢谢!

4

3 回答 3

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

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

于 2013-05-23T10:49:11.013 回答
0

我找到了答案,但忘了提及。正如错误所说,imgPicInput 和 imgPicThres 的大小和类型不同。我也应该在我没有观看的图像频道之后观看。

于 2013-06-25T16:17:43.387 回答
0

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

imgPicThres = cvCloneImage(imgPicInput );
于 2013-05-23T11:13:23.180 回答