opencv笔记:
概述:
俄罗斯团队开发的,==计算机视觉处理开源软件库==
优点:
效率高
跨平台
丰富的API
安装opencv
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
图片背后的原理
==默认颜色是RGB格式==
像素是组成图像的最小单位,可以理解成一个个“小色块”。每个像素是8位图像,0-255。
图片-----》RGB格式===三通道
创建虚拟环境
创建虚拟环境
conda create -n 环境名 python=版本
2、创建项目绑定环境
open_study
3、绑定项目和环境
绝对路径和相对路径
常用数据源
1. 阿里云镜像:https://mirrors.aliyun.com/pypi/simple/ 2. 清华大学镜像:https://pypi.tuna.tsinghua.edu.cn/simple 3. 豆瓣镜像:https://pypi.doubanio.com/simple/ 4. 中科大镜像:https://pypi.mirrors.ustc.edu.cn/simple/ 5. 华为云镜像:https://mirrors.huaweicloud.com/repository/pypi/simple/ 6. 腾讯云镜像:https://mirrors.cloud.tencent.com/pypi/simple/ 7. 网易镜像:http://mirrors.163.com/pypi/simple/ 8. 中国科学技术大学镜像:https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple/ 9. 北京理工大学镜像:https://pypi.bfsu.edu.cn/simple/ 10. 西南大学镜像:https://pypi.swu.edu.cn/simple/ 11. 南京大学镜像:https://mirrors.nju.edu.cn/pypi/simple/ 12. 山东理工大学镜像:https://pypi.sdutlinux.org/simple/ 13. 中国科学院深圳先进技术研究院镜像:https://pypi.sztu.edu.cn/simple/ 14. 东北大学镜像:https://pypi.neu.edu.cn/simple/ 15. 西北大学镜像:https://mirrors.nwafu.edu.cn/pypi/web/simple/ 16. 北京师范大学镜像:https://pypi.bnu.edu.cn/simple/ 17. 华中科技大学镜像:https://pypi.hustunique.com/simple/ 18. 武汉理工大学镜像:https://pypi.whut.edu.cn/simple/ 19. 华南理工大学镜像:https://pypi.sysu.edu.cn/simple/ 20. 上海交通大学镜像:https://pypi.sjtug.sjtu.edu.cn/simple/
查看图片形状
original_img.shape
(735, 690, 3)
高,宽 ,通道数
单通道和三通道
img_gray = cv2.cvtColor(original_img,cv2.COLOR_BGR2GRAY)
关键知识点
视频=======》图片====》三维数组(矩阵)=====》像素点组成======》0到255的数字====》
常用API
1、创建窗口
namedWindow(str:winname,是否改变大小)
imread(path:str,0/1)
imshow(name:str,mat/ndarray/numpy)
imwrite('文件名',mat/ndarray)
np.zeros()
np.random.randint()
参考代码
import cv2 import numpy as np ''' 创建窗体 1、不允许修改窗体大小 0、可以运行修改 ''' def one(): cv2.namedWindow('name',1) img_source = cv2.imread('22.jpg') # 增 删 改 查 cv2.imwrite('zj.jpg',img_source) # 创建全黑的图像 def black_img(): print("black_img") result = np.zeros((500,250,3),dtype=np.uint8) print(type(result)) ''' 显示,第二个参数 ''' cv2.imshow('black',result) print(100 * '*') cv2.waitKey(0) cv2.destroyAllWindows() # 随机填充 def random_full(): print("random_full") result = np.random.randint(0,255,(300,300,3),dtype=np.uint8) print(type(result)) print(result.shape) ''' 显示,第二个参数 ''' cv2.imshow('random_full', result) print(100 * '*') cv2.waitKey(0) cv2.destroyAllWindows() # 切片 def cut_img(): # 原图 img_source = cv2.imread("22.jpg") print(img_source.shape) img_cut = img_source[0:100,100:300] # 原图 cv2.imshow('img_source', img_source) # 切片 cv2.imshow('img_cut', img_cut) print(100 * '*') cv2.waitKey(0) cv2.destroyAllWindows() if __name__ == '__main__': cut_img()
cv2.waitkey(毫秒数) cv2.destroyAllWindows
绘制形状
参考代码:
''' 绘制形状 ''' import cv2 def draw_line(): # 读取图片 img_source = cv2.imread('zj.jpg') # 注意颜色 bgr (红色) 我们认知的范围 rgb,但是程序opencv的颜色是bgr格式 cv2.line(img_source,(300,300),(100,0),(0,0,255),2) cv2.imshow('img_source',img_source) # 等待 cv2.waitKey(0) # 销毁 cv2.destroyAllWindows() # 绘制圆形 def draw_circle(): img_source = cv2.imread('zj.jpg') #获取高和宽 height,width = img_source.shape[0],img_source.shape[1] #找到中心点 center_x,center_y = int(height/2),int(width/2) # -1实心圆 cv2.circle(img_source,(center_x,center_y),100,(0,255,0),-1) cv2.imshow('circle', img_source) # 等待 cv2.waitKey(0) # 销毁 cv2.destroyAllWindows() # 矩形右下角 def draw_rect(): # 获取高和宽 img_source = cv2.imread('zj.jpg') height, width = img_source.shape[0], img_source.shape[1] # 两个坐标点 end_point=(width,height) start_point=[width-100,height-100] #绘制矩形 cv2.rectangle(img_source,start_point,end_point,(255,0,0),2) cv2.imshow('circle', img_source) # 等待 cv2.waitKey(0) # 销毁 cv2.destroyAllWindows() # 写字 def write_font(): img_source = cv2.imread('zj.jpg') pt = (300,300) # fontface: 字体的样式 楷体,宋体 cv2.putText(img_source,'day day up',pt,cv2.FONT_HERSHEY_COMPLEX,0.5,(0,0,200),2) cv2.imshow('circle', img_source) # 等待 cv2.waitKey(0) # 销毁 cv2.destroyAllWindows() if __name__ == '__main__': # 调用 # draw_line() # draw_circle() write_font()
==注意坐标==
图像默认的是 高和宽
但是绘制图的时候,注意 高是y,宽是x
练习题
灰度实验
将三通道转换为单通道。
补充
import cv2 img = cv2.imread('1.jpg') # 查看第(100,100)像素的B通道值 img[100,100,2] img.item((100,100,2)) # 修改 img[100,100,2]=(233,123,23) img.itemset((100,100,2),91) #像素总数 print(img.size) #获取某一个通道数组 b,g,r=cv2.split(img) b1 =img[:,:,0] #设置边框 img1 = cv2.copyMakeBorder(img,2,2,2,2,cv2.BORDER_CONSTANT,value=(255,255,255)) # mat,上下左右,样式,颜色(缺一不可) cv2.nameWindows('img',cv2.WINDOW_NORMAL) cv2.imshow('img',img1) cv2.waitKey(0) cv2.destroyAllWindows()