OpenCV 初探

Posted 漫跑的小兔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV 初探相关的知识,希望对你有一定的参考价值。

OpenCV的全称是Open Source Computer Vision Library,是一个跨平台的计算机视觉库。OpenCV是由英特尔公司发起并参与开发,以BSD许可证授权发行,可以在商业和研究领域中免费使用。OpenCV可用于开发实时的图像处理计算机视觉以及模式识别程序。

OpenCV可用于解决如下领域的问题:

  • 增强现实   

  • 人脸识别

  • 手势识别

  • 人机交互

  • 动作识别

  • 运动跟踪

  • 物体识别

  • 图像分割

  • 机器人

OpenCV-Python 是对应Python语言的库,下面的代码使用Python来演示,让我们一起来简单看看它能干什么。

安装:

我使用的环境是window10,Python3.8,Pycharm。安装在Pycharm中进行:File->Settings->Project:xxxx->Python Interpreter. 点加号,选择opencv-python就行了。


打开图像、视频、摄像头:

import cv2 as cvprint(cv.__version__)# 导入并显示图片img = cv.imread("resouse/wangyin.png")cv.imshow("Output",img)cv.waitKey(0)# 导入并显示视频cap = cv.VideoCapture("resouse/weather.mp4")while True: success, img = cap.read() cv.imshow("video", img) if cv.waitKey(1) & 0xFF == ord('q'): break# 读取并显示摄像头cap2 = cv.VideoCapture(0) # 0代表默认设备,如果有多个可以选填1,2,等cap2.set(3, 80) #宽度cap2.set(4, 60) #高度cap2.set(10,50) #亮度
while True: success, img = cap2.read() cv.imshow("video", img) if cv.waitKey(1) & 0xFF == ord('q'): break

图像的处理:

import cv2 as cvimport numpy as np
img = cv.imread('resouse/jjy.jpg')imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) # 灰度imgBlur = cv.GaussianBlur(img, (1, 111), 0) # 模糊imgCanny = cv.Canny(img, 150, 200) # 边缘检测kernel = np.ones((5,5),np.uint8)imgDialation = cv.dilate(imgCanny, kernel, iterations=3) # 扩张imgEroded = cv.erode(imgDialation, kernel, iterations=2) # 侵蚀
cv.imshow('Gray jujingyi', imgGray)cv.imshow('Blur jujingyi', imgBlur)cv.imshow('Canny jujingyi', imgCanny)cv.imshow('Dialation jujingyi', imgDialation)cv.imshow('Erode jujingyi', imgEroded)cv.waitKey(0)

OpenCV 初探OpenCV 初探

OpenCV 初探OpenCV 初探

伸缩,裁剪:

import cv2 as cv
img = cv.imread('resouse/jjy.jpg')print(img.shape)imgResize = cv.resize(img, (200,300)) # 缩放imgCropped = img[400:600,200:300] # 裁剪
cv.imshow("Output", img)cv.imshow("Resize", imgResize)cv.imshow("Cropped",imgCropped)cv.waitKey(0)

OpenCV 初探


透视转换:

import cv2 as cvimport numpy as np
img = cv.imread("resouse/cards.jpg")imgresize = cv.resize(img,(600,600))
width,height = 250,350pts1 = np.float32([[276,55],[565,116],[187,498],[480,560]])pts2 = np.float32([[0,0],[width,0],[0,height],[width,height]])matrix = cv.getPerspectiveTransform(pts1,pts2) # 透视转换imgOutput = cv.warpPerspective(imgresize,matrix,(width,height))
cv.circle(imgresize,(276,55),10,(143,25,16),4)cv.circle(imgresize,(565,116),10,(42,98,135),4)cv.circle(imgresize,(187,498),10,(23,214,32),4)cv.circle(imgresize,(480,560),10,(143,67,48),4)
cv.imshow("img", imgresize)cv.imshow("imgout", imgOutput)cv.waitKey(0)

OpenCV 初探

画图型,文字:

import cv2 as cvimport numpy as np
img = np.zeros((512, 512, 3),np.uint8)
#img[:,:] = 255,0,0cv.line(img,(0,0),(img.shape[0],img.shape[1]),(0,235,32),8)cv.rectangle(img,(0,0),(250,250),(234,54,216),cv.FILLED)cv.circle(img,(100,100),100,(32,67,235),9)cv.putText(img,"Open CV",(250,250),cv.FONT_HERSHEY_COMPLEX,1,(42,87,35),4)
cv.imshow("img", img)cv.waitKey(0)

OpenCV 初探

鼠标:双击画点

import numpy as npimport cv2 as cv
evendict = {getattr(cv,name):name for name in dir(cv) if 'EVENT' in name}print(evendict)
# mouse callback functiondef draw_circle(event,x,y,flags,param): print(evendict[event],"x:",x,"y:",y,"flags:",flags,"param:",param)
if event == cv.EVENT_LBUTTONDBLCLK: cv.circle(img,(x,y),10,(255,0,0),-1)
img = np.zeros((512,512,3), np.uint8)cv.namedWindow('image')cv.setMouseCallback('image',draw_circle)while(1): cv.imshow('image',img) if cv.waitKey(20) & 0xFF == 27: # Esc breakcv.destroyAllWindows()

OpenCV 初探 OpenCV 初探


滚动条:改变颜色

import numpy as npimport cv2 as cvdef nothing(x): pass# Create a black image, a windowimg = np.zeros((300,512,3), np.uint8)cv.namedWindow('image')# create trackbars for color changecv.createTrackbar('R','image',0,255,nothing)cv.createTrackbar('G','image',0,255,nothing)cv.createTrackbar('B','image',0,255,nothing)# create switch for ON/OFF functionalityswitch = '0 : OFF 
1 : ON'cv.createTrackbar(switch, 'image',0,1,nothing)while(1): cv.imshow('image',img) k = cv.waitKey(1) & 0xFF if k == 27: break # get current positions of four trackbars r = cv.getTrackbarPos('R','image') g = cv.getTrackbarPos('G','image') b = cv.getTrackbarPos('B','image') s = cv.getTrackbarPos(switch,'image') if s == 0: img[:] = 0 else: img[:] = [b,g,r]cv.destroyAllWindows()



好了,就介绍到这里,代码主要来自官网和视频教程。等我学习到新的OpenCV技术再展示给大家看。


以上是关于OpenCV 初探的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV 初探

在 Python 多处理进程中运行较慢的 OpenCV 代码片段

OpenCV入门初探---拿走就能用的 人脸检测小项目 源码分析

Opencv 初探录制视频保持视频播放视频

初探JSP与LEeclipse

图像识别——《OpenCV3编程入门-毛星云》第二部分 初探core组件