OpenCV:Drawing
APIs
- cv2.arrowedLine
- cv2.circle
- cv2.clipLine
- cv2.drawContours
- cv2.drawMarker
- cv2.ellipse2Poly
- cv2.ellipse
- cv2.fillConvexPoly
- cv2.fillPoly
- cv2.getFontScaleFromHeight
- cv2.getTextSize
- cv2.line
- cv2.polylines
- cv2.putText
- cv2.rectangle
Tips
- cv2.addWeighted - 이미지 합성 및 블랜딩 시 사용한다.
Examples
- OpenCV:Example:Blending - 이미지 합성
LineType
lineType
인자에 대한 내용. 거두절미하고 그림으로 확인:
Opencv_drawing_linetype.png
- <span style="color: yellow;">yellow = cv2.LINE_4
- <span style="color: red;">red = cv2.LINE_AA
- <span style="color: green;">green = cv2.LINE_8
Arrows
#include <iostream>
#include <string>
#include <opencv2/opencv.hpp>
#include <highgui.h>
using namespace std;
using namespace cv;
int main(int argc, char* argv[])
{
auto width = 320;
auto height = 320;
auto img = cv::Mat(cv::Size(width, height), CV_8UC3); // create background image
auto center = cv::Point(width / 2, height / 2); // center point
int lineType = 8;
int thickness = 1;
double tipLength = 0.1;
img.setTo(0); // clear image - set to black
for (int angle = 0; angle < 360; angle += 15)
{
auto angleRad = angle*CV_PI / 180.0; // convert angle to radians
auto length = 150;
auto direction = cv::Point(length * cos(angleRad), length * sin(angleRad)); // calculate direction
tipLength = .01 + 0.4 * (angle%180) / 360;
cv::arrowedLine(img, center +direction*0.5, center + direction, CV_RGB(255, angle, 0), thickness, lineType, 0, tipLength); // draw arrow!
++thickness;
if (0 == angle % 45)
thickness = 0;
if (180 <= angle)
lineType = CV_AA;
}
imshow('Arrowed Image', img); // show image
waitKey();
return EXIT_SUCCESS;
}
Python example
import numpy as np
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Draw a diagonal blue line with thickness of 5 px
cv2.line(img,(0,0),(511,511),(255,0,0),5)
# Drawing Rectangle
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
# Drawing Circle
cv2.circle(img,(447,63), 63, (0,0,255), -1)
# Drawing Ellipse
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
# Drawing Polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img,[pts],True,(0,255,255))
# Fill Polygon
pts = np.array([[320, 245], [410, 315], [380, 415], [265, 415], [240, 315]], np.int32)
pts = pts.reshape((-1, 1, 2))
cv.fillPoly(img, [pts], (0, 255, 255))
# Adding Text to Images:
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
Text Box example
font_scale = 1
font = cv2.FONT_HERSHEY_SIMPLEX
thickness = 2
for obj in self.objects:
# obj: DetectorObject
x, y, w, h = obj.bbox
background = invert_color(obj.color)
cv2.rectangle(self.preview, (x, y), (x + w, y + h), background, thickness, cv2.LINE_AA)
text = '{} ({:.3f})'.format(obj.name, obj.score)
text_size = cv2.getTextSize(text, font, font_scale, thickness)
tw, th = text_size[0]
base_line = text_size[1]
cv2.rectangle(self.preview, (x, y), (x+tw, y-th-base_line-(thickness*2)), background, cv2.FILLED)
cv2.putText(self.preview, text, (x, y-base_line), font, font_scale, obj.color, thickness, cv2.LINE_AA)
if obj.points:
cv2.drawContours(self.preview, obj.points, -1, background, 2, cv2.LINE_AA)
Blending images
from __future__ import print_function
import cv2 as cv
alpha = 0.5
try:
raw_input # Python 2
except NameError:
raw_input = input # Python 3
print(''' Simple Linear Blender
-----------------------
* Enter alpha [0.0-1.0]: ''')
input_alpha = float(raw_input().strip())
if 0 <= alpha <= 1:
alpha = input_alpha
# [load]
src1 = cv.imread(cv.samples.findFile('LinuxLogo.jpg'))
src2 = cv.imread(cv.samples.findFile('WindowsLogo.jpg'))
# [load]
if src1 is None:
print("Error loading src1")
exit(-1)
elif src2 is None:
print("Error loading src2")
exit(-1)
# [blend_images]
beta = (1.0 - alpha)
dst = cv.addWeighted(src1, alpha, src2, beta, 0.0)
# [blend_images]
# [display]
cv.imshow('dst', dst)
cv.waitKey(0)
# [display]
cv.destroyAllWindows()