OpenCV画椭圆、实心圆、凹多边形、线段、矩形
OpenCV畫(huà)橢圓、實(shí)心圓、凹多邊形、線段、矩形
author@jason_ql
http://blog.csdn.net/lql0716
1、 實(shí)例代碼
#include <QCoreApplication> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/opencv.hpp>using namespace cv; using namespace std;#define WINDOW_WIDTH 600 //定義窗口大小的宏 #define WINDOW_NAME1 "【繪制圖1】" //為窗口標(biāo)題定義的宏 #define WINDOW_NAME2 "【繪制圖2】" //為窗口標(biāo)題定義的宏//------------------------------------------------------------- // OpenCV畫(huà)橢圓、實(shí)心圓、凹多邊形、線段、矩形 //-------------------------------------------------------------//畫(huà)橢圓 void drawEllipse(cv::Mat img, double angle){int thickness = 2;int lineType = 8;cv::ellipse(img,cv::Point(WINDOW_WIDTH/2, WINDOW_WIDTH/2),cv::Size(WINDOW_WIDTH/4, WINDOW_WIDTH/16),angle,0,360,cv::Scalar(255,129,0),thickness,lineType); }//畫(huà)實(shí)心圓 void drawFilledCircle(cv::Mat img, cv::Point center){int thickness = -1;int lineType = 8;cv::circle(img,center,WINDOW_WIDTH/32,cv::Scalar(0,0,255),thickness,lineType); }//畫(huà)凹多邊形 void drawPolygon(cv::Mat img){int lineType = 8;//創(chuàng)建一些點(diǎn)cv::Point rookPoints[1][20];rookPoints[0][0] = cv::Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][1] = cv::Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);rookPoints[0][2] = cv::Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);rookPoints[0][3] = cv::Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][4] = cv::Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][5] = cv::Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][6] = cv::Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][7] = cv::Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][8] = cv::Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][9] = cv::Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][10] = cv::Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][11] = cv::Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][12] = cv::Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][13] = cv::Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);rookPoints[0][14] = cv::Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);rookPoints[0][15] = cv::Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);rookPoints[0][16] = cv::Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);rookPoints[0][17] = cv::Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);rookPoints[0][18] = cv::Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);rookPoints[0][19] = cv::Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);const cv::Point* ppt[1] = {rookPoints[0]};int npt[] = {20};cv::fillPoly(img,ppt,npt,1,cv::Scalar(255, 255, 255),lineType);}//畫(huà)線 void drawLine(cv::Mat img, cv::Point start, cv::Point end){int thickness = 2;int lineType = 8;cv::line(img,start,end,cv::Scalar(0,0,0),thickness,lineType); }int main(void){//創(chuàng)建空白的Mat圖像cv::Mat atomImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);cv::Mat rookImage = cv::Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);//繪制橢圓drawEllipse(atomImage, 90);drawEllipse(atomImage, 0);drawEllipse(atomImage, 45);drawEllipse(atomImage, -45);//繪制圓心drawFilledCircle(atomImage, cv::Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));//繪制橢圓drawPolygon(rookImage);//繪制矩形cv::rectangle(rookImage,cv::Point(0, 7 * WINDOW_WIDTH / 8),cv::Point(WINDOW_WIDTH, WINDOW_WIDTH),cv::Scalar(0, 255, 255),-1,8);//繪制一些線段drawLine(rookImage, cv::Point(0, 15 * WINDOW_WIDTH / 16), cv::Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));drawLine(rookImage, cv::Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), cv::Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));drawLine(rookImage, cv::Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8), cv::Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));drawLine(rookImage, cv::Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8), cv::Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));//顯示繪制出的圖像cv::imshow(WINDOW_NAME1, atomImage);cv::moveWindow(WINDOW_NAME1, 0, 200);cv::imshow(WINDOW_NAME2, rookImage);cv::moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);cv::waitKey(0);return(0);}2、 ellipse()畫(huà)橢圓
Draws a simple or thick elliptic arc or fills an ellipse sector.
C++: void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
C++:void ellipse(Mat& img, const RotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)
Python:cv2.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]]) → None
Python:cv2.ellipse(img, box, color[, thickness[, lineType]]) → None
C:void cvEllipse(CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python: cv.Ellipse(img, center, axes, angle, start_angle, end_angle, color, thickness=1, lineType=8, shift=0) → None
C:void cvEllipseBox(CvArr* img, CvBox2D box, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python:cv.EllipseBox(img, box, color, thickness=1, lineType=8, shift=0) → None
Parameters:
The functions ellipse with less parameters draw an ellipse outline, a filled ellipse, an elliptic arc, or a filled ellipse sector. A piecewise-linear curve is used to approximate the elliptic arc boundary. If you need more control of the ellipse rendering, you can retrieve the curve using ellipse2Poly() and then render it with polylines() or fill it with fillPoly() . If you use the first variant of the function and want to draw the whole ellipse, not an arc, pass startAngle=0 and endAngle=360 . The figure below explains the meaning of the parameters.
3、 circle()畫(huà)實(shí)心圓
Draws a circle.
C++:void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Python: cv2.circle(img, center, radius, color[, thickness[, lineType[, shift]]]) → None
C:void cvCircle(CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python: cv.Circle(img, center, radius, color, thickness=1, lineType=8, shift=0) → None
Parameters: img – Image where the circle is drawn.center – Center of the circle.radius – Radius of the circle.color – Circle color.thickness – Thickness of the circle outline, if positive. Negative thickness means that a filled circle is to be drawn.lineType – Type of the circle boundary. See the line() description.shift – Number of fractional bits in the coordinates of the center and in the radius value.The function circle draws a simple or filled circle with a given center and radius.
4、 fillpoly()畫(huà)凹多邊形
Fills the area bounded by one or more polygons.
C++:void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int lineType=8, int shift=0, Point offset=Point() )
Python:cv2.fillPoly(img, pts, color[, lineType[, shift[, offset]]]) → None
C:void cvFillPoly(CvArr* img, CvPoint** pts, const int* npts, int contours, CvScalar color, int line_type=8, int shift=0 )
Python: cv.FillPoly(img, polys, color, lineType=8, shift=0) → None
Parameters: img – Image.pts – Array of polygons where each polygon is represented as an array of points.npts – Array of polygon vertex counters.ncontours – Number of contours that bind the filled region.color – Polygon color.lineType – Type of the polygon boundaries. See the line() description.shift – Number of fractional bits in the vertex coordinates.offset – Optional offset of all points of the contours.The function fillPoly fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections (some of their parts), and so forth.
5、 line()畫(huà)線
Draws a line segment connecting two points.
C++:void line(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
Python:cv2.line(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None
C:void cvLine(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python:cv.Line(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) → None
Parameters: img – Image.pt1 – First point of the line segment.pt2 – Second point of the line segment.color – Line color.thickness – Line thickness.lineType –Type of the line:8 (or omitted) - 8-connected line.4 - 4-connected line.CV_AA - antialiased line.shift – Number of fractional bits in the point coordinates.The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, you may use the macro CV_RGB(r, g, b) .
6、 rectangle()畫(huà)矩形
Draws a simple, thick, or filled up-right rectangle.
C++:void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
C++:void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )
Python:cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None
C:void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )
Python:cv.Rectangle(img, pt1, pt2, color, thickness=1, lineType=8, shift=0) → None
Parameters: img – Image.pt1 – Vertex of the rectangle.pt2 – Vertex of the rectangle opposite to pt1 .rec – Alternative specification of the drawn rectangle.color – Rectangle color or brightness (grayscale image).thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.lineType – Type of the line. See the line() description.shift – Number of fractional bits in the point coordinates.The function rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2, or r.tl() and r.br()-Point(1,1).
7、 更多相關(guān)函數(shù)見(jiàn)官方文檔
Drawing Functions
總結(jié)
以上是生活随笔為你收集整理的OpenCV画椭圆、实心圆、凹多边形、线段、矩形的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Xenu软件检查网站死链接
- 下一篇: 窑洞