Skip to content

Instantly share code, notes, and snippets.

@1duo
Created July 16, 2018 22:27
Show Gist options
  • Select an option

  • Save 1duo/c868f0bccf0d1f6cd8b36086ba295e04 to your computer and use it in GitHub Desktop.

Select an option

Save 1duo/c868f0bccf0d1f6cd8b36086ba295e04 to your computer and use it in GitHub Desktop.

Revisions

  1. 1duo created this gist Jul 16, 2018.
    48 changes: 48 additions & 0 deletions opencv.center.crop.cpp.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    C++ main.cpp:

    ```
    #include <opencv2/core/core.hpp>
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>
    #include <string>
    using namespace cv;
    using namespace std;
    int main(int argc, char** argv) {
    string imageName("./images.jpeg"); // by default
    if (argc > 1) {
    imageName = argv[1];
    }
    Mat image;
    image = imread(imageName.c_str(), IMREAD_COLOR);
    if (image.empty()) {
    cout << "Could not open or find the image" << std::endl ;
    return -1;
    }
    cout << "Initial image dimension: " << image.cols << " X " << image.rows << endl;
    namedWindow( "Display window", WINDOW_AUTOSIZE );
    imshow( "Display window", image );
    waitKey(0);
    const int cropSize = 128;
    const int offsetW = (image.cols - cropSize) / 2;
    const int offsetH = (image.rows - cropSize) / 2;
    const Rect roi(offsetW, offsetH, cropSize, cropSize);
    image = image(roi).clone();
    cout << "Cropped image dimension: " << image.cols << " X " << image.rows << endl;
    imshow( "Display window", image );
    waitKey(0);
    return 0;
    }
    ```

    Compile:

    ```
    g++ -std=c++11 main.cpp `pkg-config --libs --cflags opencv` -o main
    ```