2021. 5. 18. 13:19 .Net 교육/OpenCV(C++)
OpenCV(C++) - User Interface 02
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
string title = "Event Drawing";
Mat image;
void onMouse(int event, int x, int y, int flags, void* param)
{
static Point pt(-1, -1);
switch (event)
{
case EVENT_LBUTTONDOWN:
if (pt.x < 0)
pt = Point(x, y);
else
{
rectangle(image, pt, Point(x, y), Scalar(50), 2);
imshow(title, image);
pt = Point(-1, -1);
}
break;
case EVENT_RBUTTONDOWN:
if (pt.x < 0)
pt = Point(x, y);
else
{
Point2d pt2 = pt - Point(x, y);
int radius = (int)sqrt(pt2.x * pt2.x + pt2.y * pt2.y);
circle(image, pt, radius, Scalar(150), 2);
imshow(title, image);
pt = Point(-1, -1);
}
break;
}
}
int main(void)
{
image = Mat(300, 500, CV_8UC1, Scalar(255));
imshow(title, image);
setMouseCallback(title, onMouse, 0);
waitKey(0);
return 0;
}
별 그리기
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
using namespace std;
string title = "Star Drawing";
Mat image(400, 600, CV_8UC3, Scalar(255, 255, 255));
int main(void)
{
int num = 5;
int radius = 100;
double deg = 360 / num; // degree
double rad = deg / 180.0 * 3.141592;
Point pt_zero = Point(300, 200);
Point* pt;
double pt_rad = 0;
pt = new Point[num];
for (int i = 0; i < num; i++)
{
pt[i].x = pt_zero.x + (radius * cos(pt_rad));
pt[i].y = pt_zero.y + (radius * sin(pt_rad));
pt_rad += rad;
}
for (int i = 0; i < num; i++)
{
int j = i + 2;
j %= num;
line(image, pt[i], pt[j], Scalar(255, 0, 0), 4, LINE_AA);
}
line(image, pt_zero, pt_zero, Scalar(255, 0, 0), 4, LINE_AA);
imshow(title, image);
waitKey(0);
return 0;
}
#include <opencv2/opencv.hpp>
#include <cmath>
using namespace cv;
using namespace std;
string title = "Star Drawing";
Mat image(400, 600, CV_8UC3, Scalar(255, 255, 255));
int main(void)
{
Mat img = imread("Image/cat1.jpg", IMREAD_COLOR);
cout << format("dims(%d)", img.dims) << endl; // 차원
cout << format("width(%d)", img.size().width) << endl; // 가로
cout << format("height(%d)", img.size().height) << endl; // 세로
cout << format("rows(%d)", img.rows) << endl; // 가로
cout << format("cols(%d)", img.cols) << endl; // 세로
cout << img.size() << endl; //
cout << format("depth(%d)", img.depth()) << endl; //
cout << format("channels(%d)", img.channels()) << endl; // 점의 개수
cout << format("total(%d)", img.total()) << endl; // 점의 개수
cout << img.rows * img.cols << endl; // 점 1개당 바이트 수
cout << format("elemSize(%d)", img.elemSize()) << endl; // 점 1개의 칼라 바이트 수
cout << "type" << img.type() << " " << CV_8UC3 << endl; //
Rect roi(0, 0, 3, 2); // Region of Interest
cout << img(roi) << endl;
imshow(title, img);
waitKey(0);
return 0;
}
그레이스케일 값 넣어보기
int main(void)
{
Mat img = imread("image/cat1.jpg", IMREAD_COLOR);
for (int row = 0; row < img.rows; row++)
{
for (int col = 0; col < img.cols; col++)
{
Vec3b v = img.at<Vec3b>(row, col);
uchar value = (uchar)((double)v[0] * 0.11 + (double)v[1] * 0.3 + (double)v[2] * 0.59);
Vec3b result(value, value, value);
img.at<Vec3b>(row, col) = result;
}
}
imshow("img", img);
waitKey();
}
파일 압축 복사하기
int main(void)
{
Mat img = imread("Image/cat1.jpg", IMREAD_COLOR);
vector<int> att;
att.push_back(IMWRITE_JPEG_QUALITY);
att.push_back(25); // 95
imwrite("Image/cat1_001.jpg", img); // 파일 복사하기
imwrite("Image/cat1_002.jpg", img, att); // 파일 복사하기
waitKey(0);
return 0;
}
int main(void)
{
Mat img1 = imread("Image/고기.jpg", IMREAD_COLOR);
Mat img2;
flip(img1, img2, 0); // 0 : x 축
Mat img3;
flip(img1, img3, 1); // 1 : y 축
Mat img4;
flip(img1, img4, -1); // -1 : 원점대칭
Mat img5;
repeat(img1, 2, 3, img5);//2행3열만큼 반복(6개)
Mat img6;
transpose(img1, img6); //90도 회전전치
imshow("img1", img1);
imshow("img2", img2);
imshow("img3", img3);
imshow("img4", img4);
imshow("img5", img5);
imshow("img6", img6);
waitKey(0);
return 0;
}
flip 사용하지 않고 90도 회전, 상하좌우 만들기
Mat img7(img1.rows, img1.cols, CV_8UC3);
Mat img8(img1.cols, img1.rows, CV_8UC3);
for (int rows = 0; rows < img1.rows; rows++)
{
for (int cols = 0; cols < img1.cols; cols++)
{
Vec3b v = img1.at<Vec3b>(rows, cols);
img7.at<Vec3b>(rows, img1.cols - 1 - cols) = v;
img8.at<Vec3b>(img1.cols - 1 - cols, rows) = v;
}
}
imshow("img7", img7);
imshow("img8", img8);
'.Net 교육 > OpenCV(C++)' 카테고리의 다른 글
OpenCV(C++) - 행렬 연산 02 (0) | 2021.05.21 |
---|---|
OpenCV(C++) - 행렬 연산 01 (0) | 2021.05.20 |
OpenCV(C++) - User Interface 01 (0) | 2021.05.17 |
OpenCV(C++) - 기본데이터 타입 (0) | 2021.05.17 |
OpenCV(C++) 사용하기 (0) | 2021.05.14 |