OpenCV (1) OpenCV Hello World

1. C++ OpenCV Hello world:

Open an image and read it into a matrix.

#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main() {
    Mat img = imread("mini.jpg");
    imshow("Image", img); // "Image" is the name of the window
    waitKey(0);
    destroyAllWindows();
    return 0;
}

Linux makefile.

CXX=g++
LIBS=-lopencv_world

.PHONY: debug, main, clean
debug: dbgmain.out
main: main.out

dbgmain.out: main.cpp
 $(CXX) $^ -g -o $@ $(LIBS)

main.out: main.cpp
 $(CXX) $^ -O3 -o $@ $(LIBS)

clean:
 rm -fr *.out

2. C++ OpenCV Hello world 2:

Use GaussianBlur to blur the image.

#include
#include

using namespace std;
using namespace cv;

int main() {
    Mat img = imread("mini.jpg");
    // imshow("Image", img);

    Mat blur_img;
    GaussianBlur(img, blur_img, Size(5, 5), 10, 10);
    imshow("Blur Image", blur_img);

    waitKey(0);
    destroyAllWindows();

    return 0;
}

The declaration of of GaussianBlur:

void GaussianBlur(InputArray src, 
                  OutputArray dst, 
                  Size ksize, 
                  double sigmaX, 
                  double sigmaY=0, 
                  int borderType=BORDER_DEFAULT); 

Change the size of the kernel to 50*50.



Reference

[1] Adrian Kaehler, Gary Bradski, Learning OpenCV 3 , O'Reilly Media (2017)

留言

熱門文章