环境安装 opencv-Python opencv-contrib-Python pytesseract Opencv模块架构 代码练习1(创建图片窗口,使用摄像头): import cv2 as cv #科学计数 import numpy as np image_path = "datasources/images/1 (1).jpg" def loo
环境安装
Opencv模块架构
代码练习1(创建图片窗口,使用摄像头):
import cv2 as cv#科学计数
import numpy as np
image_path = "datasources/images/1 (1).jpg"
def look_dog_image():
#将传入的图片转换成为矩阵,1为原图返回,0为返回灰度图
src = cv.imread(image_path, 1)
#创建窗体, WINDOW_AUTOSIZE根据图片自适应确定窗口大小,WINDOW_NORMAL用户可以自行更改,WINDOW_OPENGL窗体支持OpenGL
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
#将传入的像素矩阵显示出来
cv.imshow("input image", src)
#使图像灰度化,并且使用cv.imwriter写入到指定图像中
gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
cv.imwrite("resule.png", gray)
#图片显示延时,0为不延时
cv.waitKey(0)
# 销毁所有窗口,释放所有内存
cv.destroyAllWindows()
return src
def get_image_info(image_path):
print(type(image_path))
print(image_path.shape)
print(image_path.size)
print(image_path.dtype)
print("-------------")
print(np.array(image_path))
def look_video():
#0表示开启笔记本内置摄像头,数是视频文件路径则打开视频,如cap = cv2.VideoCapture(“../test.avi”)
capture = cv.VideoCapture(0)
while(True):
#按帧读取视频,ret是布尔,frame是每一帧的图像
ret, frame = capture.read()
#0 - -- 垂直方向翻转; 1 - ---- 水平方向翻转; -1:水平、垂直方向同时翻转
frame = cv.flip(frame, 1)
cv.imshow("video", frame)
#表示每过50s刷新一次,返回值:如果delay>0,那么超过指定时间则返回-1;如果delay=0,将没有返回值。
c = cv.waitKey(50)
if c == 27 :
break
if __name__ == "__main__":
get_image_info(look_dog_image())
# look_video()
代码练习2(创建图像):
import cv2 as cvimport numpy as np
image_path = "datasources/images/1 (1).jpg"
def access_pixels(image_path):
src = cv.imread(image_path, 1)
# cv.namedWindow("dog_life_images", cv.WINDOW_AUTOSIZE)
# cv.imshow("dog images", src)
print(src.shape)
width = src.shape[0]
height = src.shape[1]
channels = src.shape[2]
for w in range(width):
for h in range(height):
for c in range(channels):
pv = src[w, h, c]
src[w, h, c] = 255 - pv
cv.imshow("colos", src)
cv.waitKey(0)
cv.destroyAllWindows()
def create_image():
img = np.ones([400, 400, 3], np.uint8)
img[:, :, 0] = np.ones([400, 400])*255
# img[:, :, 1] = np.ones([400, 400])*255
# img[:, :, 2] =np.ones([400, 400])*255
m = np.ones([5, 8], np.float32)
m.fill(233)
#m*n =x*y
n =m.reshape([10, 4])
print(m)
print("-----")
print(n)
cv.imshow("new image", img)
cv.imwrite("ps.png", img)
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == "__main__":
# t1 = cv.getTickCount()
# access_pixels(image_path)
# t2 = cv.getTickCount()
# print("耗时:"(t2 - t1)/cv.getTickFrequency())
create_image()
- 总结:
重点:numpy库的函数、二值化
难点:ROI与泛洪填充、滤波