图像处理基础

标签:取反   ros   rate   color   osi   auto   mat   enc   inpu   

void test() {

    Mat src = imread("D:/opencvsp/pic.png");

    if (src.empty())

    {

         printf("could not load image\n");

         return -1;

    }

    namedWindow("test opencv setup", WINDOW_AUTOSIZE);

    imshow("test opencv setup", src);

    namedWindow("output windows", WINDOW_AUTOSIZE);

    Mat output_image;

    cvtColor(src, output_image, COLOR_BGR2HLS);///

    imshow("output windows", output_image);

 

    imwrite("D:/opencvsp/hsls.png", output_image);///

}

//提高对比度 矩阵的掩膜操作

 void test02(){

     Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     if (!src.data)

     {

          printf("could not load image\n");

          return -1;

     }

     namedWindow("intput image", WINDOW_AUTOSIZE);

     imshow("input image", src);

     

     int cols = (src.cols - 1) * src.channels();

     int offsetx = src.channels();

     int rows = src.rows;

     dst = Mat::zeros(src.size(),src.type());

     for (int row = 0; row < rows - 1; row++)

     {

          const uchar* previous = src.ptr<uchar>(row - 1);

          const uchar* current = src.ptr<uchar>(row);

          const uchar* next = src.ptr<uchar>(row + 1);

          uchar* output = dst.ptr<uchar>(row);

          for (int col = 0; col < cols; col++)

          {

              output[col] = saturate_cast<uchar>(5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));

          }

     }

   

     

     Mat kernel = (Mat_<char>(3, 3) << 0,-1,0,-1,5,-1,0,-1,0 );

     filter2D(src,dst,src.depth(),kernel);/////提高对比度

     namedWindow("contrast image demo", WINDOW_AUTOSIZE);

     imshow("contrast image demo",dst);

 }

 

 void test03() {

     Mat src, dst;

     src = imread("D:/opencvsp/pic.png");

     namedWindow("input",WINDOW_AUTOSIZE);

     namedWindow("out",WINDOW_AUTOSIZE);

     cvtColor(src,dst,COLOR_BGR2GRAY);

     printf("input image channels: %d\n",src.channels());

     printf("out image channels: %d \n",dst.channels());

 

     dst = Mat(src.size(),src.type());

     dst = Scalar(127,0,255);

     const uchar* firstRow = dst.ptr<uchar>(0);

 

     Mat M(3, 3, CV_8UC3, Scalar(0, 0, 255));

 

     Mat m2 = Mat(2, 2, CV_8UC1);//

     Mat m22 = Mat::eye(2, 2, CV_8UC1);

     imshow("output",dst);

 } 

 

 void test04() {

      Mat src, gray_src;

     src = imread("D:/opencvsp/pic.png");

     if (src.empty())

     {

          std::cout << "could not load image" << std::endl;

          return -1;

     }

     namedWindow("input", WINDOW_AUTOSIZE);

     imshow("input", src);

 

     cvtColor(src, gray_src, COLOR_BGR2GRAY);

     namedWindow("output", WINDOW_AUTOSIZE);

     imshow("output", gray_src);

     int height = gray_src.rows;

     int width = gray_src.cols;

     //单通道

     for (int row = 0; row < height; row++)

     {

          for (int col = 0; col < width; col++)

          {

              int gray = gray_src.at<uchar>(row, col);

              gray_src.at<uchar>(row, col) = 255 - gray;

          }

     }

     namedWindow("gray_invert", WINDOW_AUTOSIZE);

     imshow("gray_invert", gray_src);

 

     Mat dst,dst2;

     src = imread("D:/opencvsp/pic.png");

     dst.create(src.size(), src.type());

     cvtColor(src, gray_src, COLOR_BGR2GRAY);

     namedWindow("output", WINDOW_AUTOSIZE);

     imshow("output", gray_src);

     int heightt = gray_src.rows;

     int widthh = gray_src.cols;

     int nc = src.channels();

     for (int row = 0; row < heightt; row++)

     {

          for (int col = 0; col < widthh; col++)

          {

              if (nc == 1)

              {

                  int gray = gray_src.at<uchar>(row, col);

                  gray_src.at<uchar>(row, col) = 255 - gray;

              }

              else

              {

                  int b = src.at<Vec3b>(row, col)[0];

                  int g = src.at<Vec3b>(row, col)[1];

                  int r = src.at<Vec3b>(row, col)[2];

                  dst.at<Vec3b>(row, col)[0] = 255 - b;

                  dst.at<Vec3b>(row, col)[1] = 255 - g;

                  dst.at<Vec3b>(row, col)[2] = 255 - r;

                  //灰度图

                  gray_src.at<uchar>(row, col) = max(r, max(b,g));

                  gray_src.at<uchar>(row, col) = min(r, min(b, g));

              }

              

          }

     }

     namedWindow("dst", WINDOW_AUTOSIZE);

     imshow("dst", dst);

     //取反像素

     bitwise_not(src, dst2);

     namedWindow("dstt", WINDOW_AUTOSIZE);

     imshow("dstt", dst2);

 

 

 }

图像处理基础

标签:取反   ros   rate   color   osi   auto   mat   enc   inpu   

原文地址:https://www.cnblogs.com/sundayvc/p/14232845.html

版权声明:完美者 发表于 2021-01-07 12:07:55。
转载请注明:图像处理基础 | 完美导航

暂无评论

暂无评论...