Advertisement
jack06215

[OpenCV] Image sub-sampling

Feb 27th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. #include <opencv2/highgui.hpp>
  6. #include <opencv2/core.hpp>
  7. #include <opencv2/imgproc.hpp>
  8.  
  9. #include "dlib/string.h"
  10.  
  11.  
  12. std::vector<std::string> labels{ "dolphin", "ibis" };
  13. int main() {
  14.    
  15.     std::string filename;
  16.     cv::Mat cropped_image;
  17.    
  18.     // for each category of objects
  19.     for (int class_id = 0; class_id < labels.size(); class_id++) {
  20.         // each category has 50 samples
  21.         for (int sample_id = 1; sample_id < 51; sample_id++) {
  22.             filename = "./data/" + labels[class_id] + "/image_" + dlib::pad_int_with_zeros(sample_id, 4) + ".jpg";
  23.             cv::Mat img = cv::imread(filename);
  24.             cv::Mat img_cpoy = img.clone();        
  25.             cv::Size s = img.size();
  26.             int rows = s.height;
  27.             int cols = s.width;
  28.  
  29.             /*!
  30.                 The main procedure starts here.
  31.                 1. The dimension(width and height) at each level is divided by 2^N at level N
  32.                 2. The number of blocks divided are 2^(N^2), i.e. [1, 4, 16,...]
  33.             !*/
  34.             int width = cols / 2;
  35.             int height = rows / 2;
  36.             // for level = 0 to 2
  37.             for (int j = 0; j < 3; j++) {
  38.                
  39.                 // dimension
  40.                 width = cols / std::pow(2, j);
  41.                 height = rows / std::pow(2, j);
  42.                
  43.                 // number of blocks
  44.                 for (int i = 0; i < std::pow(2, 2 * j); i++) {
  45.                     int X = (i % static_cast<int>(std::pow(2, j)))* width;
  46.                     int Y = (i / static_cast<int>(std::pow(2, j)))* height;
  47.                     cropped_image = img(cv::Rect(X, Y, width, height));
  48.                     cv::rectangle(img_cpoy, cv::Rect(X, Y, width, height), cv::Scalar(0, 0, 255), 2);
  49.  
  50.                     cv::imshow("cropped", cropped_image);
  51.                     cv::imshow("area", img_cpoy);
  52.                     cv::waitKey(0);
  53.                 }
  54.             }
  55.         }
  56.     }
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement