改变图像的亮度和对比度

时间:2013-05-20 09:26:41

标签: c++ visual-studio-2010 image opencv image-processing

请查看以下代码

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

using namespace std;
using namespace cv;

Mat change(Mat m);

int main()

{
    Mat image = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");

    Mat copy = Mat::zeros(image.size(),image.type());

    Mat changedImage = change(copy);

    namedWindow("Image");
    imshow("Image",changedImage);

    waitKey(0);
}

Mat change(Mat m)
{
    int cols = m.cols;
    int rows = m.rows;

    double alpha = 2.2;
    int beta = 50;


    for(int i=0;i<rows;i++)
    {
        for(int c=0;c<cols;c++)
        {
            m.at<Vec3b>(rows,c)[0] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(rows,cols)[0]) + beta);
            m.at<Vec3b>(rows,c)[1] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(rows,cols)[1]) + beta);
            m.at<Vec3b>(rows,c)[2] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(rows,cols)[2]) + beta);
        }
    }

    return m;

}

这个编译很好,但是当我运行它时,我收到以下错误

    OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 < (unsigned)si
ze.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channel
s()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3
) - 1))*4) & 15) == elemSize1()) in unknown function, file c:\opencv\build\inclu
de\opencv2\core\mat.hpp, line 534

为什么我会这样?我想我已经做好了一切。请帮忙。

2 个答案:

答案 0 :(得分:1)

试试这段代码。

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

using namespace std;
using namespace cv;

Mat change(Mat m);

int main()
{
    Mat image = imread("C:/Users/Public/Pictures/Sample Pictures/Penguins.jpg");

    Mat changedImage = change(image); //Modified

    namedWindow("Image");
    imshow("Image",changedImage);

    waitKey(0);
}

Mat change(Mat m)
{
    int cols = m.cols;
    int rows = m.rows;

    double alpha = 2.2;
    int beta = 50;

    for(int i=0;i<rows;i++)
    {
        for(int c=0;c<cols;c++)
        {
            m.at<Vec3b>(i,c)[0] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(i,c))[0]) + beta);  //Modified
            m.at<Vec3b>(i,c)[1] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(i,c))[1]) + beta);  //Modified
            m.at<Vec3b>(i,c)[2] = saturate_cast<uchar>(alpha* (m.at<Vec3b>(i,c))[2]) + beta);  //Modified
        }
    }

    return m;

}

答案 1 :(得分:0)

试试这个......

Mat Change(Mat input,int beta = 50)
{
Mat Output;

Scalar S(beta,beta,beta);
cv::add(input,S,Output);

return Output;
}