c++ - opencv 图像二值化

标签 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

最佳答案

看,使用 C++ 很容易:

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

另见threshold()

关于c++ - opencv 图像二值化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018382/

相关文章:

c++ - 两个 SSE2 打包 double 的最优无分支条件选择

c++ - MSVC 中的模板模板参数错误,但不是 Clang。为什么?

c++ - Xcode上找不到'opencv2/core/core.hpp'文件错误

python - 在 openCV 中显示的图像之间暂停

java - 从哪里获得 openCV 的 jar?

python - 在 Windows 上使用 python 的 uEye 相机

c++ - SFML 中的 View 存在一些问题

c++ - 如何解释这个 C++ 类型?

c++ - 将 C 风格的字符串转换为 C++ std::string

c++ - 如何计算多张图片的平均值