OpenCV 04 (channel separation and merging | drawing graphics)

1. Separation and merging of channels

- split(mat) splits the channels of the image
- merge((ch1,ch2, ch3)) merges multiple channels

import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)

b,g,r = cv2.split(img)

b[10:100, 10:100] = 255
g[10:100, 10:100] = 255

img2 = cv2.merge((b, g, r))

cv2.imshow('img', img)
cv2.imshow('b', b)
cv2.imshow('g', g)
cv2.imshow('img2', img2)

cv2.waitKey(0)
cv2.destroyAllWindows()

2. Draw graphics


Using the drawing API provided by OpenCV, you can easily draw various graphics on the image, such as straight lines, rectangles, circles, ellipses and other graphics.

- line(img, pt1, pt2, color, thickness, lineType, shift) draws a straight line
  - img: which image to draw the line on
  - pt1, pt2: start point, end point. Specify the start and end position of the line
  - color: color
  - thickness: line width
  - lineType: line type. Line type is -1, 4, 8, 16, the default is 8
  - shift: coordinate scaling ratio.

- rectangle() parameters are the same as above. Draw a rectangle
- circle(img, center, radius, color, thickness, lineType, shift) Parameters in square brackets indicate optional parameters. Draw a circle

- ellipse(img, center point, half of length and width, angle, starting angle, ending angle,...)

- polylines(img, pts, isClosed, color, thickness, lineType, shift) draw polygons


- fillPoly fill polygon


- putText(img, text, org, fontFace, fontScale, color, thickness, lineType, shift) Draw text
  - text The text to be drawn
  - org The coordinates of the lower left corner of the text in the picture
  - fontFace The font type is the font
  - fontScale The font size

import cv2
import numpy as np

img = np.zeros((480, 640, 3), np.uint8)
# cv2.line(img, (10, 20), (300, 400), (0, 0, 255), 5, 4)
# cv2.line(img, (80, 100), (380, 480), (0, 0, 255), 5, 16)

# 画矩形
# cv2.rectangle(img, (10,10), (100, 100), (0, 0, 255), -1)

# 画圆
# cv2.circle(img, (320, 240), 100, (0, 0, 255))
# cv2.circle(img, (320, 240), 5, (0, 0, 255), -1)
# 画椭圆
# cv2.ellipse(img, (320, 240), (100, 50), 15, 0, 360, (0, 0, 255), -1)

#画多边形
# pts = np.array([(300, 10), (150, 100), (450, 100)], np.int32)
# cv2.polylines(img, [pts], True, (0, 0, 255))

#填充多边形
# cv2.fillPoly(img, [pts], (255, 255, 0))

cv2.putText(img, "Hello OpenCV!", (10, 400), cv2.FONT_HERSHEY_TRIPLEX, 3, (255,0,0))
cv2.imshow('draw', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

- Drawing Chinese is not supported by opencv itself because there is no Chinese font. We can use pillow to draw Chinese

 # 安装pillow
  import cv2
  import numpy as np
  from PIL import ImageFont, ImageDraw, Image
  
  img = np.full((200, 200, 3), fill_value=255, dtype=np.uint8)
  # 导入字体文件. 
  font_path = 'msyhbd.ttc'
  font = ImageFont.truetype(font_path, 15)
  img_pil = Image.fromarray(img)
  draw = ImageDraw.Draw(img_pil)
  draw.text((10, 150), '绘制中文', font=font, fill=(0, 255, 0, 0))
  img = np.array(img_pil)
  
  # 中文会显示问号
  cv2.putText(img, '中文', (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 1)
  
  cv2.imshow('img', img)
  cv2.waitKey(0)
  cv2.destroyAllWindows()

Guess you like

Origin blog.csdn.net/peng_258/article/details/132766928