opencv3基本操作

1.numpy矩阵表示一个图片

import cv2
import numpy
img = numpy.zeros((3,3),dtype = numpy.uint8)
#每个像素由8个位来表示,像素范围0-255
print(img)

opencv3基本操作_第1张图片
2.将矩阵表示为BGR图

img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
#cv2.cvtColor将图像转变为BGR模式
#其他色彩空间HSV表示方式类似,只是通道数和取值范围不同
print(img)
print(img.shape)#显示通道数和每个通道下的图片大小

(3, 3, 3)

3.cv3改变一张图片格式,存储改变后图片

import cv2
image = cv2.imread('E:\\elephant.jpeg')
cv2.imwrite('E:\\elephant.jpg',image)
#原图片格式改变成.jpg文件之后重新储存了

4.以灰度图查看图片,保存灰度图

import cv2
grayimage = cv2.imread('E:\\elephant.jpg',cv2.IMREAD_GRAYSCALE)
#将图片转变为灰度图片查看
#图片名不能是中文,否则图片读入不了
cv2.imwrite('E:\\elephant_gray.jpg',grayimage)

image.shape
Out[12]: (600, 899, 3)
三个坐标分别表示(y坐标,x坐标,通道数)
第(0,0)处为蓝色,那么
image[0,0] = [255,0,0] (BGR表示就是蓝色)
image[0,0,1] =255

5.将序列变成图片

import cv2
import numpy
import os

#make an array of 120000 random bytes
randomByteArray = bytearray(os.urandom(120000))
#随机产生120000个数,bytearray产生8位,正好在0-255之间,二进制表示
flatNumpyArray = numpy.array(randomByteArray)#变成array

#Convert the array to make 300*400 grayscale image
grayImage = flatNumpyArray.reshape(300,400)
cv2.imwrite('RandomGray.png',grayImage)

#Convert the array to make 400*100*3 color image
bgrImage = flatNumpyArray.reshape(100,400,3)
cv2.imwrite('RandomColor.png',bgrImage)

产生两张图片:
在这里插入图片描述
opencv3基本操作_第2张图片
也可以用:

numpy.random.randint(0,256,120000).reshape(300,400)

产生0-255之间的随机整数120000个,reshape成(300,400)的形状

6.使用numpy.array访问数据图像

###numpy.array访问图像
import cv2
import numpy as np
img = cv2.imread('E:\\elephant.jpg')
img[0,0] = [255,255,255]#在坐标(0,0)位置上将像素变白
img[:,:,1] = 0#将R通道置为0,高效操作像素点

cv2.imshow('result.jpg',img)
cv2.waitKey()
cv2.destroyAllWindows()


该图片完全没有绿色了
将一部分图片拷贝到另一部分位置上

import cv2
import numpy as np
img = cv2.imread('E:\\elephant.jpg')
my_roi = img[200:400,150:350]
cv2.imshow('100*100 size',my_roi)
img[0:200,0:200] = my_roi
cv2.imshow('result',img)
cv2.waitKey()
cv2.destroyAllWindows()


一块图像信息的属性获取(如my_roi):
opencv3基本操作_第3张图片

7.视频文件的读写

###7.视频读写
import cv2

videoCapture = cv2.VideoCapture('E:\\MyInput.mp4')
fps = videoCapture.get(cv2.CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('E:\\MyInput.mp4',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)

success,frame = videoCapture.read()
while success:#循环直到没有帧了
    print(success)
    print(frame.shape)
    videoWriter.write(frame)
    success,frame = videoCapture.read('E:\\MyInput.mp4')

8.捕获摄像头

import cv2

cameraCapture = cv2.VideoCapture(0)
fps = 30
size = (int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter = cv2.VideoWriter('E:\\MyInput.mp4',cv2.VideoWriter_fourcc('I','4','2','0'),fps,size)
success,frame = cameraCapture.read()
numFramesRemaining = 10*fps - 1
while success and numFramesRemaining > 0:
    videoWriter.write(frame)
    success,frame = cameraCapture.read()
    numFramesRemaining -= 1
cameraCapture.release()

9.在窗口中显示图像

###窗口中显示图像
import cv2
import numpy as np
img = cv2.imread('E:\\elephant.jpg')
cv2.imshow('elephant',img)
cv2.waitKey()
cv2.destroyAllWindows()


10.在窗口中显示摄像头帧

###窗口显示摄像头帧
import cv2
clicked = False
def onMouse(event, x, y, flags, param):
    global clicked
    if event == cv2.EVENT_LBUTTONUP:
        clicked = True

cameraCapture = cv2.VideoCapture(0)#获取笔记本电脑自带摄像头,参数为0代表本身的摄像头
cv2.namedWindow('MyWindow')#给显示窗口命名
cv2.setMouseCallback('MyWindow', onMouse)#鼠标回调函数,鼠标点击左键,就会使变量clicked变成True

print('showing camera feed. Click window or press any key to stop.')
success, frame = cameraCapture.read()#读取摄像头
#success为是否读取到帧,为一个bool类型值
#frame是一张rgb图片,frame.shape = (480,640,3)
while success and cv2.waitKey(1) == -1 and not clicked:#如果键盘不按键,鼠标不点左键,就一直刷新读取到的帧图像
    cv2.imshow('MyWindow', frame)
    success, frame = cameraCapture.read()
    
cv2.destroyWindow('MyWindow')#销毁窗口
cameraCapture.release()#清理内存

你可能感兴趣的:(计算机视觉)