mulx10

loadimg

May 25th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include<armadillo>
  3. using namespace std;
  4.  
  5.  
  6. #define STB_IMAGE_IMPLEMENTATION
  7. #include "stb_image.h"
  8. /* https://raw.githubusercontent.com/nothings/stb/master/stb_image.h */
  9.  
  10. int LoadImage(const char* fileName, int *width, int *height, int *channels, arma::Mat<unsigned char>&& output, size_t offset = 0)
  11. {
  12.  
  13.     unsigned char *image;
  14.     stbi_set_flip_vertically_on_load(true);
  15.     if (*channels == 3)
  16.     {
  17.         image = stbi_load(fileName,
  18.                          width,
  19.                          height,
  20.                          channels,
  21.                          STBI_rgb);
  22.     }
  23.     if(*channels == 1)
  24.     {
  25.         image = stbi_load(fileName,
  26.                          width,
  27.                          height,
  28.                          channels,
  29.                          STBI_grey);
  30.     }
  31.     int size = (*width)*(*height)*(*channels);
  32.     cout<< width <<" "<< height <<" "<< channels <<endl;
  33.     output = arma::Mat<unsigned char>( image, 1, size, false, true );
  34.     return 0;
  35. }
  36.  
  37. int main(int argc, char const *argv[])
  38. {
  39.     int width, height, channels = 3;
  40.  
  41.     arma::Mat<unsigned char> img;
  42.     LoadImage(argv[1], & width, &height, &channels, std::move(img));
  43.  
  44.     img.print("Image");
  45.     return 0;
  46. }
Add Comment
Please, Sign In to add comment