openCV+numpy基础

2019-04-15 14:51发布

画图

import cv2 import numpy as np def drew(): pic = np.zeros([512, 512, 3]) #定义一个像素长宽分别为512/512,通道为3层的画布 # 直线(参数分别为:画布名称,起点坐标,终点坐标,颜 {MOD},线宽) cv2.line(pic, (256, 512), (256, 0), (255, 255, 0), 4) # 矩形(参数分别为:画布名称,左上角坐标,右下角坐标,颜 {MOD},线宽) cv2.rectangle(pic, (128, 384), (384, 128), (0, 255, 255), 4) # 圆(参数分别为:画布名称,圆心坐标,半径,颜 {MOD},线宽) cv2.circle(pic, (256, 256), 50, (250, 250, 250), 4) # 椭圆(参数分别为:画布名称,圆心坐标,(长半径,短半径),逆时针旋转角度,逆时针开始画图的角度, 逆时针结束画图角度,颜 {MOD},线宽) cv2.ellipse(pic, (256, 256), (128, 64), 90, 0, 360, (255, 0, 255), 4) #多边形 ply = np.array([[50, 190], [380, 420], [255, 50], [120, 420], [450, 190]]) # 注:像素数组 cv2.polylines(pic, [ply], True, (200, 100, 0), 4) # 多边形(像素数组,是否封口) #文字 cv2.putText(pic, 'this is change', (128, 450), cv2.FONT_HERSHEY_SIMPLEX, 1.6, (100, 100, 100), 2) # 文字(text,起始坐标,字体,文字大小,颜 {MOD},线宽) cv2.namedWindow('line') cv2.imshow('line', pic) cv2.waitKey(0) cv2.destroyWindow('line') 运行效果图
遗留问题:文字坐标与显示位置不对

像素编辑

# 像素赋值 def white(img, numbers): for i in range(numbers): for j in range(numbers): for k in range(3): img[i][j][k] = 255 return img # 椒盐函数 def salt(img, numbers): for x in range(numbers): i = np.random.randint(img.shape[0]) j = np.random.randint(img.shape[1]) for k in range(3): img[i][j][k] = 255 return img # 通道分离 def conduit(img): b, g, r = cv2.split(img) pic = np.zeros(np.shape(img), np.uint8) # 像素清零 pic[:, :, 0] = b cv2.namedWindow('Blue') cv2.imshow('Blue', pic) cv2.waitKey(0) pic[:, :, 1] = g cv2.namedWindow('Green') cv2.imshow('Green', pic) cv2.waitKey(0) pic[:, :, 2] = r cv2.namedWindow('Red') cv2.imshow('Red', pic) cv2.waitKey(0) cv2.destroyAllWindows() # 通道合并 def merge(b, g, r): m = cv2.merge([b, g, r]) # cv2自带通道合并方法 dstack = np.dstack([b, g, r]) # np的dstack