参考教程:
在终端中输入口令如下:
pip3 install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install opencv-contrib-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip3 install pytesseract -i https://pypi.tuna.tsinghua.edu.cn/simple
import cv2 as cv
#读一张图片
src = cv.imread('cat.jpg')
#创建一个GUI显示图片
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
#将图片放入窗口
cv.imshow("input image",src)
#指定窗口等待时间,0为无限等待
cv.waitKey(0)
#关闭窗口
cv.destroyAllWindows()
对于计算机来说,图像就是结构化存储的数据信息。
计算机中图像属性主要包括:
import cv2 as cv
import numpy as np
def get_image_info(image):
"""
输出图像属性
- - - -
image - 图像名
"""
print(type(image)) #类型
print(image.shape) #(高,宽,通道数)
print(image.size) #像素数据=高*宽*通道数
print(image.dtype) #每个像素点的每个通道所占的位数
#获取像素数据并打印
pixel_data = np.array(image)
print(pixel_data)
if __name__ == "__main__":
#读一张图片
src = cv.imread('cat.jpg')
#创建一个GUI显示图片
cv.namedWindow("input image",cv.WINDOW_AUTOSIZE)
#将图片放入窗口
cv.imshow("input image",src)
#输出图像属性
get_image_info(src)
#指定窗口等待时间,0为无限等待
cv.waitKey(0)
#关闭窗口
cv.destroyAllWindows()
结果:
(500, 500, 3)
750000
uint8
[[[149 179 228]
[148 178 227]
[148 178 227]
...
[ 0 36 86]
[ 4 41 91]
[ 6 43 93]]
[[148 178 227]
[148 178 227]
[147 177 226]
...
[ 6 43 95]
[ 11 48 100]
[ 12 49 101]]
[[148 178 227]
[147 177 226]
[149 176 226]
...
[ 9 45 99]
[ 13 49 103]
[ 15 51 105]]
...
[[ 61 120 170]
[ 62 121 171]
[ 62 121 170]
...
[ 35 69 92]
[ 36 70 93]
[ 37 71 94]]
[[ 61 120 170]
[ 62 121 171]
[ 62 121 170]
...
[ 34 68 91]
[ 36 70 93]
[ 40 74 97]]
[[ 61 120 170]
[ 62 121 171]
[ 62 121 170]
...
[ 34 68 91]
[ 37 71 94]
[ 39 73 96]]]
import cv2 as cv
if __name__ == "__main__":
src = cv.imread('cat.jpg')
#为了区别,获取灰度图像
gray = cv.cvtColor(src,cv.COLOR_BGR2GRAY)
#将灰度图像写成png形式储存
cv.imwrite("cat.png",gray)
调用电脑摄像头读视频
import cv2 as cv
def video_demo():
"""
调用电脑摄像头读视频
"""
#打开电脑摄像头
capture = cv.VideoCapture(0)
while(True):
ret,frame = capture.read()
#镜像调换,1为左右,-1为上下
frame = cv.flip(frame,1)
#显示视频中的每一帧
cv.imshow("video",frame)
c = cv.waitKey(50)
#当输入“Esc”(ASCII码27)时,退出循环
if c == 27:
break
if __name__ == "__main__":
video_demo()
cv.destroyAllWindows()
通过修改像素点属性值进行反色处理
import cv2 as cv
import numpy as np
def access_pixels(image):
"""
通过修改像素点属性值进行反色处理
- - - -
image - 原图像名
"""
height = image.shape[0]
wide = image.shape[1]
channels = image.shape[2]
# 3通道,分别对应blue,green,red
print("height:",height,"\nwide:",wide,"\nchannels:",channels)
#对每一行
for row in range(height):
#对每一列
for rol in range(wide):
#对每一通道
for c in range(channels):
#修改像素点数值
pv=image[row,rol,c]
image[row,rol,c]=255-pv
#将反色图像写成png形式储存
cv.imwrite("cat.png",image)
if __name__ == "__main__":
src = cv.imread('cat.jpg')
# 获取当前cpu时钟总圈数
t1 = cv.getTickCount()
access_pixels(src)
t2 = cv.getTickCount()
# 打印用时,单位ms(cv.getTickFrequency()获取cpu时钟每秒走多少圈)
print("time:%fms"%((t2-t1)/cv.getTickFrequency()*1000))
结果:
height: 500
wide: 500
channels: 3
time:2857.571823ms
src = cv.imread('cat.jpg')
t1 = cv.getTickCount()
#反色处理
dst = cv.bitwise_not(src)
cv.imwrite("cat.png",dst)
t2 = cv.getTickCount()
print("time:%fms"%((t2-t1)/cv.getTickFrequency()*1000))
结果:
time:23.518412ms
import cv2 as cv
import numpy as np
def create_image():
"""
直接创建图像
"""
# 3通道图像
img = np.zeros([400,400,3],np.uint8)
# 将蓝色通道(第1通道)与红色通道(第3通道)全部赋为255,应为紫(粉?)图
img[:,:,0]=np.ones([400,400])*255
img[:,:,2]=np.ones([400,400])*255
cv.imwrite("newimg_3c.png",img)
# 单通道图像
img = np.ones([400,400,1],np.uint8)
# 将唯一通道(第1通道)全部赋为127,应为灰图
img = img * 127
cv.imwrite("newimg_1c.png",img)
if __name__ == "__main__":
create_image()