Advertisement
Georgiy031

Untitled

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