malloc(): memory corruption (fast): C++ opencv编写出错

好象是亮度调整那里出错了,可是不知道怎么改

int main(){
    Mat re_src_image;
    Mat src_image = imread("/home/zn/桌面/13740146-f01b848b12b93174.png",1); 
    if(! src_image.data){
        cout << "Couldn't find the image!\n";
        return false;
    }
    else {
        resize(src_image, re_src_image, Size(640, 480));
        vector<Mat> channels;
        split(re_src_image, channels);
        re_src_image = channels.at(2); 
    }
    //亮度调整
    int ContrastValue = 100;      
    int BrightValue = 0;
    Mat light_image = Mat::zeros(re_src_image.size(), re_src_image.type());
    for (int y = 0; y < re_src_image.rows; y++)
    {
        for (int x = 0; x < re_src_image.cols; x++)
        {
            for (int c = 0; c  < 3; c++){
                light_image.at<Vec3b>(y, x)[c] = (ContrastValue*0.01)*(re_src_image.at<Vec3b>(y, x)[c]) + BrightValue;
            }
        }
    }

    imshow("a",re_src_image);
    while(1){
        if(waitKey(0)==27)
            break;
    }
    return 0;
}

split(re_src_image, channels);
re_src_image = channels.at(2);
这里已经将一个彩色图像分为RGB三个单通道的图像保存在channels中,然后将红色通道赋值给了re_src_image;
至此,re_src_image是一个仅保存了红色通道信息的数组.
light_image.at(y, x)[c] = (ContrastValue*0.01)*(re_src_image.at(y, x)[c]) + BrightValue;
这里又对re_src_image进行3通道操作所以报错.
可以将上句话改写为 light_image.at(y, x) = (ContrastValue*0.01)*(re_src_image.at(y, x)) + BrightValue;

就你的程序而言不是对图像整体的亮度调整, 而是调整了一个通道,并且结果也丢失了其他通道的信息
你可以参考以下代码两种彩色模型下对亮度的调整
void RGB_BrightnessEnhancement(cv::Mat &SrcImg, float BrightValue,
float ContrastValue, cv::Mat &dstImg)
{
    std::vector<cv::Mat>channels;
    cv::split(SrcImg, channels);
    for (int i = 0; i < channels.size(); i++)
    {
        for (int px = 0; px < SrcImg.rows; px++)
        {
            for (int py = 0; py < SrcImg.cols; py++)
            {
                channels[i].at<uchar>(px, py) =  (ContrastValue*0.01)*(channels[i].at<uchar>(px, py)) + BrightValue;
            }
        }
    }
    cv::merge(channels, dstImg);
}
void HSV_BrightnessEnhancement(cv::Mat &SrcImg, float BrightValue,float ContrastValue, cv::Mat &dstImg)
{
    std::vector<cv::Mat> HSVImg;
    cv::cvtColor(SrcImg, dstImg, CV_BGR2HSV);
    cv::split(dstImg, HSVImg);
    for (int i = 0; i < dstImg.rows; i++)
    {
        for (int j = 0; j < dstImg.cols; j++)
        {
            HSVImg[2].at<uchar>(i, j) = (ContrastValue*0.01)* (HSVImg[2].at<uchar>(i, j) )+ BrightValue;
        }
    }
    cv::merge(HSVImg, dstImg);
    cv::cvtColor(dstImg, dstImg, CV_HSV2BGR);
}