참고서적

생능출판사

OpenCV로 배우는 영상 처리 및 응용

 

 

OpenCV 4.1 설치 하기, 비주얼스튜디오 15에 적용시키기

https://webnautes.tistory.com/1132

 

Visual Studio 2017에서 OpenCV 4.1.0을 사용하는 방법

안녕하세요 : ) Visual Studio 2017에서 OpenCV 4.1.0을 사용하는 방법을 소개합니다. 기존 글로 적는 방식도 복구시켰습니다. 영상 아래쪽에 있습니다. 영상과 글 중 편한 방식으로 선택해서 진행하세요 : ) 마지..

webnautes.tistory.com

 

 

 

기본 예제. 맛보기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <opencv2/highgui.hpp>
 
void  main()
{
    cv::Mat  image(300400, CV_8UC1, cv::Scalar(200));
    cv::imshow("영상보기", image);
    cv::waitKey(0);
}
 
 
 
 
 
 
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

point

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <opencv2/opencv.hpp>
 
int  main()
{
    // Point_ 객체 선언 방식
    cv::Point_<int> pt1(100200);
    cv::Point_<float> pt2(92.3f, 125.23f);
    cv::Point_<double> pt3(100.2300.9);
 
    // Point_ 객체 간결 선언 방식
    cv::Point2i  pt4(12069);
    cv::Point2f  pt5(0.3f, 0.f), pt6(0.f, 0.4f);
    cv::Point2d  pt7(0.250.6);
 
    // Point_ 객체 연산 
    cv::Point    pt8 = pt1 + (cv::Point) pt2;
    cv::Point2f  pt9 = pt6 * 3.14f;
    cv::Point2d  pt10 = (pt3 + (cv::Point2d) pt6) * 10;
 
    std::cout << "pt8 = " << pt8.x << " , " << pt8.y << std::endl;
    std::cout << "[pt9] = " << pt9 << std::endl;
    std::cout << (pt2 == pt6) << std::endl;
    std::cout << "pt7과 pt8의 내적 : " << pt7.dot(pt8) << std::endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

size

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int  main()
{
    // Size_ 객체 기본 선언 방식
    Size_<int> sz1(100200);
    Size_<float> sz2(192.3f, 25.3f);
    Size_<double> sz3(100.230.9);
 
    // Size 객체 간결 선언 방식
    Size   sz4(12069);
    Size2f  sz5(0.3f, 0.f);
    Size2d  sz6(0.250.6);
 
    Point2d  pt1(0.250.6);
    Size2i   sz7 = sz1 + (Size2i)sz2;
    Size2d   sz8 = sz3 - (Size2d)sz4;
    Size2d   sz9 = sz5 + (Size2f)pt1;
 
    cout << "sz1.width = " << sz1.width;
    cout << ",  sz1.height = " << sz1.height << endl;
    cout << "sz1 넓이 " << sz1.area() << endl;
    cout << "[sz7] = " << sz7 << endl;
    cout << "[sz8] = " << sz8 << endl;
    cout << "[sz9] = " << sz9 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

rect

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int  main()
{
    Size2d  sz(100.560.6);
    Point2f  pt1(20.f, 30.f), pt2(100.f, 200.f);
 
    // Rect_ 객체 기본 선언 방식
    Rect_<int>    rect1(10103050);
    Rect_<float>   rect2(pt1, pt2);
    Rect_<double> rect3(Point2d(20.510), sz);
 
    // 간결 선언 방식 & 연산 적용
    Rect   rect4 = rect1 + (Point)pt1;
    Rect2f rect5 = rect2 + (Size2f)sz;
    Rect2d rect6 = rect1 & (Rect)rect2;
 
    // 결과 출력
    cout << "rect3 = " << rect3.x << "," << rect3.y << ", ";
    cout << rect3.width << "x" << rect3.height << endl;
    cout << "rect4 = " << rect4.tl() << "  " << rect4.br() << endl;
    cout << "rect5 크기 = " << rect5.size() << endl;
    cout << "[rect6] = " << rect6 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

vec

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int  main()
{
    // 기본 선언 및 간렬 방식
    Vec <int, 2> v1(512);
    Vec <double, 3> v2(40130.7125.6);
    Vec2b  v3(1010);
    Vec6f  v4(40.f, 230.25f, 525.6f);
    Vec3i  v5(200230250);
 
    // 객체 연산 및 형변환
    Vec3d v6 = v2 + (Vec3d)v5;
    Vec2b v7 = (Vec2b)v1 + v3;
    Vec6f v8 = v4 * 20.0f;
 
    Point  pt1 = v1 + (Vec2i)v7;
    Point3_<int> pt2 = v2;
 
    // 콘솔창 출력
    cout << "[v3] =  " << v3 << endl;
    cout << "[v7] =  " << v7 << endl;
    cout << "[v3 * v7] =  " << v3.mul(v7) << endl;
    cout << "v8[0] =  " << v8[0<< endl;
    cout << "v8[1] =  " << v8[1<< endl;
    cout << "v8[2] =  " << v8[2<< endl;
    cout << "[v2] =  " << v2 << endl;
    cout << "[pt2] =  " << pt2 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

scalar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int  main()
{
    Scalar_<uchar> red(00255);
    Scalar_<int>   blue(25500);
    Scalar_<double> color1(500);
    Scalar_<float>  color2(100.f, 200.f, 125.9f);
 
    Vec3d  green(00300.5);
    Scalar  green1 = color1 + (Scalar)green;
    Scalar  green2 = color2 + (Scalar_<float>)green;
 
    cout << "blue = " << blue[0<< ", " << blue[1];
    cout << ", " << blue[1<< ", " << blue[2<< endl;
    cout << "red =  " << red << endl;
    cout << "green =  " << green << endl << endl;
    cout << "green1 =  " << green1 << endl;
    cout << "green2 =  " << green2 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

회전사각형

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int  main()
{
    Mat  image(300500, CV_8UC1, Scalar(255));
    Point2f  center(250150), pts[4];
    Size2f  size(300100);
    RotatedRect  rot_rect(center, size, 20);
 
    Rect bound_rect = rot_rect.boundingRect();
    rectangle(image, bound_rect, Scalar(0), 1);
    circle(image, rot_rect.center, 1, Scalar(0), 2);
 
    for (int i = 0; i < 4; i++)
    {
        circle(image, pts[i], 4, Scalar(0), 1);
        line(image, pts[i], pts[(i + 1) % 4], Scalar(0), 2);
    }
 
    imshow("회전사각형", image);
    waitKey(0);
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

mat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
    float data[] = {
        1.2f, 2.3f, 3.2f,
        4.5f, 5.f, 6.5f,
    };
    // Mat 객체 선언 방법
    Mat m1(23, CV_8U);
    Mat m2(23, CV_8U, Scalar(300));
    Mat m3(23, CV_16S, Scalar(300));
    Mat m4(23, CV_32F, data);
 
    // Size 객체로 Mat 객체 선언 방법
    Size sz(23);
    Mat m5(Size(23), CV_64F);
    Mat m6(sz, CV_32F, data);
 
    cout << "[m1] =" << endl << m1 << endl;
    cout << "[m2] =" << endl << m2 << endl;
    cout << "[m3] =" << endl << m3 << endl;
    cout << "[m4] =" << endl << m4 << endl << endl;
    cout << "[m5] =" << endl << m5 << endl;
    cout << "[m6] =" << endl << m6 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

mat 클래스를 이용한 초기화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
    double a = 32.12345678, b = 2.7;
    short  c = 400;
    float  d = 10.f, e = 11.f, f = 13.f;
 
    Mat_<int>     m1(24);
    Mat_<uchar>     m2(34100);
    Mat_<short>       m3(45, c);
 
    m1 << 123456;
    Mat m4 = (Mat_<double>(23<< 123456);
    Mat m5 = (Mat_<float>(23<< a, b, c, d, e, f);
 
    cout << "[m1]=" << endl << m1 << endl;
    cout << "[m2]=" << endl << m2 << endl << endl;
    cout << "[m3]=" << endl << m3 << endl << endl;
    cout << "[m4]=" << endl << m4 << endl;
    cout << "[m5]=" << endl << m5 << endl;
    return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

 

 

+ Recent posts