python opencv
1.画图
import cv2
import numpy as np
def drew():
pic = np.zeros([512, 512, 3]) #定义一个像素长宽分别为512/512,通道为3层的画布
# 直线(参数分别为:画布名称,起点坐标,终点坐标,颜色,线宽)
cv2.line(pic, (256, 512), (256, 0), (255, 255, 0), 4)
# 矩形(参数分别为:画布名称,左上角坐标,右下角坐标,颜色,线宽)
cv2.rectangle(pic, (128, 384), (384, 128), (0, 255, 255), 4)
# 圆(参数分别为:画布名称,圆心坐标,半径,颜色,线宽)
cv2.circle(pic, (256, 256), 50, (250, 250, 250), 4)
# 椭圆(参数分别为:画布名称,圆心坐标,(长半径,短半径),逆时针旋转角度,逆时针开始画图的角度, 逆时针结束画图角度,颜色,线宽)
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,起始坐标,字体,文字大小,颜色,线宽)
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
import os
import cv2
root = "/media/data_2/everyday/0807/20190806/"
save_path = os.path.dirname(os.path.dirname(root)) + '/root_new/'
if not os.path.exists(save_path):
os.makedirs(save_path)
new_size = 1280
list_file = os.listdir(root)
for cnt,img_name in enumerate(list_file):
print("cnt=%d,img=%s"%(cnt,img_name))
img_path = root + img_name
img = cv2.imread(img_path)
img_new = cv2.resize(img,(new_size,new_size))
cv2.imwrite(save_path + img_name,img_new)
# 通道分离
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, import os
import cv2
root = "/media/data_2/everyday/0807/20190806/"
save_path = os.path.dirname(os.path.dirname(root)) + '/root_new/'
if not os.path.exists(save_path):
os.makedirs(save_path)
new_size = 1280
list_file = os.listdir(root)
for cnt,img_name in enumerate(list_file):
print("cnt=%d,img=%s"%(cnt,img_name))
img_path = root + img_name
img = cv2.imread(img_path)
img_new = cv2.resize(img,(new_size,new_size))
cv2.imwrite(save_path + img_name,img_new)r]) # np的dstack
3.roi区域
m_roi = img[0:img.shape[0],x1:x1+w1]
4. 批量resize
import os
import cv2
root = "/media/data_2/everyday/0807/20190806/"
save_path = os.path.dirname(os.path.dirname(root)) + '/root_new/'
if not os.path.exists(save_path):
os.makedirs(save_path)
new_size = 1280
list_file = os.listdir(root)
for cnt,img_name in enumerate(list_file):
print("cnt=%d,img=%s"%(cnt,img_name))
img_path = root + img_name
img = cv2.imread(img_path)
img_new = cv2.resize(img,(new_size,new_size))
cv2.imwrite(save_path + img_name,img_new)
5. cv2.copyMakeBorder
import cv2
img = cv2.imread("/data_2/part-00729-3292.jpg")
######cv2.copyMakeBorder(img, top, bottem, left, right, cv2.BORDER_REPLICATE)
# img2 = cv2.copyMakeBorder(img,300,400,20,100,cv2.BORDER_REPLICATE)
img2 = cv2.copyMakeBorder(img,300,400,500,400,cv2.BORDER_CONSTANT,value=(127,127,127))
cv2.namedWindow("img",0)
cv2.namedWindow("img2",0)
cv2.imshow("img",img)
cv2.imshow("img2",img2)
cv2.waitKey()
好记性不如烂键盘---点滴、积累、进步!