Georgiy031

Untitled

Feb 16th, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <opencv2/opencv.hpp>
  2. #include <math.h>
  3. #include <time.h>
  4. #include <iostream>
  5. #include <iomanip>
  6. #include <vector>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10.  
  11. int main() {
  12.  
  13.     Mat img = imread("input.jpg");
  14.  
  15.     if (img.empty()) {
  16.         cout << "Can not open or image is not present" << endl;
  17.         return 0;
  18.     }
  19.  
  20.     Mat img_chromo;
  21.  
  22.     cvtColor(img, img_chromo, COLOR_BGR2GRAY);
  23.  
  24.     Mat result(256, 512, CV_8U, 255);
  25.  
  26.     vector<int> v(256);
  27.    
  28.     for (int i = 0; i < img_chromo.cols; ++i) {
  29.         for (int j = 0; j < img_chromo.rows; ++j) {
  30.             uchar cur_color = img_chromo.at<uchar>(j, i);
  31.             v[cur_color]++;
  32.         }
  33.     }
  34.  
  35.     int max_count = *max_element(v.begin(), v.end());
  36.  
  37.     for (int i = 0; i < 256; ++i) {
  38.         int h = v[i] * 250 / max_count;
  39.         Point pt1(i * 2, 255 - h);
  40.         Point pt2(i * 2 + 1,255);
  41.         rectangle(result, pt1, pt2, 0);
  42.     }
  43.  
  44.  
  45.  
  46.     imshow("result", result);
  47.     imshow("cat", img);
  48.     imshow("cat2", img_chromo);
  49.     waitKey(0);
  50.     return 0;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment