অধ্যায়Phase 3 · ক্লাসিক্যাল কম্পিউটার ভিশন
3.7 22 মিনিট পড়া
Camera Calibration Basics
Intrinsic ও extrinsic parameters।
🎬 গল্প দিয়ে শুরু
ক্যামেরায় তোলা ছবিতে সরল রেখা বাঁকা দেখায় (lens distortion)। 3D জগতের একটি বস্তু 2D pixel-এ কীভাবে map হয়? এই সব প্রশ্নের উত্তর — Camera Calibration। Computer Vision-কে metric measurement-এ রূপান্তরের চাবি।
Camera Model — Pinhole
আদর্শ pinhole camera-তে 3D point (X,Y,Z) ছবিতে map হয়:
text
x = fx * (X/Z) + cx
y = fy * (Y/Z) + cy
fx, fy = focal length (pixel)
cx, cy = principal point (image center)এদের বলে intrinsic parameters — ক্যামেরার নিজস্ব।
Extrinsic Parameters
ক্যামেরা পৃথিবীর কোথায়, কোন দিকে তাকিয়ে — এটাই extrinsic (rotation R + translation t)। দুটো মিলে full projection:
text
s · [u v 1]ᵀ = K · [R | t] · [X Y Z 1]ᵀ
K = intrinsic matrix (3x3)Distortion Coefficients
- Radial (k1, k2, k3) — barrel/pincushion bending
- Tangential (p1, p2) — lens মাউন্ট-এ ভুল alignment
Chessboard Calibration
python
calibrate.py
import cv2, numpy as np, glob
CB = (9, 6) # inner corners
objp = np.zeros((CB[0]*CB[1], 3), np.float32)
objp[:,:2] = np.mgrid[0:CB[0], 0:CB[1]].T.reshape(-1,2)
objpoints, imgpoints = [], []
for fname in glob.glob("calib/*.jpg"):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CB, None)
if ret:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11,11), (-1,-1),
(cv2.TERM_CRITERIA_EPS+cv2.TERM_CRITERIA_MAX_ITER, 30, 1e-3))
imgpoints.append(corners2)
ret, K, dist, rvecs, tvecs = cv2.calibrateCamera(
objpoints, imgpoints, gray.shape[::-1], None, None)
print("K =\n", K)
print("dist =", dist.ravel())
np.savez("calib.npz", K=K, dist=dist)ভালো calibration-এর tip
১৫-২০টি ছবি — বিভিন্ন angle, distance, ছবির বিভিন্ন কোণায় chessboard। সব ছবি sharp ও properly lit।
Undistort করা
python
import cv2, numpy as np
data = np.load("calib.npz"); K, dist = data["K"], data["dist"]
img = cv2.imread("distorted.jpg")
h, w = img.shape[:2]
newK, roi = cv2.getOptimalNewCameraMatrix(K, dist, (w,h), 1, (w,h))
und = cv2.undistort(img, K, dist, None, newK)
x,y,w2,h2 = roi
und = und[y:y+h2, x:x+w2]
cv2.imwrite("undistorted.jpg", und)কেন এটি গুরুত্বপূর্ণ?
- AR — virtual object সঠিক angle-এ overlay করতে।
- Robotics/Drone — pixel → real-world distance।
- Stereo vision — depth হিসাব আগে দুটি ক্যামেরা calibrate দরকার।
- Photogrammetry, 3D reconstruction — calibration ছাড়া অসম্ভব।
Phase 3 শেষ — কী শিখলেন?
- Contour analysis — shape, count, document scan।
- Feature detection — Harris, Shi-Tomasi, SIFT, ORB।
- Feature matching — BF, FLANN, Lowe ratio, homography।
- Optical flow — sparse LK ও dense Farneback।
- Motion detection ও background subtraction।
- Camera calibration — pixel থেকে real-world geometry।
পরবর্তী — Phase 4: Video & Real-Time Systems
এখন আপনি classical CV-এর প্রায় পুরোটাই জানেন। পরের Phase-এ আমরা এই tool-গুলো একত্র করে real-time video pipeline — webcam, FPS, threading, object tracker — সব দাঁড় করাবো।
প্র্যাকটিস টাস্ক
- প্রিন্ট-করা 9×6 chessboard দিয়ে নিজের webcam calibrate করুন।
- Calibration-এর আগে ও পরে straight-line photo compare করুন।
- Calibrated camera দিয়ে aruco marker detect করে real-world distance বের করুন (বোনাস)।