• 机器视觉-笔试题2 统计直方图


    不允许用opencv的Mat,要求手写代码;

      1 #include<iostream>
      2 #include<vector>
      3 using namespace std;
      4 
      5 //随机数头文件
      6 #include <stdlib.h> 
      7 #include <stdio.h> 
      8 #include <time.h> 
      9 
     10 
     11 
     12 #define MAX_PIXEL_VALUE 25
     13 
     14 namespace MY
     15 {
     16     class Mat
     17     {
     18     public:
     19         Mat();
     20         Mat(int rows, int cols);
     21         int rows() const
     22         {
     23             return rows_;
     24         }
     25         int cols() const
     26         {
     27             return cols_;
     28         }
     29         ~Mat();
     30         bool empty() const
     31         {
     32             return image.empty();
     33         }
     34         int get(int rows, int cols) const
     35         {
     36             return image[rows][cols];
     37         }
     38     public:vector<vector<int>> image;
     39     private:
     40         int rows_;
     41         int cols_;
     42     };
     43     Mat::Mat(int rows, int cols) :rows_(rows), cols_(cols)
     44     {
     45         srand((unsigned)time(NULL));//随机数种子
     46         for (int j = 0; j < rows; j++)
     47         {
     48             vector<int> temp;
     49             for (int i = 0; i < cols; i++)
     50             {
     51                 temp.push_back(rand() % (MAX_PIXEL_VALUE+1));//【0,26)
     52             }
     53             image.push_back(temp);
     54         }
     55     }
     56     Mat::Mat()
     57     {
     58     }
     59 
     60     Mat::~Mat()
     61     {
     62     }
     63     
     64     //统计直方图
     65     void ImHist(const vector<vector<int>>& vecs)
     66     {
     67         if (vecs.empty())
     68         {
     69             cout << "empty! " << endl;
     70             return;
     71         }
     72         else
     73         {
     74             int rows = vecs.size();
     75             int cols = vecs[0].size();
     76             vector<int> imhist((MAX_PIXEL_VALUE + 1), 0);
     77             for (int j = 0; j < rows; j++)
     78             {
     79                 for (int i = 0; i < cols; i++)
     80                 {
     81                     imhist[vecs[j][i]]++;
     82                 }
     83             }
     84             // 直方图,控制台显示
     85             for(auto& pixelvalue:imhist)
     86             {
     87                 for (int i = 0; i < pixelvalue; i++)
     88                 {
     89                     cout << "-";
     90                 }
     91                 cout << endl;
     92             }
     93         }
     94     }
     95     //显示Mat
     96     void PrintMat(const Mat& mat) //const 对象只能访问const成员函数
     97     {
     98         if (mat.empty())
     99         {
    100             cout << "empty! " << endl;
    101             return;
    102         }
    103         else
    104         {
    105             for (int j = 0; j < mat.rows(); j++)
    106             {
    107                 for (int i = 0; i < mat.cols(); i++)
    108                 {
    109                     cout << mat.get(j,i) << " ";
    110                     cout.width(2);
    111                 }
    112                 cout << endl;
    113             }
    114         }
    115 
    116     }
    117 }
    118 
    119 
    120 int main()
    121 {
    122     MY::Mat matrix(30, 30);
    123     vector<vector<int>> image = matrix.image;
    124     MY::PrintMat(matrix);
    125     MY::ImHist(image);
    126     return 1;
    127 }

     凑合显示下直方图

  • 相关阅读:
    Socket和数据库的一些使用---郭雪彬
    懒加载--初步理解. by:王朋
    自定义带图片和文字的Button的排版--陈棚
    一些开源库分享 ---严焕培
    实现“手机qq”侧滑菜单 -- 吴欧
    iOS 小技巧总结
    Responder一点也不神秘————iOS用户响应者链完全剖析 周傅琦君
    通过版本号来判断用户是否是第一次登陆----By张秀清
    常用第三方推荐
    static, const 和 static const 变量的初始化问题
  • 原文地址:https://www.cnblogs.com/winslam/p/9473087.html
Copyright © 2020-2023  润新知