OpenCV 04(通道分离与合并 | 绘制图形)

一、通道的分离与合并

  • split(mat)分割图像的通道

  • merge((ch1,ch2, ch3)) 融合多个通道

python 复制代码
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()

二、绘制图形

利用OpenCV提供的绘制图形API可以轻松在图像上绘制各种图形, 比如直线, 矩形, 圆, 椭圆等图形.

  • line(img, pt1, pt2, color, thickness, lineType, shift) 画直线

  • img: 在哪个图像上画线

  • pt1, pt2: 开始点, 结束点. 指定线的开始与结束位置

  • color: 颜色

  • thickness: 线宽

  • lineType: 线型.线型为-1, 4, 8, 16, 默认为8

  • shift: 坐标缩放比例.

  • rectangle() 参数同上 画矩形

  • circle(img, center, radius, color, thickness, lineType, shift) 中括号内参数表示可选参数. 画圆

  • ellipse(img, 中心点, 长宽的一半, 角度, 从哪个角度开始, 从哪个角度结束,...)

  • polylines(img, pts, isClosed, color, thickness, lineType, shift) 画多边形

  • fillPoly 填充多边形

  • putText(img, text, org, fontFace, fontScale, color, thickness, lineType, shift) 绘制文本

  • text 要绘制的文本

  • org 文本在图片中的左下角坐标

  • fontFace 字体类型即字体

  • fontScale 字体大小

python 复制代码
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()
  • 绘制中文 opencv本身不支持, 因为没有中文字体.我们可以借助pillow来实现绘制中文
python 复制代码
 # 安装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()
相关推荐
张较瘦_1 小时前
[论文阅读] 人工智能 + 软件工程 | 需求获取访谈中LLM生成跟进问题研究:来龙去脉与创新突破
论文阅读·人工智能
一 铭2 小时前
AI领域新趋势:从提示(Prompt)工程到上下文(Context)工程
人工智能·语言模型·大模型·llm·prompt
顾道长生'3 小时前
(Arxiv-2025)通过动态 token 剔除实现无需训练的高效视频生成
计算机视觉·音视频·视频生成
麻雀无能为力5 小时前
CAU数据挖掘实验 表分析数据插件
人工智能·数据挖掘·中国农业大学
时序之心5 小时前
时空数据挖掘五大革新方向详解篇!
人工智能·数据挖掘·论文·时间序列
.30-06Springfield6 小时前
人工智能概念之七:集成学习思想(Bagging、Boosting、Stacking)
人工智能·算法·机器学习·集成学习
说私域7 小时前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
永洪科技7 小时前
永洪科技荣获商业智能品牌影响力奖,全力打造”AI+决策”引擎
大数据·人工智能·科技·数据分析·数据可视化·bi
shangyingying_17 小时前
关于小波降噪、小波增强、小波去雾的原理区分
人工智能·深度学习·计算机视觉
书玮嘎8 小时前
【WIP】【VLA&VLM——InternVL系列】
人工智能·深度学习