※ 행렬 데이터 생성 = 메모리를 새로 생성하지 않고 공유함
// y-행에 대한 행렬 헤더 생성
cv::row(int y)
// x-열에 대한 행렬 헤더 생성
cv:col(int x)
// y-행에 대한 행렬 헤더 생성 cv::row(int y) // x-열에 대한 행렬 헤더 생성 cv:col(int x) // 부분 행렬 헤더 생성 cv::Mat cv::colRange(int startcol, int endcol) const cv::Mat cv::rowRange(int startrow, int endrow) const // 전치 행렬 변환 cv::MatExpr cv::Mat::t() const
// 이미지 파일 읽기 cv::Mat image_color = cv::imread("image.jpg"); cv::Mat image_gray = cv::imread("image.jpg", 0); // 생성 // 1채널 unsigned char cv::Mat mat(h, w, CV_8UC1); // 3채널 unsigned char cv::Mat mat(h, w, CV_8UC3); cv::Mat mat = cv::Mat::zeros(h, w, CV_32FC1); cv::Mat mat = cv::Mat::ones(h, w, CV_64FC3); unsigned char *pbBuffer; // 이미지 버퍼 cv::Mat mat(h, w, CV_8UC3, pbBuffer); // 메모리 공유 // 원소 초기화 cv::Mat mat(h, w, CV_8UC1); // 모든 원소값을 3으로 초기화 mat = cv::Scalar(3); cv::Mat mat2 = mat; // 메모리 공유 cv::Mat mat3 = mat.clone(); // 별도 메모리 cv::Mat mat4; mat.copyTo(mat4); // 별도 메모리 // 형변환 복사 cv::Mat matf(h, w, CV_32FC1); cv::Mat matb; matf.convertTo(matb, CV_8U); // color-gray 변환 cv::cvtColor(color, gray, CV_BGR2GRAY); cv::cvtColor(gray, color, CV_GRAY2BGR); // ROI cv::Rect roi; cv::Mat mat_roi = mat(roi); cv::Mat mat_roi = mat(roi).clone(); // Resize cv::resize(src, dst, cv::Size(w2, h2)); // Scale(sx, sy) cv::resize(src, dst, cv::Size(), sx, sy); // Flip cv::flip(src, dst, 0); // vertical flip cv::flip(src, dst, 1); // horizontal flip cv::flip(src, dst, -1); // vertical & horizontal flip // Drawing cv::Rect rc(x, y, w, h); cv::Scalar color(B, G, R); int thickness = 1; cv::line(img, cv::Point(x1, y1), cv::Point(x2, y2), color, thickness); cv::Rectangle(img, rc, color, thickness); cv::Rectangle(img, rc.tl(), rc.br(), color, thickness); cv::Rectangle(img, rc, color, CV_FILLED); cv::Point center(rc.x + rc.width / 2, rc.y + rc.height / 2); cv::Size radius(rc.width / 2, rc.height / 2); double rot_deg = 0; // rotation of ellipse double s_deg = 0; // start angle of arc double e_deg = 360; // end angle of arc cv::ellipse(img, center, radius, rot_deg, s_deg, e_deg, color, thickness); cv::ellipse(img, center, radius, rot_deg, s_deg, e_deg, color, CV_FILLED); int circle_radius = 10; cv::circle(img, center, circle_radius, color, thickness); cv::circle(img, center, circle_radius, color, CV_FILLED); cv::putText(img, "text", cv::Point(x, y), FONT_HERSHEY_SIMPLEX, 1, color, thickness); cv::putText(img, "text", cv::Point(x, y), FONT_HERSHEY_DUPLEX, 1, color, thickness); // display cv::namedWindow("name"); // auto resized cv::namedWindow("name", CV_WINDOW_NORMAL); // manual resize cv::imshow("name", img); char ch = cv::waitKey(); // 무한대기 char ch = cv::waitKey(10); // 10 ms 대기 cv::destroyWindow("name"); cv::destroyAllWindows();
cv::VideoCapture vc(0); // webcam cv::VideoCapture vc("video.mp4"); // video file if (!vc.isOpened()) return; // failed vc.set(CV_CAP_PROP_FRAME_WIDTH, 640); vc.set(CV_CAP_PROP_FRAME_HEIGHT, 480); cv::Mat img; for (;;) { vc >> img; if (img.empty()) break; cv::imshow("image", img); if (cv::waitKey(10) == 27) // ESC break; } cv::destroyAllWindows();
double fps = 30; int fourcc = CV_FOURCC('X', 'V', 'I', 'D'); // XVID codec bool isColor = true; cv::VideoWriter* video = new cv::VideoWriter; if (!video->open("video.avi", fourcc, fps, cv::Size(w, h), isColor)) { delete video; return; } cv::Mat img; for (;;) { ; *video << img; ; } delete video;
// cv::Mat -> cv::IplImage cv::Mat img; cv::IplImage* pIplImg = &IplImage(img); // cv::IplImage -> cv::Mat IplImage* pIplImg; cv::Mat img(pIplImg); // 3 channels image for (int r = 0; r < img.rows; ++r) { for (int c = 0; c < img.cols; ++c) { img.at<Vec3b>(r, c)[0] += 10; img.at<Vec3b>(r, c)[1] += 20; img.at<Vec3b>(r, c)[2] += 30; } } // cv::Mat_<T> cv::Mat_<unsigned char> img2 = img; img2(r, c) = v;