OPENCV阈值

时间:2015-11-20 06:31:10

标签: c++ opencv

我已经在open cv中进行了阈值处理,但是我得到了一个全黑的彩色窗口作为输出。我使用的命令是:

IplImage* frame = cvLoadImage("threshold.jpg",CV_LOAD_IMAGE_COLOR);
IplImage* dst = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
IplImage* grayframe = cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
cvCvtColor(frame,grayframe,CV_RGB2GRAY);

double thresh = 127;
double maxValue = 255;
double cvThreshold(const CvArr* grayframe,CvArr* dst,double thresh,double maxValue,int CV_THRESH_BINARY_INV);

请帮助我获得正确的输出。

2 个答案:

答案 0 :(得分:1)

首先我建议你仔细阅读 Threshold_Opencv

其次double thresh = 127;表示intensity小于127的所有像素都是黑色,其余像素是白色,所以尝试更改值例如,从127到200再次检查。

答案 1 :(得分:0)

您正在使用函数声明,而不是函数调用。将最后一行更改为:

cvThreshold(grayframe, dst, thresh, maxValue, CV_THRESH_BINARY_INV);

完整代码:

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    IplImage* frame = cvLoadImage("threshold.jpg", CV_LOAD_IMAGE_COLOR);
    IplImage* dst = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    IplImage* grayframe = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 1);
    cvCvtColor(frame, grayframe, CV_RGB2GRAY);

    double thresh = 127;
    double maxValue = 255;
    cvThreshold(grayframe, dst, thresh, maxValue, CV_THRESH_BINARY_INV);

    cvShowImage("Threshold", dst);
    cvWaitKey();

    return 0;
}
但是,您应该避免使用这种过时的语法。您可以使用C ++语法,如:

#include <opencv2/opencv.hpp>
using namespace cv;

int main()
{
    Mat3b frame = imread("threshold.jpg", IMREAD_COLOR);
    Mat1b grayframe;
    cvtColor(frame, grayframe, COLOR_BGR2GRAY);

    double thresh = 127;
    double maxValue = 255;

    Mat1b dst;
    threshold(grayframe, dst, thresh, maxValue, THRESH_BINARY_INV);

    imshow("Threshold", dst);
    waitKey();

    return 0;
}