- Practical Computer Vision
- Abhinav Dadhich
- 339字
- 2021-06-30 18:54:45
Opencv FAQs
Though we saw an introductory OpenCV programs in previous chapter, we will see some more frequently used code snippets that will be used throughout this book.
- Let's begin with importing OpenCV and will print the version of OpenCV used:
import cv2
print(cv2.__version__)
- We can read an image from a file as:
img = cv2.imread('flower.png')
The previous snippet will decode an image stored in common formats such as .jpg, .png , .jpeg , .tiff , .pgm, and so on. using image codecs either installed with OpenCV or available on the platform. If there are no codecs available, then OpenCV will not be able to read image or write image to a file. So, it is necessary for the user to install codecs on a non-supported platforms such as embedded devices.
We can write an image to file as:
cv2.imwrite('image.png', img)
In writing a file also there is need for image codecs which are generally installed with OpenCV. We can write the image with file formats such as JPG, PNG, JPEG, TIFF, and so on.
Processing a video includes opening a video file and applying algorithms on each frame. We will first initialize the source of frames which can be a video file or an attached USB camera as:
# to use default usb camera set the value to 0
video_capture = cv2.VideoCapture(0)
Or we can also write it as follows:
# to use video file, set filename
video_capture = cv2.VideoCapture('video.avi')
Similar to image reading and writing, video reading will also require codecs which are installed with OpenCV or available from the OS. Once the source is setup we can continue processing each frame as:
while(True):
# get each frame
ret, frame = video_capture.read()
# if no frame available then quit
if not ret:
print("Frame not available")
break
# show read frame in window
cv2.imshow('frame', frame)
# escape the loop on pressing 'q'
if cv2.waitKey(1) & 0xFF == ord('q'):
break
Here cv2.imshow is to display image and cv2.waitKey() is time delay in the execution.
- 協(xié)作機(jī)器人技術(shù)及應(yīng)用
- Hadoop 2.x Administration Cookbook
- R Machine Learning By Example
- Hands-On Machine Learning on Google Cloud Platform
- 走入IBM小型機(jī)世界
- 大數(shù)據(jù)安全與隱私保護(hù)
- VB語言程序設(shè)計
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- Android游戲開發(fā)案例與關(guān)鍵技術(shù)
- Mastering ServiceNow Scripting
- 網(wǎng)絡(luò)安全技術(shù)及應(yīng)用
- 計算機(jī)與信息技術(shù)基礎(chǔ)上機(jī)指導(dǎo)
- Excel 2007常見技法與行業(yè)應(yīng)用實例精講
- 從零開始學(xué)Java Web開發(fā)
- INSTANT Adobe Story Starter