• Opencv图像变成灰度图像、取反图像


    #include <iostream>
    #include <opencv2/opencv.hpp>

    using namespace cv;
    using namespace std;

    int main(int argc,char** argv)
    {
      Mat img1, img2,img3,img1_1;
      img1 = imread("D://images//4.jpg");
      if (img1.empty())
      {
        cout <<"could not load images......" << endl;
        return 0;
      }

      namedWindow("win1",CV_WINDOW_AUTOSIZE);
      imshow("win1",img1);
      img1.copyTo(img1_1);
      //namedWindow("win1_1", CV_WINDOW_AUTOSIZE);
      //imshow("win1_1", img1_1);

      cvtColor(img1,img2,CV_BGR2GRAY);
      namedWindow("win2", CV_WINDOW_AUTOSIZE);
      imshow("win2", img2);

      int height = img2.rows;
      int width = img2.cols;

      for (int row=0;row<height;row++)
      {
        for (int col=0;col<width;col++)
        {
          int px_value = img2.at<uchar>(row,col);
          //cout << px_value<< endl;
          img2.at<uchar>(row, col) = 255 - px_value;
        }
      }

      namedWindow("win3", CV_WINDOW_AUTOSIZE);
      imshow("win3", img2);

      img3.create(img1_1.size(), img1_1.type());
      int height_3 = img1_1.rows;
      int widht_3 = img1_1.cols;
      int channels_3 = img1_1.channels();

      /*
      for (int row = 0; row < height_3; row++)
      {
        for (int col=0;col<widht_3;col++)
        {
          if (channels_3==1)
          {
            int px_value = img1_1.at<uchar>(row,col);
            img1_1.at<uchar>(row, col) = 255 - px_value;
          }
          else if (channels_3==3)
          {
            int b = img3.at<Vec3b>(row, col)[0];
            int g = img3.at<Vec3b>(row, col)[1];
            int r = img3.at<Vec3b>(row,col)[2];

            img3.at<Vec3b>(row, col)[0] = 255 - b;
            img3.at<Vec3b>(row, col)[1] = 255 - g;
            img3.at<Vec3b>(row, col)[2] = 255 - r;
          }
        }
      }
      */

      bitwise_not(img1_1,img3);

      namedWindow("win4", CV_WINDOW_AUTOSIZE);
      imshow("win4", img3);


      waitKey(0);
      return 0;
    }

  • 相关阅读:
    [转] websocket新版协议分析+python实现 & websocket 通信协议
    [转] html5演示
    新浪网内部讲师进阶研习会之笔记
    css3 animation属性
    【转】python多线程编程
    搭建selenium自动化环境步骤
    win10下搭建QTP测试环境
    cocos2dx跨平台环境
    Cocos2dx运行win32工程
    原型模式(prototype)
  • 原文地址:https://www.cnblogs.com/herd/p/9675726.html
Copyright © 2020-2023  润新知