使用OpenCV将灰度图像转换为负片

six*_*eet 0 c++ opencv

我正在尝试制作一个简单的函数,使用openCV将灰度图像转换为负像.下面我有函数的代码:

#include "stdafx.h"
#include "common.h"
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
.........................................

void negative_image()
{
    Mat img = imread("Images/cameraman.bmp", CV_LOAD_IMAGE_GRAYSCALE);

    for (int i = 0; i < img.row; i++)
    {
        for (int j = 0; j < img.cols; j++)
        {
            img.at<uchar>(i, j) = 255 - img.at<uchar>(i, j);
        }
    }

    imshow("negative image", img);
    waitKey(0);
}
Run Code Online (Sandbox Code Playgroud)

当我尝试构建应用程序时,我收到以下错误:

在此输入图像描述

任何帮助表示赞赏!

Dan*_*šek 6

OpenCV提供了各种矩阵运算,可以组合在矩阵表达式中.从标量中减去矩阵就是其中之一.

因此,在我看来,将灰度图像转换为负像的简单函数看起来像这样:

cv::Mat invert_image(cv::Mat const& input)
{
    return 255 - input;
}
Run Code Online (Sandbox Code Playgroud)