Introduction to Image Processing, Computer Vision and Deep Learning
目錄
1. Find Contour
1) Draw Contour
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Follow the steps:
- RGB -> Grayscale -> Binary
- Remember to use Gaussian Blur to remove the noise.
- Using some edge detection functions to get better results. (Ex: cv2.Canny)
- use cv2.findContours
- use cv2.drawContours
Example code:
2) Count Coins
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
2. Camera Calibration
Relationship of 2D x Intrinsic x Extrinsic x 3D
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
1) Corner detection
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Follow the steps:
- Given 15 of chessboard images
- Read the images and convert to grayscale
- Find the chessboard corners
- Draw and display the corners
Example code:
2) Find the intrinsic matrix
Find the intrinsic matrix:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Follow the steps:
- termination criteria
- prepare object points, like (0,0,0), (1,0,0), (2,0,0) …,(6,5,0)
- Arrays to store object points and image points from all the images
- Select all images, convert to grayscale, and find the chessboard corners
- Further optimization calculations on the corners
- use cv2.calibrateCamera
Example code:
3) Find the extrinsic matrix
Extrinsic matrix:

Follow the steps:
- Follow 2) Find the intrinsic matrix
- use cv2.Rodrigues 做旋轉向量和旋轉矩陣的轉換
- use np.hstack 做陣列橫向合併
Bonus: 陣列縱向合併–np.vstack()
Example code:
4) Find the distortion matrix
Distortion Coefficients: k1, k2, k3, p1, p2
Follow the steps:
- Follow 2) Find the intrinsic matrix
Example code:
參考:
- https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga687a1ab946686f0d85ae0363b5af1d7b
3. Augmented Reality

Follow the steps:
- Follow 2) Find the intrinsic matrix
- prepare object points which you want to draw, like (3,3,-3), (1,1,0), (3,5,0), (5,1,0)
- use cv2.projectPoints to project 3D points to image plane
- use cv2.line draw line
Example code:
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
objp = np.zeros((11 * 8, 3), np.float32)
objp[:, :2] = np.mgrid[0:8, 0:11].T.reshape(-1, 2)
axis = np.float32([[5, 3, -3], [7, 1, 0], [3, 3, 0], [7, 5, 0]]).reshape(-1, 3)
objpoints = []
imgpoints = []
chess_images = glob.glob('./Datasets/Q3_Image/*.bmp')
for i in range(len(chess_images)):
image = cv2.imread(chess_images[i])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (8, 11), None)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (7, 7), (-1, -1), criteria)
imgpoints.append(corners2)
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (2048, 2048), None, None)
imgpts, jac = cv2.projectPoints(axis, rvecs[i], tvecs[i], mtx, dist)
def draw(image, imgpts):
image = cv2.line(image, tuple(imgpts[0].ravel()), tuple(imgpts[1].ravel()), (0, 0, 255), 5)
image = cv2.line(image, tuple(imgpts[0].ravel()), tuple(imgpts[2].ravel()), (0, 0, 255), 5)
image = cv2.line(image, tuple(imgpts[0].ravel()), tuple(imgpts[3].ravel()), (0, 0, 255), 5)
image = cv2.line(image, tuple(imgpts[1].ravel()), tuple(imgpts[2].ravel()), (0, 0, 255), 5)
image = cv2.line(image, tuple(imgpts[1].ravel()), tuple(imgpts[3].ravel()), (0, 0, 255), 5)
image = cv2.line(image, tuple(imgpts[2].ravel()), tuple(imgpts[3].ravel()), (0, 0, 255), 5)
return image
img = draw(image, imgpts)
cv2.imwrite('%s_v.jpg' % i, img)
img = cv2.resize(img, (1024, 1024), interpolation=cv2.INTER_AREA)
cv2.namedWindow('img')
cv2.imshow('img', img)
cv2.waitKey(5)
參考:
- https://docs.opencv.org/3.4/d9/d0c/group__calib3d.html#ga1019495a2c8d1743ed5cc23fa0daff8c
- https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_calib3d/py_pose/py_pose.html
4. Stereo Disparity Map
Original image(left&right):


Stereo Disparity map:

1) Compute disparity map

Follow the steps:
- open image left and right
- use cv2.StereoBM_create, stereo.compute
2) Calculate the depth
這題做的時候有問題,disparity[y][x]的值一直是0,真的值output不出來QQ
所以code的部分參考參考,可以修改更好。
Follow the steps:
- focal_len = 2826
baseline = 178
Cx(𝑐_𝑥^𝑟𝑖𝑔ℎ𝑡 − 𝑐_𝑥^𝑙𝑒𝑓𝑡) = 123
We know that: 
d(distance) = (your point) - Cx
Z(depth) = focal_length * baseline / d
- use cv2.setMouseCallback to show the points which you clicked
- use cv2.rectangle to draw the point
- use cv2.putText to show disparity and depth
Example code:
imgL = cv2.imread('./Datasets/Q4_Image/imgL.png', 0)
imgR = cv2.imread('./Datasets/Q4_Image/imgR.png', 0)
stereo = cv2.StereoBM_create(numDisparities=256, blockSize=25)
disparity = stereo.compute(imgL, imgR).astype(np.float32) / 16.0
disparity = cv2.resize(disparity, (1400, 950), interpolation=cv2.INTER_AREA)
focal_len = 2826
baseline = 178
Cx = 123
def draw_circle(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.rectangle(disparity, (x-3, y-3), (x+3, y+3), (0, 0, 255), -1)
dist = disparity[y][x] - Cx
depth = int(focal_len * baseline / abs(dist))
cv2.rectangle(disparity, (1100, 850), (1390, 940), (255, 255, 255), -1)
cv2.putText(disparity, "Disparity: " + str(int(disparity[y][x])) + " pixels", (1120, 890),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(disparity, "Depth: " + str(depth) + " mm", (1120, 930),
cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 0), 2, cv2.LINE_AA)
cv2.imshow('image', disparity)
while(1):
cv2.namedWindow('image')
cv2.setMouseCallback('image', draw_circle)
cv2.imshow('image', disparity)
if cv2.waitKey(20) & 0xFF == 27:
break
cv2.waitKey(0)
cv2.destroyAllWindows()