#python code
import cv2
import numpy as np
def drawLines():
img = np.full((400, 400, 3), (255, 255, 255), dtype=np.uint8)
cv2.line(img, (50, 50), (200, 50), (0, 0, 255))
cv2.line(img, (50, 100), (200, 100), (255, 0, 255), 3)
cv2.line(img, (50, 150), (200, 150), (255, 0, 0), 10)
cv2.line(img, (250, 50), (350, 100), (0, 0, 255), 1, cv2.LINE_4)
cv2.line(img, (250, 70), (350, 120), (255, 0, 255), 1, cv2.LINE_8)
cv2.line(img, (250, 90), (350, 140), (255, 0, 0), 1, cv2.LINE_AA)
cv2.arrowedLine(img, (50, 200), (150, 200), (0, 0, 255), 1)
cv2.arrowedLine(img, (50, 250), (350, 250), (255, 0, 255), 1)
cv2.arrowedLine(img, (50, 300), (350, 300), (255, 0, 0), 1, tipLength=0.05)
cv2.drawMarker(img, (50, 350), (0, 0, 255), markerType=cv2.MARKER_CROSS)
cv2.drawMarker(img, (100, 350), (0, 0, 255), markerType=cv2.MARKER_TILTED_CROSS)
cv2.drawMarker(img, (150, 350), (0, 0, 255), markerType=cv2.MARKER_STAR)
cv2.drawMarker(img, (200, 350), (0, 0, 255), markerType=cv2.MARKER_DIAMOND)
cv2.drawMarker(img, (250, 350), (0, 0, 255), markerType=cv2.MARKER_SQUARE)
cv2.drawMarker(img, (300, 350), (0, 0, 255), markerType=cv2.MARKER_TRIANGLE_UP)
cv2.drawMarker(img, (350, 350), (0, 0, 255), markerType=cv2.MARKER_TRIANGLE_DOWN)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def drawPolys():
img = np.full((400, 400, 3), (255, 255, 255), dtype=np.uint8)
cv2.rectangle(img, (50, 50, 100, 50), (0, 0, 255), 2)
cv2.rectangle(img, (50, 150, 100, 50), (0, 0, 128), -1)
cv2.circle(img, (300, 120), 30, (255, 255, 0), -1, cv2.LINE_AA)
cv2.circle(img, (300, 120), 60, (255, 0, 0), 3, cv2.LINE_AA)
cv2.ellipse(img, (120, 300), (60, 30), 20, 0, 270, (255, 255, 0), -1, cv2.LINE_AA)
cv2.ellipse(img, (120, 300), (100, 50), 20, 0, 360, (0, 255, 0), 2, cv2.LINE_AA)
pts = np.array([[250, 250], [300, 250], [300, 300], [350, 300], [350, 350], [250, 350]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv2.polylines(img, [pts], True, (255, 0, 255), 2)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def drawText1():
img = np.full((500, 800, 3), (255, 255, 255), dtype=np.uint8)
cv2.putText(img, "FONT_HERSHEY_SIMPLEX", (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
cv2.putText(img, "FONT_HERSHEY_PLAIN", (20, 100), cv2.FONT_HERSHEY_PLAIN, 1, (0, 0, 255))
cv2.putText(img, "FONT_HERSHEY_DUPLEX", (20, 150), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255))
cv2.putText(img, "FONT_HERSHEY_COMPLEX", (20, 200), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0))
cv2.putText(img, "FONT_HERSHEY_TRIPLEX", (20, 250), cv2.FONT_HERSHEY_TRIPLEX, 1, (255, 0, 0))
cv2.putText(img, "FONT_HERSHEY_COMPLEX_SMALL", (20, 300), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 0, 0))
cv2.putText(img, "FONT_HERSHEY_SCRIPT_SIMPLEX", (20, 350), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 1, (255, 0, 255))
cv2.putText(img, "FONT_HERSHEY_SCRIPT_COMPLEX", (20, 400), cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 1, (255, 0, 255))
cv2.putText(img, "FONT_HERSHEY_COMPLEX | FONT_ITALIC", (20, 450), cv2.FONT_HERSHEY_COMPLEX | cv2.FONT_ITALIC, 1, (255, 0, 0))
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def drawText2():
img = np.full((200, 640, 3), (255, 255, 255), dtype=np.uint8)
text = "Hello, OpenCV"
fontFace = cv2.FONT_HERSHEY_TRIPLEX
fontScale = 2.0
thickness = 1
sizeText = cv2.getTextSize(text, fontFace, fontScale, thickness)[0]
sizeImg = img.shape
org = ((sizeImg[1] - sizeText[0]) // 2, (sizeImg[0] + sizeText[1]) // 2)
cv2.putText(img, text, org, fontFace, fontScale, (255, 0, 0), thickness)
cv2.rectangle(img, org, (org[0] + sizeText[0], org[1] - sizeText[1]), (0, 255, 0), 1)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
drawLines()
drawPolys()
drawText1()
drawText2()
// C++ code
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
void drawLines();
void drawPolys();
void drawText1();
void drawText2();
int main(void)
{
drawLines();
drawPolys();
drawText1();
drawText2();
return 0;
}
void drawLines()
{
Mat img(400, 400, CV_8UC3, Scalar(255, 255, 255));
line(img, Point(50, 50), Point(200, 50), Scalar(0, 0, 255));
line(img, Point(50, 100), Point(200, 100), Scalar(255, 0, 255), 3);
line(img, Point(50, 150), Point(200, 150), Scalar(255, 0, 0), 10);
line(img, Point(250, 50), Point(350, 100), Scalar(0, 0, 255), 1, LINE_4);
line(img, Point(250, 70), Point(350, 120), Scalar(255, 0, 255), 1, LINE_8);
line(img, Point(250, 90), Point(350, 140), Scalar(255, 0, 0), 1, LINE_AA);
arrowedLine(img, Point(50, 200), Point(150, 200), Scalar(0, 0, 255), 1);
arrowedLine(img, Point(50, 250), Point(350, 250), Scalar(255, 0, 255), 1);
arrowedLine(img, Point(50, 300), Point(350, 300), Scalar(255, 0, 0), 1, LINE_8, 0, 0.05);
drawMarker(img, Point(50, 350), Scalar(0, 0, 255), MARKER_CROSS);
drawMarker(img, Point(100, 350), Scalar(0, 0, 255), MARKER_TILTED_CROSS);
drawMarker(img, Point(150, 350), Scalar(0, 0, 255), MARKER_STAR);
drawMarker(img, Point(200, 350), Scalar(0, 0, 255), MARKER_DIAMOND);
drawMarker(img, Point(250, 350), Scalar(0, 0, 255), MARKER_SQUARE);
drawMarker(img, Point(300, 350), Scalar(0, 0, 255), MARKER_TRIANGLE_UP);
drawMarker(img, Point(350, 350), Scalar(0, 0, 255), MARKER_TRIANGLE_DOWN);
imshow("img", img);
waitKey();
destroyAllWindows();
}
void drawPolys()
{
Mat img(400, 400, CV_8UC3, Scalar(255, 255, 255));
rectangle(img, Rect(50, 50, 100, 50), Scalar(0, 0, 255), 2);
rectangle(img, Rect(50, 150, 100, 50), Scalar(0, 0, 128), -1);
circle(img, Point(300, 120), 30, Scalar(255, 255, 0), -1, LINE_AA);
circle(img, Point(300, 120), 60, Scalar(255, 0, 0), 3, LINE_AA);
ellipse(img, Point(120, 300), Size(60, 30), 20, 0, 270, Scalar(255, 255, 0), FILLED, LINE_AA);
ellipse(img, Point(120, 300), Size(100, 50), 20, 0, 360, Scalar(0, 255, 0), 2, LINE_AA);
vector<Point> pts;
pts.push_back(Point(250, 250)); pts.push_back(Point(300, 250));
pts.push_back(Point(300, 300)); pts.push_back(Point(350, 300));
pts.push_back(Point(350, 350)); pts.push_back(Point(250, 350));
polylines(img, pts, true, Scalar(255, 0, 255), 2);
imshow("img", img);
waitKey();
destroyAllWindows();
}
void drawText1()
{
Mat img(500, 800, CV_8UC3, Scalar(255, 255, 255));
putText(img, "FONT_HERSHEY_SIMPLEX", Point(20, 50), FONT_HERSHEY_SIMPLEX, 1, Scalar(0, 0, 255));
putText(img, "FONT_HERSHEY_PLAIN", Point(20, 100), FONT_HERSHEY_PLAIN, 1, Scalar(0, 0, 255));
putText(img, "FONT_HERSHEY_DUPLEX", Point(20, 150), FONT_HERSHEY_DUPLEX, 1, Scalar(0, 0, 255));
putText(img, "FONT_HERSHEY_COMPLEX", Point(20, 200), FONT_HERSHEY_COMPLEX, 1, Scalar(255, 0, 0));
putText(img, "FONT_HERSHEY_TRIPLEX", Point(20, 250), FONT_HERSHEY_TRIPLEX, 1, Scalar(255, 0, 0));
putText(img, "FONT_HERSHEY_COMPLEX_SMALL", Point(20, 300), FONT_HERSHEY_COMPLEX_SMALL, 1, Scalar(255, 0, 0));
putText(img, "FONT_HERSHEY_SCRIPT_SIMPLEX", Point(20, 350), FONT_HERSHEY_SCRIPT_SIMPLEX, 1, Scalar(255, 0, 255));
putText(img, "FONT_HERSHEY_SCRIPT_COMPLEX", Point(20, 400), FONT_HERSHEY_SCRIPT_COMPLEX, 1, Scalar(255, 0, 255));
putText(img, "FONT_HERSHEY_COMPLEX | FONT_ITALIC", Point(20, 450), FONT_HERSHEY_COMPLEX | FONT_ITALIC, 1, Scalar(255, 0, 0));
imshow("img", img);
waitKey();
destroyAllWindows();
}
void drawText2()
{
Mat img(200, 640, CV_8UC3, Scalar(255, 255, 255));
const String text = "Hello, OpenCV";
int fontFace = FONT_HERSHEY_TRIPLEX;
double fontScale = 2.0;
int thickness = 1;
Size sizeText = getTextSize(text, fontFace, fontScale, thickness, 0);
Size sizeImg = img.size();
Point org((sizeImg.width - sizeText.width) / 2, (sizeImg.height + sizeText.height) / 2);
putText(img, text, org, fontFace, fontScale, Scalar(255, 0, 0), thickness);
rectangle(img, org, org + Point(sizeText.width, -sizeText.height), Scalar(0, 255, 0), 1);
imshow("img", img);
waitKey();
destroyAllWindows();
}
C++ Code 출처 : OpenCV 4로 배우는 컴퓨터 비전과 머신 러닝 - 황선규 저




'OpenCV' 카테고리의 다른 글
ch04 mouse (0) | 2024.05.19 |
---|---|
ch04 keyboard (0) | 2024.05.19 |
ch03 ScalarOp (0) | 2024.05.19 |
ch03 MatOp (0) | 2024.05.19 |
ch03 InputArrayOp (0) | 2024.05.19 |