[OpenCV3程式設計入門讀書筆記]基本影象繪製(4)
阿新 • • 發佈:2018-12-04
目錄
用於繪製填充多邊形的fillPoly函式和不填充多邊形polylines函式;
每個函式第一個部分是原始碼中的函式定義,第二個部分輔以一個小例子。
用於繪製直線的line函式;
/** @brief Draws a line segment connecting two points. 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. @param img Image. @param pt1 First point of the line segment. @param pt2 Second point of the line segment. @param color Line color. @param thickness Line thickness. @param lineType Type of the line. See #LineTypes. @param shift Number of fractional bits in the point coordinates. */ CV_EXPORTS_W void line(InputOutputArray img, Point pt1, Point pt2, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
例子:
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace cv; int main() { Mat image = Mat::zeros(300, 300, CV_8UC3); line(image, Point(50, 50), Point(250, 250), Scalar(0, 255, 0), 2, 8); line(image, Point(250, 50), Point(50, 250), Scalar(0, 255, 0), 2, 8); imshow("繪製圖像", image); waitKey(0); }
用於繪製橢圓的ellipse函式;
/** @param img Image. @param center Center of the ellipse. @param axes Half of the size of the ellipse main axes. @param angle Ellipse rotation angle in degrees. @param startAngle Starting angle of the elliptic arc in degrees. @param endAngle Ending angle of the elliptic arc in degrees. @param color Ellipse color. @param thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn. @param lineType Type of the ellipse boundary. See #LineTypes @param shift Number of fractional bits in the coordinates of the center and values of axes. */ CV_EXPORTS_W void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0); /** @overload @param img Image. @param box Alternative ellipse representation via RotatedRect. This means that the function draws an ellipse inscribed in the rotated rectangle. @param color Ellipse color. @param thickness Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn. @param lineType Type of the ellipse boundary. See #LineTypes */ CV_EXPORTS_W void ellipse(InputOutputArray img, const RotatedRect& box, const Scalar& color, int thickness = 1, int lineType = LINE_8);
例子:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main() {
Mat image = Mat::zeros(300, 300, CV_8UC3);
ellipse(image, Point(150, 150), Size(100, 100), 45, 0, 360, Scalar(0, 0, 255), 2, 8);
ellipse(image, Point(100, 130), Size(25, 50), 180, 180, 0, Scalar(0, 0, 255), 2, 8);
ellipse(image, Point(200, 130), Size(25, 50), 180, 180, 0, Scalar(0, 0, 255), 2, 8);
ellipse(image, Point(150, 175), Size(50, 30), 0, 180, 0, Scalar(0, 0, 255), 2, 8);
imshow("繪製圖像", image);
waitKey(0);
}
用於繪製矩形的rectangle函式;
/** @brief Draws a simple, thick, or filled up-right rectangle.
The function cv::rectangle draws a rectangle outline or a filled rectangle whose two opposite corners
are pt1 and pt2.
@param img Image.
@param pt1 Vertex of the rectangle.
@param pt2 Vertex of the rectangle opposite to pt1 .
@param color Rectangle color or brightness (grayscale image).
@param thickness Thickness of lines that make up the rectangle. Negative values, like #FILLED,
mean that the function has to draw a filled rectangle.
@param lineType Type of the line. See #LineTypes
@param shift Number of fractional bits in the point coordinates.
*/
CV_EXPORTS_W void rectangle(InputOutputArray img, Point pt1, Point pt2,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
/** @overload
use `rec` parameter as alternative specification of the drawn rectangle: `r.tl() and
r.br()-Point(1,1)` are opposite corners
*/
CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
例子:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main() {
Mat image = Mat::zeros(300, 300, CV_8UC3);
rectangle(image,Rect(100,100,100,100),Scalar(255,0,0),1,8,0);
rectangle(image, Rect(75, 75, 150, 150), Scalar(255, 0, 0), 1, 8, 0);
rectangle(image, Rect(50, 50, 200, 200), Scalar(255, 0, 0), 1, 8, 0);
imshow("繪製圖像", image);
waitKey(0);
}
用於繪製圓形的circle函式;
/** @example samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_2.cpp
An example using drawing functions
*/
/** @brief Draws a circle.
The function cv::circle draws a simple or filled circle with a given center and radius.
@param img Image where the circle is drawn.
@param center Center of the circle.
@param radius Radius of the circle.
@param color Circle color.
@param thickness Thickness of the circle outline, if positive. Negative values, like #FILLED,
mean that a filled circle is to be drawn.
@param lineType Type of the circle boundary. See #LineTypes
@param shift Number of fractional bits in the coordinates of the center and in the radius value.
*/
CV_EXPORTS_W void circle(InputOutputArray img, Point center, int radius,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);
例子:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main() {
Mat image = Mat::zeros(300, 300, CV_8UC3);
circle(image, Point(150, 150), 25, Scalar(0, 255, 0));
circle(image, Point(120, 150), 25, Scalar(0, 255, 0));
circle(image, Point(180, 150), 25, Scalar(0, 255, 0));
circle(image, Point(130, 170), 25, Scalar(0, 255, 0));
circle(image, Point(170, 170), 25, Scalar(0, 255, 0));
imshow("繪製圖像", image);
waitKey(0);
}
用於繪製填充多邊形的fillPoly函式和不填充多邊形polylines函式;
/** @overload */
CV_EXPORTS void fillPoly(Mat& img, const Point** pts,
const int* npts, int ncontours,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
/** @example samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp
An example using drawing functions
Check @ref tutorial_random_generator_and_text "the corresponding tutorial" for more details
*/
/** @brief Fills the area bounded by one or more polygons.
The function cv::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.
@param img Image.
@param pts Array of polygons where each polygon is represented as an array of points.
@param color Polygon color.
@param lineType Type of the polygon boundaries. See #LineTypes
@param shift Number of fractional bits in the vertex coordinates.
@param offset Optional offset of all points of the contours.
*/
CV_EXPORTS_W void fillPoly(InputOutputArray img, InputArrayOfArrays pts,
const Scalar& color, int lineType = LINE_8, int shift = 0,
Point offset = Point() );
/** @overload */
CV_EXPORTS void polylines(Mat& img, const Point* const* pts, const int* npts,
int ncontours, bool isClosed, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0 );
/** @brief Draws several polygonal curves.
@param img Image.
@param pts Array of polygonal curves.
@param isClosed Flag indicating whether the drawn polylines are closed or not. If they are closed,
the function draws a line from the last vertex of each curve to its first vertex.
@param color Polyline color.
@param thickness Thickness of the polyline edges.
@param lineType Type of the line segments. See #LineTypes
@param shift Number of fractional bits in the vertex coordinates.
The function cv::polylines draws one or more polygonal curves.
*/
CV_EXPORTS_W void polylines(InputOutputArray img, InputArrayOfArrays pts,
bool isClosed, const Scalar& color,
int thickness = 1, int lineType = LINE_8, int shift = 0 );
例子:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
int main() {
Mat image = Mat::zeros(300, 300, CV_8UC3);
Point root_points[1][6];
root_points[0][0] = Point(15, 220);
root_points[0][1] = Point(60, 225);
root_points[0][2] = Point(46, 250);
root_points[0][3] = Point(35, 46);
root_points[0][4] = Point(20, 90);
root_points[0][5] = Point(96, 100);
const Point* ppt[1] = { root_points[0] };
int npt[] = { 6 };
fillPoly(image, ppt, npt, 1, Scalar(255, 0, 0), 8, 0);
polylines(image, ppt, npt, 1, 1, Scalar(0, 0, 255), 1, 8, 0);
imshow("繪製圖像", image);
waitKey(0);
}