简体   繁体   中英

OPENCV Thresholding

I have done thresholding in open cv, but I got a full black color window as output. The command I used is:

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);

Please help me to get the correct output.

First I advise you to read this carefully Threshold_Opencv

Second the double thresh = 127; that's mean all the pixels that's intensity is less than 127 will be black, and the rest will be white, so try to change the value from 127 to 200 for example and check again.

You're using a function declaration, instead of a function call. Change last line to:

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

Full code:

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

You should, however, avoid to use such obsolete syntax. You can use C++ syntax like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM