[OpenCV] Basics review notes (1) & PyCharm unboxing
Preface
之前做Computer vision的東西都在Google colab做,因為最熟悉的是jupter notebook,加上需要使用GPU來跑機器學習,就沒有考慮要在local開發,頂多要改python檔的時候開Xcode。然而久聞Pycharm大名,加上開發Revit API的時候用Reshaper帶來的極致體驗(開發不大的專案所以沒有影響到效能的體驗lol),所以就換了環境想試用看看jet brain 的Python IDE產品。
Setup in Pycharm (Community Free Edition)
Firstly create a new project and then click the settings ( up right corner of the window). Find the ‘Project: yourProjectName’ on the sidebar, and click on the Python Interpreter. There will be an adding icon, click it and choose desired packages to install.
modules are usually imported before coding with OpenCV:
import cv2 as cv
import numpy as np
Basics
Read, Show, and Write images
Three main functions: imread(), imshow() and imwrite():
# read the image from a file
img = cv.imread('image path')
img_grayscale = cv.imread('image path', 0) # show the image
cv.imshow('Image', img)
cv.imshow('Gray', img_grayscale)
# write the gray image
cv.imwrite('grayscale.jpg', img_grayscale)
cv.waitKey(0)
cv.destroyAllwindows()
Read and show videos
Videos are displayed by each frame, so a loop is needed. VideoCapture() is for video reading.
# Create a video capture object, in this case we are reading the video from a file
vid = cv.VideoCapture('video path')
# display the video
while True:
isTrue, frame = vid.read()
cv.imshow('Video', frame)
key = cv.waitKey(20)
if key == ord('q'):
break
vid.release()
cv.destroyAllWindows()
Resize and Rescale Frames
resize() can be used by setting the width and height respectively,
# get the current size of the image
h, w, c = img.shape
print("Original Height and Width:", h, "x", w)
# set the width and height
# define the width and height (w,h)
resizeWH = (300, 400)
resize_down = cv.resize(img, resizeWH)
cv.imshow('Resized Down by defining height and width', resize_down)
cv.waitKey()
or setting the scaling factor by assigning it to the parameters fx and fy
# set scaling factor
scaling_f = 1.5
scaled_f_up = cv.resize(img, None, fx=scaling_f, fy=scaling_f)
cv.imshow('Resized Up by defining scaling factor', scaled_f_up)
cv.waitKey()
With live videos, the function works:
vid.set(3, width)
vid.set(4, height)
Draw shapes and put text
From the documentation of OpenCV, there are a few functions to use for regular ones, like rectangle(), circle(), ellipse(), etc. putText() is for putting text on the view.
# define img
blank = np.zeros((500, 500), dtype='uint8')
# draw rectangle
# cv.rectangle(img, pt1, pt2, color, thickness)
cv.rectangle(blank, (0, 0), (200, 300), (255, 0, 0), thickness=2)
# draw ellipse
# cv.ellipse(img, center, axes, angle, startAngle, endAngle, color[, thickness[, lineType[, shift]]])
cv.ellipse(blank, (150, 150), (100, 50), 360, 0, 360, (255, 255, 0), thickness=3)
# draw circle
# cv.circle(img, center, radius, color[, thickness[, lineType[, shift]]])
cv.circle(blank, (250, 250), 50, color=(100, 100, 255), thickness=5)# put text
# cv.putText(img, text, org, fontFace, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]] ) -> img
cv.putText(blank, "Hello world", (30, 150),cv.FONT_HERSHEY_TRIPLEX, 1.0, (0, 0, 0), 2)cv.imshow('blank', blank)
cv.waitKey(0)