opencv 基本繪圖函式總結
阿新 • • 發佈:2019-02-08
#include "opencv2/calib3d/calib3d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv; using namespace std; void DrawTransRec(Mat mat, int x, int y, int width, int height, CvScalar color, double alpha); void DrawTransRec(Mat mat, int x, int y, int width, int height, CvScalar color, double alpha) { IplImage * img = &(IplImage)mat; IplImage * rec = cvCreateImage(cvSize(width, height), img->depth, img->nChannels); //if(x>0&&y>0&&x<mat.cols&&y<mat.){ cv::Rect roi = cv::Rect(x, y, width, height) & cv::Rect(0, 0, mat.cols, mat.rows); if (roi.area() != width*height)return; cvRectangle(rec, cvPoint(0, 0), cvPoint(width, height), color, -1); cvSetImageROI(img, cvRect(roi.x, roi.y, roi.width, roi.height)); cvAddWeighted(img, alpha, rec, 1 - alpha, 0.0, img); cvResetImageROI(img); cvReleaseImage( &rec); } int main(int argc, char** argv) { Mat picture(500, 500, CV_8UC3, Scalar(255, 255, 255, 0.5)); // 畫箭頭 Point point1 = Point(0, 0); Point point2 = Point(100, 100); // 2表示線寬 8 表示線型 0 暫時不清楚 0.1表示箭頭的長度 arrowedLine(picture, point1, point2, Scalar(0, 255, 0), 2,8,0,0.1); //畫直線 point1 = Point(0, 100); point2 = Point(100, 200); // 2表示線寬 8 表示線型 line(picture, point1, point2, Scalar(0, 0, 255), 2, 8, 0); //畫圓 int r = 50;//半徑 Point center = Point(150, 50); //其中2改為-1 表示填充 circle(picture, center, r, Scalar(123, 21, 32), 2,8,0); //畫矩形 Rect rec(300,300,150,150); rectangle(picture, rec, Scalar(123, 21, 32), 2, 8, 0); //clipLine的作用是剪下影象矩形區域內部的直線 point1 = Point(250, 250); point2 = Point(450, 450); line(picture, point1, point2, Scalar(0, 255, 0), 2, 8, 0); clipLine(rec, point1, point2); line(picture, point1, point2, Scalar(0, 0, 255), 2, 8, 0); //輸入文字 Point textPt(210, 50); String text = "drawtext"; int fontFace = FONT_HERSHEY_PLAIN; double fontScale = 2; int thickness = 2; putText(picture, text, textPt, fontFace, fontScale, Scalar(0, 111, 255), thickness); //getTextSize()返回一個指定字型以及大小的String所佔的空間 int baseline = 0; Size textSize = getTextSize(text, fontFace,fontScale, thickness, &baseline); rectangle(picture, Point(textPt.x, textPt.y - textSize.height), Point(textPt.x + textSize.width, textPt.y + thickness/2), Scalar(0, 255, 0, 1), 1); //畫標記 //目前支援六種標記 //MARKER_TILTED_CROSS,MARKER_STAR, MARKER_DIAMOND, MARKER_SQUARE ,MARKER_TRIANGLE_UP, MARKER_TRIANGLE_DOWN drawMarker(picture, Point(480, 50), Scalar(0, 0, 255), MARKER_TILTED_CROSS); //畫橢圓 Size 軸(20, 50);//軸線長度,橫著20,豎著50 ellipse(picture, Point(50, 150), 軸, 20.0, 0.0, 300.0, Scalar(55, 123, 222), -1); //引數說明,axes表示軸,20.0表示整個橢圓旋轉20度,從0開始畫到300度 //過載函式利用了RotatedRect,表示在一個旋轉矩形裡畫最大橢圓 RotatedRect box1(Point(150, 150), Size(40, 100), -20);//負的為逆時針 ellipse(picture, box1, Scalar(122, 122, 122)); vector<Point> pointstest; ellipse2Poly(Point(50, 150), 軸, 20.0, 0.0, 300.0, 1, pointstest); //畫多邊形 Point points[2][20]; points[0][0] = Point(300, 100); points[0][1] = Point(400, 100); points[0][2] = Point(450, 200); points[0][3] = Point(350, 250); points[0][4] = Point(250, 200); points[1][0] = Point(300, 100) + Point(-200, +150); points[1][1] = Point(400, 100) + Point(-200, +150); points[1][2] = Point(450, 200) + Point(-200, +150); points[1][3] = Point(350, 250) + Point(-200, +150); points[1][4] = Point(250, 200) + Point(-200, +150); const Point* pt[2] = { points[0], points[1] }; int npt[2] = { 5,5 }; polylines(picture, pt, npt, 2, 1, Scalar(0, 0, 255), 3); fillPoly(picture, pt, npt, 1, Scalar(250, 255, 0), 8); //繪製半透明矩形 DrawTransRec(picture, 50, 320, 400, 100, Scalar(255, 122, 122), 0.2); imshow("show", picture); waitKey(0); return 0; }