2021. 5. 14. 17:11 .Net 교육/OpenCV(C++)
OpenCV(C++) 사용하기
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
// 행렬의 약자.
// 사용은 할수 있는 형태이다.
//Mat image;
// 200 : 가로 해상도
// 300 : 세로 해상도
// CV_8U : 8bit unsigned char(gray)
// 255 (세로, 가로)
Mat image(200, 300, CV_8U, Scalar(255));
cout << "행 개수 : " << image.rows << endl;
cout << "열 개수 : " << image.cols << endl;
// 윈도우 창 생성
string title = "윈도우";
namedWindow(title, WINDOW_AUTOSIZE);
moveWindow(title, 100, 200);
imshow(title, image);
//블로킹 용도로 사용
waitKey();
}
Mat image(600, 800, CV_8U, Scalar(0)); // 0은 검은색 255는 흰색
Point 활용
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
//제너릭 방식
Point_<int> pt1(100, 200);
cout << pt1 << endl; // [100,200]
cout << pt1.x << endl; // 100
cout << pt1.y << endl; // 200
Point_<float> pt2(10.12f, 20.20f);
cout << pt2 << endl; // [10.12,20.2]
cout << pt2.x << endl; // 10.12
cout << pt2.y << endl; // 20.2
//2i 가 정수타입
// typedef Point_<int> Point2i;
Point2i pt3(100, 200);
cout << pt3 << endl; // [100,200]
cout << pt3.x << endl; // 100
cout << pt3.y << endl; // 200
Point2i a(3, 4), b(4, 5);
Point2i c = a + b; // 벡터 합
cout << c << endl;
Point2i d = c * 2;
cout << d << endl;
//int e = a.dot(b); // 내적
cout << a.dot(b) << endl;
Point f(1, 2), g(1, 2);
cout << (f == g) << endl; //비교 가능
Point h(1, 2);
Point2f i(1.0f, 2.0f);
Point j = h + (Point)i;
cout << j << endl; // [2, 4]
Point3i k(1, 2, 3);
cout << k << endl; // [1, 2, 3]
}
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
Size t;
Size_<int> a(10, 20);
cout << a << endl;
Size2i b(10, 20);
cout << b.width << endl;
cout << b.height << endl;
cout << b.area() << endl;
}
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
/*
_Tp x; //!< x coordinate of the top-left corner
_Tp y; //!< y coordinate of the top-left corner
_Tp width; //!< width of the rectangle
_Tp height; // !< height of the rectangle
*/
Rect t(10, 20, 30, 40);
cout << t << endl;
cout << t.x << endl;
cout << t.y << endl;
cout << t.width << endl;
cout << t.height << endl;
Rect s(Point(1, 2), Point(3, 4));
cout << s << endl;
}
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(void)
{
Vec<int, 5> v1(1, 2, 3, 4, 5);
cout << v1 << endl;
Vec3i v2(1, 2, 3);
Vec3i v3(3, 6, 7);
Vec3i v4 = v2 + v3;
cout << v4 << endl;
cout << v4[0] << endl;
cout << v2 * 3 << endl;
}
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
void onMouse(int e, int x, int y, int f, void *p)
{
switch ( e)
{
case EVENT_LBUTTONDOWN:
cout << 1<< ":" <<*(int *)p << endl;
break;
case EVENT_RBUTTONDOWN:
cout << 2 << endl;
break;
case EVENT_LBUTTONUP:
cout << 3 << endl;
break;
case EVENT_RBUTTONUP:
cout << 4 << endl;
break;
case EVENT_MOUSEMOVE:
cout << x<< " " << y << endl;
break;
default:
break;
}
}
int main(void)
{
// B G R
Scalar red(0, 0, 255);
cout << red << endl; // [0, 0, 255, 0]
Mat image(200, 300, CV_8U);
image.setTo(255);
// UI
imshow("마우스", image);
// event1
int num = 10;
setMouseCallback("마우스", onMouse, &num);
waitKey();
}
'.Net 교육 > OpenCV(C++)' 카테고리의 다른 글
OpenCV(C++) - 행렬 연산 02 (0) | 2021.05.21 |
---|---|
OpenCV(C++) - 행렬 연산 01 (0) | 2021.05.20 |
OpenCV(C++) - User Interface 02 (0) | 2021.05.18 |
OpenCV(C++) - User Interface 01 (0) | 2021.05.17 |
OpenCV(C++) - 기본데이터 타입 (0) | 2021.05.17 |