opencv iplimage二值化

时间:2014-04-11 16:55:25

标签: c++ opencv

我有一个IplImage,我希望使用以下代码进行二值化(不使用cvThreshold函数):

#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
    static IplImage* img ;

    img = cvLoadImage ("c:\\Mytest.jpg");

    for(int i=0;i<img->height;i++)
    {
        for(int j=0;j<img->width;j++)
        {
            if (img->imageData[i*img->widthStep+j]<=10)
                ((uchar *)img->imageData)[i*img->widthStep+j]=255;
            else 
                ((uchar *)img->imageData)[i*img->widthStep+j]=0;
        }
    }

    cvShowImage("After",img);
    waitKey(0);
};

但是此代码仅影响部分图像,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:3)

看,用c ++轻而易举:

Mat img = imread("c:\\Mytest.jpg", 0); // load grayscale
Mat thresh = ( im <= 10 ); // that's it already!
imshow("After",img);
waitKey(0);

另请参阅threshold()