Open DanceSmile opened 6 years ago
import cv2 as cv
# opencv读取图片资源 numpy 类型
src = cv.imread("./tim.jpeg")
# 设置窗口
cv.namedWindow('img window', cv.WINDOW_AUTOSIZE)
# 显示图像
cv.imshow("img window", src)
# 任意键退出
cv.waitKey(0)
# 销毁窗口
cv.destroyAllWindows()
import cv2 as cv
# 获取视频
def get_video():
# 获取第几号摄像头
video = cv.VideoCapture(0)
while(True):
# 读取摄像头帧数据
ret, frame = video.read()
# 翻转帧数据
frame = cv.flip(frame, 1)
# 显示视频
cv.imshow("video view", frame)
c = cv.waitKey(50)
if c == 27 :
break
# 保存图像
def save_img(img):
# 图像灰度处理
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 写入磁盘
cv.imwrite('./copy.jpg', gray)
# 读取图像信息
def get_img_info(img):
# 图像资源类型
print(type(img))
# 图像长、宽、通道数量
print(img.shape)
# 图像大小
print(img.size)
# 每个通道占字节
print(img.dtype)
src = cv.imread("./tim.jpeg")
save_img(src)
get_img_info(src)
get_video();
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
def show_img(src):
shape = src.shape
print(shape)
# 图像高度
height = shape[0]
# 图像宽度
width = shape[1]
# 色彩通道
channels = shape[2]
# 反向rgb色彩
for row in range(height):
for col in range(width):
for c in range(channels):
src[row, col, c] = 255 - src[row, col, c]
cv.imshow('test', src)
time1 = cv.getTickCount()
src = cv.imread('./src.jpg')
time2 = cv.getTickCount()
# 获取修改图像信息消耗的时间
print( (time2-time1)/cv.getTickFrequency())
show_img(src)
cv.waitKey(0)
cv.destroyAllWindows()
import numpy as np
import cv2 as cv
# row300 col200 通道3 的三位数组,默认值为0 值类型为int8
d = np.zeros([300, 200, 3], np.uint8)
# 默认值为1 , 修改通道
d[:, :, 0] = np.ones([300,200]) * 255 # blue green red
cv.imshow("test", d)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
src = cv.imread('./src.jpg')
# 像素取反
dst = cv.bitwise_not(src)
cv.imshow("test", dst)
cv.waitKey(0)
cv.destroyAllWindows()
# RGB
# HSV
# HIS
# YCRCB
# YUV
grap = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imshow('grap', grap)
hsv = cv.cvtColor(src, cv.COLOR_RGB2HSV)
cv.imshow('hsv', hsv)
his = cv.cvtColor(src, cv.COLOR_BGR2HLS)
cv.imshow('his', his)
ycrcb = cv.cvtColor(src, cv.COLOR_BGR2YCrCb)
cv.imshow('ycrcb', ycrcb)
yuv = cv.cvtColor(src, cv.COLOR_BGR2YUV)
cv.imshow('yuv', yuv)
import cv2 as cv
import numpy as np
video = cv.VideoCapture(0)
while(True):
ret, frame = video.read()
frame = cv.flip(frame, 1)
lowerb = np.array([37, 43, 46])
upperb = np.array([77, 255, 255])
hsv = cv.inRange(frame, lowerb=lowerb, upperb=upperb)
cv.imshow("video", frame)
cv.imshow("hsv", hsv)
key = cv.waitKey(50)
if(key == 27):
break
cv.destroyAllWindows()
import cv2 as cv
import numpy as np
src = cv.imread("./src.jpg")
# 通道拆分
b, g, r = cv.split(src)
cv.imshow('blue', b)
cv.imshow('green', g)
cv.imshow('red', r)
# 通道合并
merge = cv.merge([b, g, r])
cv.imshow('merge', merge)
cv.waitKey(0)
cv.destroyAllWindows()
import cv2 as cv
# 加
cv.add()
# 减
cv.subtract()
# 乘
cv.multiply()
# 除
cv.divide()
# 均值
cv.mean()
# 方差
cv.meanStdDev()
# 与
cv.bitwise_and()
# 或
cv.bitwise_or()
# 非
cv.bitwise_not()
# 亮度对比度
cv.addWeighted()
import cv2 as cv
face_cascade = cv.CascadeClassifier("/usr/local/lib/python3.6/site-packages/cv2/data/haarcascade_frontalface_default.xml")
video = cv.VideoCapture(0)
while(True):
ret, frame = video.read()
src = cv.flip(frame, 1)
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 1)
for (x, y, w, h) in faces:
cv.rectangle(src, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv.imshow('face', src)
key = cv.waitKey(50)
if key == 27:
break
cv.destroyAllWindows()
opencv 图像