Open CV学习记录(二)——图像翻转、锐化、亮度和对比度

来源:互联网 发布:竞彩关注数据 编辑:程序博客网 时间:2024/06/03 11:07

使用指针和迭代器的都失败了。。。只能使用at进行访问

#include <opencv2\opencv.hpp>#include <iostream>using namespace std;using namespace cv;void salt(Mat& image, int n = 3000);void Reversal(Mat& image, Mat& result);Mat Contrast_luminance(Mat& image);Mat Sharpen(Mat& image);int main(){    Mat image = imread("E:\\3.jpg");    if (!image.data)    {        cout << "fail to read a image" << endl;        return -1;    }    imshow("image", image);    salt(image);    imshow("salt", image);    Mat re_image;    Reversal(image, re_image);    imshow("re_image", re_image);    /*Mat constrate_image = Contrast_luminance(image);    imshow("contrast", constrate_image);*/    Mat sharpen_image = Sharpen(image);    imshow("sharpen", sharpen_image);    waitKey(0);}void salt(Mat& image, int n){    for (int k = 0; k < n; k++)    {        int i = rand() % image.rows;        int j = rand() % image.cols;        if (image.channels() == 1)        {            image.at<uchar>(i, j) = 255;        }        else        {            image.at<Vec3b>(i, j)[0] = 255;            image.at<Vec3b>(i, j)[1] = 255;            image.at<Vec3b>(i, j)[2] = 255;        }    }}void Reversal(Mat& image,Mat& result){    result.create(image.rows, image.cols, image.type());    int rows = image.rows;    int cols = image.cols;    //image.copyTo(result);  //error!    //result = image.clone();//error!    //result = image;//error!    /*int rows = result.rows;    int cols = result.cols*result.channels();*/    /*for (int i = 0; i <rows; i++)    {        uchar* p_result = result.ptr<uchar>(i);        for (int j = 0; j<cols; j++)        {            p_result[j] = p_result[cols-j-1];        }    }*/    for (int i = 0; i < rows; i++)    {        for (int j = 0; j < cols; j++)        {            result.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, cols - j - 1)[0];            result.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, cols - j - 1)[1];            result.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, cols - j - 1)[2];        }    }    /*result = image.clone();    Mat_<Vec3b>::iterator it = image.begin<Vec3b>();    Mat_<Vec3b>::iterator itend;    Mat_<Vec3b> cimage = result;    Mat_<Vec3b>::iterator itout;    Mat_<Vec3b>::iterator itoutend = cimage.end();    for (itend= image.end<Vec3b>()-1,itout== cimage.begin(); itout != itoutend; itend--, itout++)    {        (*itout)[0] = (*it)[0];        (*itout)[1] = (*it)[1];        (*itout)[2] = (*it)[2];    }*/}Mat Contrast_luminance(Mat& image){    double alpha=2;    int beta=50;    /*cout << "Enter the alpha:";    cin >> alpha;    cout << "Enter the beta:";    cin >> beta;*/    Mat new_image = Mat::zeros(image.size(), image.type());    cout << " Basic Linear Transforms " << endl;    cout << "-------------------------" << endl;    cout << "* Enter the alpha value [1.0-3.0]: ";    cin >> alpha;    cout << "* Enter the beta value [0-100]: ";    cin >> beta;    for (int i = 0; i < image.rows; i++)    {        for (int j = 0; j < image.cols; j++)        {            for (int c = 0; c < 3; c++)            {                //由于运算结果可能不是整数,所以需要格式转换                  new_image.at<Vec3b>(i, j)[c] = saturate_cast<uchar>(alpha*(image.at<Vec3b>(i, j)[c]) + beta);            }        }    }    //for (int i = 0; i < image.rows; i++)    //{    //  uchar* p_image = image.ptr<uchar>(i);    //  for (int j = 0; j < image.cols*image.channels(); j++)    //  {    //      //          //          //由于运算结果可能不是整数,所以需要格式转换      //          p_image[j] = saturate_cast<uchar>(alpha*(p_image[j]) + beta);    //          //  }    //}    return new_image;}Mat Sharpen(Mat& image){    Mat result;    result.create(image.rows, image.cols, image.type());    //使用3*3滤波器,所以遍历的像素中不能包括图像最外围的一圈      for (int i = 1; i < image.rows - 1; i++)    {        //前一行、当前行、后一行的指针          uchar* previous = image.ptr< uchar>(i - 1);        uchar* current = image.ptr< uchar>(i);        uchar* next = image.ptr< uchar>(i + 1);        //输出结果图像的行指针          uchar* output = result.ptr<uchar>(i);        for (int j = 1; j < image.cols - 1; j++)        {            //图像锐化操作               *output++ = cv::saturate_cast<uchar>(5 * current[j] - current[j - 1] - current[j + 1] - previous[j] - next[j]); //saturate_cast<uchar>会将小于0的置零,大于255的改为255          }    }    result.row(0).setTo(cv::Scalar(0));    result.row(result.rows - 1).setTo(cv::Scalar(0));    result.col(0).setTo(cv::Scalar(0));    result.col(result.cols - 1).setTo(cv::Scalar(0));    /*    //调用滤波函数来完成图像的锐化    //滤波器的核    Mat kernel(3,3,CV_32F,Scalar(0));    // 分配像素置    kernel.at<float>(1,1) = 5.0;    kernel.at<float>(0,1) = -1.0;    kernel.at<float>(2,1) = -1.0;    kernel.at<float>(1,0) = -1.0;    kernel.at<float>(1,2) = -1.0;    //调用滤波函数    filter2D(image,result,image.depth(),kernel);    */    return result;}

image.copyTo(result); //error!
result = image.clone();//error!
result = image;//error!
这三种方式都失败了,只有使用create才能正常运行

0 0
原创粉丝点击