├── LICENSE ├── Computer Vision Fundamentals ├── 01 Core_Image_Processing │ ├── 01 Image_Basics │ │ └── image_basics.py │ ├── 04 Color_Processing │ │ └── color_processing.py │ ├── 03 Image_Filtering │ │ └── image_filtering.py │ ├── 02 Image_Transformations │ │ └── image_transformations.py │ └── README.md ├── 03 Video_Processing │ ├── 01 Video_Basics │ │ └── video_basics.py │ ├── 02 Video_Analysis │ │ └── video_analysis.py │ ├── 03 Object_Tracking │ │ └── object_tracking.py │ └── README.md ├── 04 Object_Detection_and_Recognition │ ├── 01 Template_Matching │ │ └── template_matching.py │ ├── 02 Haar_Cascades │ │ └── haar_cascades.py │ ├── 04 Image_Classification │ │ └── image_classification.py │ ├── 03 Deep_Learning_Based_Detection │ │ └── deep_learning_detection.py │ └── README.md ├── 02 Advanced_Image_Processing │ ├── 03 Optical_Flow │ │ └── optical_flow.py │ ├── 02 Image_Segmentation │ │ └── image_segmentation.py │ ├── 01 Feature_Detection │ │ └── feature_detection.py │ └── README.md └── 05 Optimization_and_Deployment │ ├── 01 Performance_Optimization │ └── performance_optimization.py │ ├── 03 Integration │ └── integration.py │ ├── 02 Deployment │ └── deployment.py │ └── README.md ├── README.md └── Computer Vision Interview Questions └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/01 Image_Basics/image_basics.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Basics] 2 | # Learn to read/write images, manipulate pixels, and explore color spaces with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_basics_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a red rectangle 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [0, 0, 255] # Red square (BGR format) 14 | print("Synthetic Image: 300x300 RGB with red square created") 15 | 16 | # %% [3. Reading/Writing Images] 17 | cv2.imwrite("synthetic_image.png", img) 18 | img_read = cv2.imread("synthetic_image.png") 19 | print("Image Read/Written: synthetic_image.png") 20 | 21 | # %% [4. Color Spaces] 22 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 23 | img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 24 | print(f"Color Spaces: Converted to Grayscale and HSV") 25 | 26 | # %% [5. Pixel Manipulation] 27 | img[50:100, 50:100] = [255, 0, 0] # Add blue square 28 | print("Pixel Manipulation: Added blue square at (50:100, 50:100)") 29 | 30 | # %% [6. Image Properties] 31 | height, width, channels = img.shape 32 | print(f"Image Properties: Height={height}, Width={width}, Channels={channels}") 33 | 34 | # %% [7. Visualization] 35 | plt.figure(figsize=(10, 8)) 36 | plt.subplot(1, 3, 1) 37 | plt.title("Original (BGR)") 38 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 39 | plt.subplot(1, 3, 2) 40 | plt.title("Grayscale") 41 | plt.imshow(img_gray, cmap="gray") 42 | plt.subplot(1, 3, 3) 43 | plt.title("HSV") 44 | plt.imshow(cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)) 45 | plt.savefig("image_basics_output.png") 46 | print("Visualization: Images saved as image_basics_output.png") 47 | 48 | # %% [8. Interview Scenario: Image Basics] 49 | """ 50 | Interview Scenario: Image Basics 51 | Q: What’s the difference between RGB and HSV color spaces? 52 | A: RGB defines colors by red, green, blue intensities; HSV uses hue, saturation, value for intuitive color manipulation. 53 | Key: HSV is better for color-based segmentation (e.g., filtering specific hues). 54 | Example: cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 55 | """ 56 | 57 | # Execute the demo 58 | if __name__ == "__main__": 59 | run_image_basics_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/01 Video_Basics/video_basics.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Video Basics] 2 | # Learn to read/write videos, extract frames, and access video properties with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_video_basics_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate a sequence of 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | print("Synthetic Video: 10 frames with moving white square created") 19 | 20 | # %% [3. Write Video] 21 | height, width = 300, 300 22 | fps = 10 23 | out = cv2.VideoWriter("synthetic_video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height)) 24 | for frame in frames: 25 | out.write(frame) 26 | out.release() 27 | print("Video Written: synthetic_video.mp4 saved") 28 | 29 | # %% [4. Read Video and Extract Frames] 30 | cap = cv2.VideoCapture("synthetic_video.mp4") 31 | extracted_frames = [] 32 | while cap.isOpened(): 33 | ret, frame = cap.read() 34 | if not ret: 35 | break 36 | extracted_frames.append(frame) 37 | cap.release() 38 | print(f"Video Read: {len(extracted_frames)} frames extracted") 39 | 40 | # %% [5. Video Properties] 41 | cap = cv2.VideoCapture("synthetic_video.mp4") 42 | fps = cap.get(cv2.CAP_PROP_FPS) 43 | width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 44 | height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 45 | frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 46 | print(f"Video Properties: FPS={fps}, Resolution={width}x{height}, Frames={frame_count}") 47 | cap.release() 48 | 49 | # %% [6. Visualization] 50 | plt.figure(figsize=(15, 5)) 51 | for i, idx in enumerate([0, 4, 9]): # Show first, middle, last frames 52 | plt.subplot(1, 3, i+1) 53 | plt.title(f"Frame {idx+1}") 54 | plt.imshow(cv2.cvtColor(extracted_frames[idx], cv2.COLOR_BGR2RGB)) 55 | plt.savefig("video_basics_output.png") 56 | print("Visualization: Frames saved as video_basics_output.png") 57 | 58 | # %% [7. Interview Scenario: Video Basics] 59 | """ 60 | Interview Scenario: Video Basics 61 | Q: How do you read a video and extract its properties in OpenCV? 62 | A: Use cv2.VideoCapture to read frames and get properties like FPS, resolution. 63 | Key: Properties are accessed via cap.get(cv2.CAP_PROP_*). 64 | Example: cap.get(cv2.CAP_PROP_FPS) 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_video_basics_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/01 Template_Matching/template_matching.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Template Matching] 2 | # Learn single and multi-object template matching with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_template_matching_demo(): 10 | # %% [2. Create Synthetic Image and Template] 11 | # Generate a 300x300 image with multiple white squares 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[50:100, 50:100] = [255, 255, 255] # Square 1 14 | img[150:200, 150:200] = [255, 255, 255] # Square 2 15 | img[250:300, 50:100] = [255, 255, 255] # Square 3 16 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | 18 | # Create a 50x50 template (white square) 19 | template = np.ones((50, 50), dtype=np.uint8) * 255 20 | print("Synthetic Image: 300x300 with three white squares; Template: 50x50 white square") 21 | 22 | # %% [3. Single Object Matching] 23 | result = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) 24 | _, max_val, _, max_loc = cv2.minMaxLoc(result) 25 | img_single = img.copy() 26 | top_left = max_loc 27 | bottom_right = (top_left[0] + 50, top_left[1] + 50) 28 | cv2.rectangle(img_single, top_left, bottom_right, (0, 0, 255), 2) 29 | print(f"Single Object Matching: Best match at {max_loc}, score={max_val:.2f}") 30 | 31 | # %% [4. Multi-Object Matching] 32 | threshold = 0.8 33 | loc = np.where(result >= threshold) 34 | img_multi = img.copy() 35 | for pt in zip(*loc[::-1]): 36 | bottom_right = (pt[0] + 50, pt[1] + 50) 37 | cv2.rectangle(img_multi, pt, bottom_right, (0, 255, 0), 2) 38 | print(f"Multi-Object Matching: {len(loc[0])} matches found with threshold={threshold}") 39 | 40 | # %% [5. Visualization] 41 | plt.figure(figsize=(15, 5)) 42 | plt.subplot(1, 3, 1) 43 | plt.title("Original Image") 44 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 45 | plt.subplot(1, 3, 2) 46 | plt.title("Single Match") 47 | plt.imshow(cv2.cvtColor(img_single, cv2.COLOR_BGR2RGB)) 48 | plt.subplot(1, 3, 3) 49 | plt.title("Multi Match") 50 | plt.imshow(cv2.cvtColor(img_multi, cv2.COLOR_BGR2RGB)) 51 | plt.savefig("template_matching_output.png") 52 | print("Visualization: Template matching results saved as template_matching_output.png") 53 | 54 | # %% [6. Interview Scenario: Template Matching] 55 | """ 56 | Interview Scenario: Template Matching 57 | Q: How does template matching work, and what are its limitations? 58 | A: Template matching slides a template over an image, computing similarity (e.g., normalized correlation). 59 | Key: Limited to fixed-scale, rotation-invariant templates; sensitive to lighting changes. 60 | Example: cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) 61 | """ 62 | 63 | # Execute the demo 64 | if __name__ == "__main__": 65 | run_template_matching_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/02 Haar_Cascades/haar_cascades.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Haar Cascades] 2 | # Learn face and eye detection using pretrained Haar cascades with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_haar_cascades_demo(): 10 | # %% [2. Create Synthetic Face Image] 11 | # Generate a 300x300 image with a simplified face (ovals for face and eyes) 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | cv2.ellipse(img, (150, 150), (100, 120), 0, 0, 360, (200, 200, 200), -1) # Face 14 | cv2.ellipse(img, (120, 120), (20, 30), 0, 0, 360, (50, 50, 50), -1) # Left eye 15 | cv2.ellipse(img, (180, 120), (20, 30), 0, 0, 360, (50, 50, 50), -1) # Right eye 16 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | print("Synthetic Image: 300x300 with simplified face and eyes created") 18 | 19 | # %% [3. Load Haar Cascade Classifiers] 20 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") 21 | eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml") 22 | print("Haar Cascades: Loaded face and eye classifiers") 23 | 24 | # %% [4. Face Detection] 25 | faces = face_cascade.detectMultiScale(img_gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) 26 | img_faces = img.copy() 27 | for (x, y, w, h) in faces: 28 | cv2.rectangle(img_faces, (x, y), (x+w, y+h), (0, 0, 255), 2) 29 | print(f"Face Detection: {len(faces)} faces detected") 30 | 31 | # %% [5. Eye Detection] 32 | eyes = eye_cascade.detectMultiScale(img_gray, scaleFactor=1.1, minNeighbors=5, minSize=(20, 20)) 33 | img_eyes = img.copy() 34 | for (x, y, w, h) in eyes: 35 | cv2.rectangle(img_eyes, (x, y), (x+w, y+h), (0, 255, 0), 2) 36 | print(f"Eye Detection: {len(eyes)} eyes detected") 37 | 38 | # %% [6. Visualization] 39 | plt.figure(figsize=(15, 5)) 40 | plt.subplot(1, 3, 1) 41 | plt.title("Original Image") 42 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 43 | plt.subplot(1, 3, 2) 44 | plt.title("Face Detection") 45 | plt.imshow(cv2.cvtColor(img_faces, cv2.COLOR_BGR2RGB)) 46 | plt.subplot(1, 3, 3) 47 | plt.title("Eye Detection") 48 | plt.imshow(cv2.cvtColor(img_eyes, cv2.COLOR_BGR2RGB)) 49 | plt.savefig("haar_cascades_output.png") 50 | print("Visualization: Haar cascade results saved as haar_cascades_output.png") 51 | 52 | # %% [7. Interview Scenario: Haar Cascades] 53 | """ 54 | Interview Scenario: Haar Cascades 55 | Q: What are the limitations of Haar cascades for face detection? 56 | A: Haar cascades are fast but struggle with non-frontal faces, occlusions, and varying lighting. 57 | Key: Use integral images for efficiency but less robust than deep learning. 58 | Example: cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") 59 | """ 60 | 61 | # Execute the demo 62 | if __name__ == "__main__": 63 | run_haar_cascades_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/04 Color_Processing/color_processing.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Color Processing] 2 | # Learn color space conversion, color filtering, histogram equalization, and quantization with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_color_processing_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a blue-to-red gradient 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | for i in range(300): 14 | img[i, :, 0] = i % 255 # Blue gradient 15 | img[i, :, 2] = 255 - (i % 255) # Red gradient 16 | print("Synthetic Image: 300x300 RGB with blue-to-red gradient created") 17 | 18 | # %% [3. Color Space Conversion] 19 | img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 20 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 21 | print("Color Space Conversion: Converted to HSV and Grayscale") 22 | 23 | # %% [4. Color Filtering] 24 | # Filter blue colors in HSV 25 | lower_blue = np.array([100, 50, 50]) 26 | upper_blue = np.array([130, 255, 255]) 27 | mask = cv2.inRange(img_hsv, lower_blue, upper_blue) 28 | img_filtered = cv2.bitwise_and(img, img, mask=mask) 29 | print("Color Filtering: Isolated blue colors") 30 | 31 | # %% [5. Histogram Equalization] 32 | img_eq = cv2.equalizeHist(img_gray) 33 | print("Histogram Equalization: Applied to Grayscale image") 34 | 35 | # %% [6. Color Quantization] 36 | # Reduce colors using K-Means 37 | Z = img.reshape((-1, 3)).astype(np.float32) 38 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 39 | K = 4 40 | _, labels, centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) 41 | centers = np.uint8(centers) 42 | img_quantized = centers[labels.flatten()].reshape(img.shape) 43 | print(f"Color Quantization: Reduced to {K} colors") 44 | 45 | # %% [7. Visualization] 46 | plt.figure(figsize=(15, 8)) 47 | plt.subplot(2, 3, 1) 48 | plt.title("Original") 49 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 50 | plt.subplot(2, 3, 2) 51 | plt.title("HSV") 52 | plt.imshow(cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)) 53 | plt.subplot(2, 3, 3) 54 | plt.title("Grayscale") 55 | plt.imshow(img_gray, cmap="gray") 56 | plt.subplot(2, 3, 4) 57 | plt.title("Blue Filtered") 58 | plt.imshow(cv2.cvtColor(img_filtered, cv2.COLOR_BGR2RGB)) 59 | plt.subplot(2, 3, 5) 60 | plt.title("Equalized") 61 | plt.imshow(img_eq, cmap="gray") 62 | plt.subplot(2, 3, 6) 63 | plt.title("Quantized") 64 | plt.imshow(cv2.cvtColor(img_quantized, cv2.COLOR_BGR2RGB)) 65 | plt.savefig("color_processing_output.png") 66 | print("Visualization: Color processing saved as color_processing_output.png") 67 | 68 | # %% [8. Interview Scenario: Color Processing] 69 | """ 70 | Interview Scenario: Color Processing 71 | Q: Why is HSV useful for color filtering compared to RGB? 72 | A: HSV separates hue (color type) from saturation and value, making it easier to isolate specific colors. 73 | Key: Use cv2.inRange in HSV for robust color segmentation. 74 | Example: cv2.inRange(img_hsv, lower_blue, upper_blue) 75 | """ 76 | 77 | # Execute the demo 78 | if __name__ == "__main__": 79 | run_color_processing_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/04 Image_Classification/image_classification.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Classification] 2 | # Learn pretrained MobileNetV2 classification and transfer learning with TensorFlow. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import tensorflow as tf 9 | from tensorflow.keras.applications import MobileNetV2 10 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 11 | 12 | def run_image_classification_demo(): 13 | # %% [2. Load CIFAR-10 Dataset] 14 | (x_train, y_train), _ = tf.keras.datasets.cifar10.load_data() 15 | class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] 16 | img = x_train[0] # Example: airplane image 17 | print("CIFAR-10: Loaded sample image for classification") 18 | 19 | # %% [3. Pretrained MobileNetV2 Classification] 20 | model = MobileNetV2(weights='imagenet') 21 | img_resized = cv2.resize(img, (224, 224)) 22 | img_array = np.expand_dims(img_resized, axis=0) 23 | img_preprocessed = preprocess_input(img_array) 24 | predictions = model.predict(img_preprocessed) 25 | decoded_preds = decode_predictions(predictions, top=3)[0] 26 | img_pred = img.copy() 27 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 28 | cv2.putText(img_pred, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 29 | print(f"MobileNetV2: Top prediction: {decoded_preds[0][1]} ({decoded_preds[0][2]:.2f})") 30 | 31 | # %% [4. Transfer Learning (Simulated)] 32 | # Simulate fine-tuning MobileNetV2 for CIFAR-10 (subset) 33 | base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) 34 | base_model.trainable = False 35 | model = tf.keras.Sequential([ 36 | base_model, 37 | tf.keras.layers.GlobalAveragePooling2D(), 38 | tf.keras.layers.Dense(10, activation='softmax') 39 | ]) 40 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 41 | model.fit(x_train[:1000], y_train[:1000], epochs=1, batch_size=32, verbose=0) 42 | pred = model.predict(x_train[:1]) 43 | class_id = np.argmax(pred[0]) 44 | print(f"Transfer Learning: Predicted class for sample: {class_names[class_id]}") 45 | 46 | # %% [5. Visualization] 47 | plt.figure(figsize=(10, 5)) 48 | plt.subplot(1, 2, 1) 49 | plt.title("Original Image") 50 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 51 | plt.subplot(1, 2, 2) 52 | plt.title("MobileNetV2 Prediction") 53 | plt.imshow(cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)) 54 | plt.savefig("image_classification_output.png") 55 | print("Visualization: Classification results saved as image_classification_output.png") 56 | 57 | # %% [6. Interview Scenario: Image Classification] 58 | """ 59 | Interview Scenario: Image Classification 60 | Q: How does transfer learning improve image classification? 61 | A: Transfer learning uses pretrained models (e.g., MobileNetV2) to leverage learned features, reducing training time and data needs. 62 | Key: Fine-tune top layers for specific tasks like CIFAR-10 classification. 63 | Example: MobileNetV2(weights='imagenet', include_top=False) 64 | """ 65 | 66 | # Execute the demo 67 | if __name__ == "__main__": 68 | run_image_classification_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/03 Optical_Flow/optical_flow.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Optical Flow] 2 | # Learn dense and sparse optical flow and motion tracking with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_optical_flow_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate two 300x300 frames with a moving white square 12 | frame1 = np.zeros((300, 300, 3), dtype=np.uint8) 13 | frame2 = np.zeros((300, 300, 3), dtype=np.uint8) 14 | frame1[100:150, 100:150] = [255, 255, 255] # White square 15 | frame2[110:160, 110:160] = [255, 255, 255] # Shifted 10px right, down 16 | frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) 17 | frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) 18 | print("Synthetic Frames: Two frames with moving white square created") 19 | 20 | # %% [3. Dense Optical Flow (Farneback)] 21 | flow = cv2.calcOpticalFlowFarneback(frame1_gray, frame2_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) 22 | mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) 23 | hsv = np.zeros_like(frame1) 24 | hsv[..., 1] = 255 25 | hsv[..., 0] = ang * 180 / np.pi / 2 26 | hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) 27 | img_flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) 28 | print("Dense Optical Flow: Farneback flow computed") 29 | 30 | # %% [4. Sparse Optical Flow (Lucas-Kanade)] 31 | corners = cv2.goodFeaturesToTrack(frame1_gray, maxCorners=10, qualityLevel=0.3, minDistance=7) 32 | p1, st, err = cv2.calcOpticalFlowPyrLK(frame1_gray, frame2_gray, corners, None) 33 | img_lk = frame2.copy() 34 | for i, (new, old) in enumerate(zip(p1[st==1], corners[st==1])): 35 | a, b = new.ravel() 36 | c, d = old.ravel() 37 | cv2.line(img_lk, (int(a), int(b)), (int(c), int(d)), (0, 255, 0), 2) 38 | cv2.circle(img_lk, (int(a), int(b)), 5, (0, 0, 255), -1) 39 | print("Sparse Optical Flow: Lucas-Kanade tracked keypoints") 40 | 41 | # %% [5. Motion Tracking] 42 | # Simulate tracking by drawing bounding box around moving square 43 | x, y = int(p1[st==1][0][0]), int(p1[st==1][0][1]) 44 | img_track = frame2.copy() 45 | cv2.rectangle(img_track, (x-25, y-25), (x+25, y+25), (0, 0, 255), 2) 46 | print("Motion Tracking: Bounding box drawn around moving square") 47 | 48 | # %% [6. Visualization] 49 | plt.figure(figsize=(15, 8)) 50 | plt.subplot(2, 2, 1) 51 | plt.title("Frame 1") 52 | plt.imshow(cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB)) 53 | plt.subplot(2, 2, 2) 54 | plt.title("Frame 2") 55 | plt.imshow(cv2.cvtColor(frame2, cv2.COLOR_BGR2RGB)) 56 | plt.subplot(2, 2, 3) 57 | plt.title("Dense Flow") 58 | plt.imshow(cv2.cvtColor(img_flow, cv2.COLOR_BGR2RGB)) 59 | plt.subplot(2, 2, 4) 60 | plt.title("Lucas-Kanade") 61 | plt.imshow(cv2.cvtColor(img_lk, cv2.COLOR_BGR2RGB)) 62 | plt.savefig("optical_flow_output.png") 63 | print("Visualization: Optical flow results saved as optical_flow_output.png") 64 | 65 | # %% [7. Interview Scenario: Optical Flow] 66 | """ 67 | Interview Scenario: Optical Flow 68 | Q: What are the applications of optical flow in computer vision? 69 | A: Optical flow estimates motion for tracking, video stabilization, and activity recognition. 70 | Key: Dense flow (Farneback) is global; sparse flow (Lucas-Kanade) tracks specific points. 71 | Example: cv2.calcOpticalFlowFarneback(frame1, frame2, ...) 72 | """ 73 | 74 | # Execute the demo 75 | if __name__ == "__main__": 76 | run_optical_flow_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/03 Image_Filtering/image_filtering.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Filtering] 2 | # Learn blurring, sharpening, edge detection, thresholding, and morphological operations with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_filtering_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a white rectangle on black 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [255, 255, 255] # White square (BGR format) 14 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 15 | print("Synthetic Image: 300x300 RGB with white square created") 16 | 17 | # %% [3. Blurring] 18 | img_gaussian = cv2.GaussianBlur(img_gray, (5, 5), 0) 19 | img_median = cv2.medianBlur(img_gray, 5) 20 | img_bilateral = cv2.bilateralFilter(img_gray, 9, 75, 75) 21 | print("Blurring: Applied Gaussian, Median, and Bilateral filters") 22 | 23 | # %% [4. Sharpening] 24 | kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]], dtype=np.float32) 25 | img_sharpened = cv2.filter2D(img_gray, -1, kernel) 26 | print("Sharpening: Applied sharpening filter") 27 | 28 | # %% [5. Edge Detection] 29 | img_sobel = cv2.Sobel(img_gray, cv2.CV_64F, 1, 0, ksize=3) 30 | img_sobel = cv2.convertScaleAbs(img_sobel) 31 | img_canny = cv2.Canny(img_gray, 100, 200) 32 | print("Edge Detection: Applied Sobel and Canny") 33 | 34 | # %% [6. Thresholding] 35 | _, img_binary = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) 36 | img_adaptive = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) 37 | print("Thresholding: Applied Binary and Adaptive") 38 | 39 | # %% [7. Morphological Operations] 40 | kernel = np.ones((5,5), np.uint8) 41 | img_dilation = cv2.dilate(img_binary, kernel, iterations=1) 42 | img_erosion = cv2.erode(img_binary, kernel, iterations=1) 43 | print("Morphological Operations: Applied Dilation and Erosion") 44 | 45 | # %% [8. Visualization] 46 | plt.figure(figsize=(15, 10)) 47 | plt.subplot(2, 4, 1) 48 | plt.title("Original") 49 | plt.imshow(img_gray, cmap="gray") 50 | plt.subplot(2, 4, 2) 51 | plt.title("Gaussian Blur") 52 | plt.imshow(img_gaussian, cmap="gray") 53 | plt.subplot(2, 4, 3) 54 | plt.title("Median Blur") 55 | plt.imshow(img_median, cmap="gray") 56 | plt.subplot(2, 4, 4) 57 | plt.title("Sharpened") 58 | plt.imshow(img_sharpened, cmap="gray") 59 | plt.subplot(2, 4, 5) 60 | plt.title("Sobel") 61 | plt.imshow(img_sobel, cmap="gray") 62 | plt.subplot(2, 4, 6) 63 | plt.title("Canny") 64 | plt.imshow(img_canny, cmap="gray") 65 | plt.subplot(2, 4, 7) 66 | plt.title("Binary Threshold") 67 | plt.imshow(img_binary, cmap="gray") 68 | plt.subplot(2, 4, 8) 69 | plt.title("Dilation") 70 | plt.imshow(img_dilation, cmap="gray") 71 | plt.savefig("image_filtering_output.png") 72 | print("Visualization: Filters saved as image_filtering_output.png") 73 | 74 | # %% [9. Interview Scenario: Image Filtering] 75 | """ 76 | Interview Scenario: Image Filtering 77 | Q: How does Canny edge detection work, and what are its parameters? 78 | A: Canny uses Gaussian blur, gradient computation, non-maximum suppression, and hysteresis thresholding. 79 | Key: Parameters are low/high thresholds for edge strength. 80 | Example: cv2.Canny(img, 100, 200) 81 | """ 82 | 83 | # Execute the demo 84 | if __name__ == "__main__": 85 | run_image_filtering_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/03 Deep_Learning_Based_Detection/deep_learning_detection.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Deep Learning-Based Detection] 2 | # Learn YOLOv3 object detection with OpenCV and TensorFlow. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | # Download: yolov3.weights, yolov3.cfg, coco.names (see README) 6 | import cv2 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | def run_deep_learning_detection_demo(): 11 | # %% [2. Create Synthetic Image] 12 | # Generate a 300x300 image with a car-like shape (rectangle + wheels) 13 | img = np.zeros((300, 300, 3), dtype=np.uint8) 14 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car body 15 | cv2.circle(img, (120, 200), 10, (0, 0, 0), -1) # Left wheel 16 | cv2.circle(img, (180, 200), 10, (0, 0, 0), -1) # Right wheel 17 | print("Synthetic Image: 300x300 with car-like shape created") 18 | 19 | # %% [3. Load YOLOv3 Model] 20 | net = cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights") 21 | layer_names = net.getLayerNames() 22 | output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()] 23 | with open("coco.names", "r") as f: 24 | classes = [line.strip() for line in f.readlines()] 25 | print("YOLOv3: Model and COCO classes loaded") 26 | 27 | # %% [4. Preprocess and Detect Objects] 28 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False) 29 | net.setInput(blob) 30 | outs = net.forward(output_layers) 31 | 32 | height, width = img.shape[:2] 33 | boxes = [] 34 | confidences = [] 35 | class_ids = [] 36 | 37 | for out in outs: 38 | for detection in out: 39 | scores = detection[5:] 40 | class_id = np.argmax(scores) 41 | confidence = scores[class_id] 42 | if confidence > 0.5 and class_id == 2: # Class 2 is 'car' in COCO 43 | center_x = int(detection[0] * width) 44 | center_y = int(detection[1] * height) 45 | w = int(detection[2] * width) 46 | h = int(detection[3] * height) 47 | x = int(center_x - w / 2) 48 | y = int(center_y - h / 2) 49 | boxes.append([x, y, w, h]) 50 | confidences.append(float(confidence)) 51 | class_ids.append(class_id) 52 | 53 | indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) 54 | img_yolo = img.copy() 55 | for i in indices: 56 | box = boxes[i] 57 | x, y, w, h = box 58 | label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}" 59 | cv2.rectangle(img_yolo, (x, y), (x+w, y+h), (0, 0, 255), 2) 60 | cv2.putText(img_yolo, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 61 | print(f"YOLOv3 Detection: {len(indices)} objects detected (car-like shape)") 62 | 63 | # %% [5. Visualization] 64 | plt.figure(figsize=(10, 5)) 65 | plt.subplot(1, 2, 1) 66 | plt.title("Original Image") 67 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 68 | plt.subplot(1, 2, 2) 69 | plt.title("YOLOv3 Detection") 70 | plt.imshow(cv2.cvtColor(img_yolo, cv2.COLOR_BGR2RGB)) 71 | plt.savefig("deep_learning_detection_output.png") 72 | print("Visualization: YOLOv3 results saved as deep_learning_detection_output.png") 73 | 74 | # %% [6. Interview Scenario: Deep Learning-Based Detection] 75 | """ 76 | Interview Scenario: Deep Learning-Based Detection 77 | Q: Compare YOLO, SSD, and Faster R-CNN for object detection. 78 | A: YOLO is fast, single-pass, ideal for real-time; SSD balances speed and accuracy; Faster R-CNN is accurate but slower due to region proposals. 79 | Key: YOLOv3 uses a single network with grid-based predictions. 80 | Example: cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights") 81 | """ 82 | 83 | # Execute the demo 84 | if __name__ == "__main__": 85 | run_deep_learning_detection_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/02 Image_Segmentation/image_segmentation.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Segmentation] 2 | # Learn contour detection, watershed, GrabCut, and K-Means clustering with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_segmentation_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 image with overlapping shapes 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | cv2.circle(img, (150, 100), 50, (255, 0, 0), -1) # Blue circle 14 | cv2.rectangle(img, (100, 150, 100, 100), (0, 255, 0), -1) # Green rectangle 15 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | print("Synthetic Image: 300x300 with blue circle and green rectangle created") 17 | 18 | # %% [3. Contour Detection] 19 | _, thresh = cv2.threshold(img_gray, 1, 255, cv2.THRESH_BINARY) 20 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 21 | img_contours = img.copy() 22 | cv2.drawContours(img_contours, contours, -1, (0, 0, 255), 2) 23 | print(f"Contour Detection: {len(contours)} contours found") 24 | 25 | # %% [4. Watershed Algorithm] 26 | dist_transform = cv2.distanceTransform(thresh, cv2.DIST_L2, 5) 27 | _, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) 28 | sure_fg = np.uint8(sure_fg) 29 | unknown = cv2.subtract(thresh, sure_fg) 30 | _, markers = cv2.connectedComponents(sure_fg) 31 | markers = markers + 1 32 | markers[unknown == 255] = 0 33 | img_watershed = img.copy() 34 | markers = cv2.watershed(img_watershed, markers) 35 | img_watershed[markers == -1] = [255, 0, 0] 36 | print("Watershed Algorithm: Segmented overlapping regions") 37 | 38 | # %% [5. GrabCut] 39 | mask = np.zeros(img.shape[:2], np.uint8) 40 | bgdModel = np.zeros((1, 65), np.float64) 41 | fgdModel = np.zeros((1, 65), np.float64) 42 | rect = (50, 50, 200, 200) 43 | cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) 44 | mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') 45 | img_grabcut = img * mask2[:, :, np.newaxis] 46 | print("GrabCut: Foreground segmented with rectangular initialization") 47 | 48 | # %% [6. K-Means Clustering] 49 | Z = img.reshape((-1, 3)).astype(np.float32) 50 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 51 | K = 3 52 | _, labels, centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) 53 | centers = np.uint8(centers) 54 | img_kmeans = centers[labels.flatten()].reshape(img.shape) 55 | print(f"K-Means Clustering: Segmented into {K} colors") 56 | 57 | # %% [7. Visualization] 58 | plt.figure(figsize=(15, 8)) 59 | plt.subplot(2, 3, 1) 60 | plt.title("Original") 61 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 62 | plt.subplot(2, 3, 2) 63 | plt.title("Contours") 64 | plt.imshow(cv2.cvtColor(img_contours, cv2.COLOR_BGR2RGB)) 65 | plt.subplot(2, 3, 3) 66 | plt.title("Watershed") 67 | plt.imshow(cv2.cvtColor(img_watershed, cv2.COLOR_BGR2RGB)) 68 | plt.subplot(2, 3, 4) 69 | plt.title("GrabCut") 70 | plt.imshow(cv2.cvtColor(img_grabcut, cv2.COLOR_BGR2RGB)) 71 | plt.subplot(2, 3, 5) 72 | plt.title("K-Means") 73 | plt.imshow(cv2.cvtColor(img_kmeans, cv2.COLOR_BGR2RGB)) 74 | plt.savefig("image_segmentation_output.png") 75 | print("Visualization: Segmentation results saved as image_segmentation_output.png") 76 | 77 | # %% [8. Interview Scenario: Image Segmentation] 78 | """ 79 | Interview Scenario: Image Segmentation 80 | Q: How does GrabCut work for image segmentation? 81 | A: GrabCut uses graph cuts to separate foreground and background, initialized with a rectangle or mask. 82 | Key: Iteratively refines segmentation using Gaussian Mixture Models. 83 | Example: cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5) 84 | """ 85 | 86 | # Execute the demo 87 | if __name__ == "__main__": 88 | run_image_segmentation_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/01 Performance_Optimization/performance_optimization.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Performance Optimization] 2 | # Learn parallel processing, GPU acceleration, memory management, and algorithm efficiency with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib multiprocess 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | from multiprocess import Pool 9 | import time 10 | 11 | def process_image(img): 12 | """Helper function to process an image (e.g., edge detection).""" 13 | return cv2.Canny(img, 100, 200) 14 | 15 | def run_performance_optimization_demo(): 16 | # %% [2. Create Synthetic Images] 17 | # Generate 10 synthetic 300x300 images with a white square 18 | images = [] 19 | for _ in range(10): 20 | img = np.zeros((300, 300), dtype=np.uint8) 21 | img[100:200, 100:200] = 255 # White square 22 | images.append(img) 23 | print("Synthetic Images: 10 images with white squares created") 24 | 25 | # %% [3. Parallel Processing] 26 | start_time = time.time() 27 | with Pool() as pool: 28 | results_parallel = pool.map(process_image, images) 29 | parallel_time = time.time() - start_time 30 | print(f"Parallel Processing: Processed {len(images)} images in {parallel_time:.2f} seconds") 31 | 32 | # %% [4. Sequential Processing (for Comparison)] 33 | start_time = time.time() 34 | results_sequential = [process_image(img) for img in images] 35 | sequential_time = time.time() - start_time 36 | print(f"Sequential Processing: Processed {len(images)} images in {sequential_time:.2f} seconds") 37 | 38 | # %% [5. GPU Acceleration (Simulated)] 39 | # Note: Requires OpenCV with CUDA support (not standard opencv-python) 40 | # Simulated by describing CUDA-based Canny edge detection 41 | img_cuda = images[0].copy() # Simulate single image processing 42 | # In real CUDA setup: cv2.cuda_GpuMat, cv2.cuda.Canny 43 | img_cuda_canny = cv2.Canny(img_cuda, 100, 200) # CPU fallback for demo 44 | print("GPU Acceleration: Simulated CUDA-based Canny edge detection") 45 | # Real CUDA setup: Install opencv-python with CUDA (e.g., via opencv-contrib-python with CUDA build) 46 | 47 | # %% [6. Memory Management] 48 | # Optimize memory by reusing buffers and reducing copies 49 | img_memory = images[0].copy() 50 | # Pre-allocate output buffer 51 | output_buffer = np.zeros_like(img_memory) 52 | cv2.Canny(img_memory, 100, 200, dst=output_buffer) 53 | print("Memory Management: Used pre-allocated buffer for Canny edge detection") 54 | 55 | # %% [7. Algorithm Efficiency] 56 | # Compare efficient vs. naive edge detection 57 | start_time = time.time() 58 | img_efficient = cv2.Canny(images[0], 100, 200) # Efficient OpenCV Canny 59 | efficient_time = time.time() - start_time 60 | print(f"Algorithm Efficiency: Efficient Canny took {efficient_time:.2f} seconds") 61 | 62 | # %% [8. Visualization] 63 | plt.figure(figsize=(15, 5)) 64 | plt.subplot(1, 3, 1) 65 | plt.title("Original Image") 66 | plt.imshow(images[0], cmap="gray") 67 | plt.subplot(1, 3, 2) 68 | plt.title("Parallel Canny") 69 | plt.imshow(results_parallel[0], cmap="gray") 70 | plt.subplot(1, 3, 3) 71 | plt.title("Memory Optimized Canny") 72 | plt.imshow(output_buffer, cmap="gray") 73 | plt.savefig("performance_optimization_output.png") 74 | print("Visualization: Optimization results saved as performance_optimization_output.png") 75 | 76 | # %% [9. Interview Scenario: Performance Optimization] 77 | """ 78 | Interview Scenario: Performance Optimization 79 | Q: How do you optimize OpenCV for real-time performance? 80 | A: Use parallel processing (e.g., multiprocess.Pool), GPU acceleration (CUDA), memory management (pre-allocated buffers), and efficient algorithms (e.g., OpenCV's optimized Canny). 81 | Key: Profile bottlenecks with tools like cProfile and prioritize GPU for compute-heavy tasks. 82 | Example: pool.map(process_image, images) 83 | """ 84 | 85 | # Execute the demo 86 | if __name__ == "__main__": 87 | run_performance_optimization_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/02 Image_Transformations/image_transformations.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Transformations] 2 | # Learn to resize, crop, rotate, flip, and apply affine/perspective transformations with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_transformations_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a green rectangle 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [0, 255, 0] # Green square (BGR format) 14 | print("Synthetic Image: 300x300 RGB with green square created") 15 | 16 | # %% [3. Resizing] 17 | img_resized = cv2.resize(img, (150, 150), interpolation=cv2.INTER_LINEAR) 18 | print("Resizing: Image resized to 150x150") 19 | 20 | # %% [4. Cropping] 21 | img_cropped = img[50:250, 50:250] 22 | print("Cropping: Image cropped to 200x200") 23 | 24 | # %% [5. Rotation] 25 | center = (img.shape[1]//2, img.shape[0]//2) 26 | matrix = cv2.getRotationMatrix2D(center, 45, 1.0) 27 | img_rotated = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 28 | print("Rotation: Image rotated by 45 degrees") 29 | 30 | # %% [6. Flipping] 31 | img_flipped = cv2.flip(img, 1) # Horizontal flip 32 | print("Flipping: Image flipped horizontally") 33 | 34 | # %% [7. Translation] 35 | matrix = np.float32([[1, 0, 50], [0, 1, 50]]) # Shift 50px right, 50px down 36 | img_translated = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 37 | print("Translation: Image shifted 50px right and down") 38 | 39 | # %% [8. Affine Transformation] 40 | pts1 = np.float32([[50,50], [200,50], [50,200]]) 41 | pts2 = np.float32([[10,100], [200,50], [100,250]]) 42 | matrix = cv2.getAffineTransform(pts1, pts2) 43 | img_affine = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 44 | print("Affine Transformation: Image skewed") 45 | 46 | # %% [9. Perspective Transformation] 47 | pts1 = np.float32([[50,50], [250,50], [50,250], [250,250]]) 48 | pts2 = np.float32([[0,0], [300,0], [50,300], [250,300]]) 49 | matrix = cv2.getPerspectiveTransform(pts1, pts2) 50 | img_perspective = cv2.warpPerspective(img, matrix, (img.shape[1], img.shape[0])) 51 | print("Perspective Transformation: Image warped") 52 | 53 | # %% [10. Visualization] 54 | plt.figure(figsize=(15, 10)) 55 | plt.subplot(2, 4, 1) 56 | plt.title("Original") 57 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 58 | plt.subplot(2, 4, 2) 59 | plt.title("Resized") 60 | plt.imshow(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)) 61 | plt.subplot(2, 4, 3) 62 | plt.title("Cropped") 63 | plt.imshow(cv2.cvtColor(img_cropped, cv2.COLOR_BGR2RGB)) 64 | plt.subplot(2, 4, 4) 65 | plt.title("Rotated") 66 | plt.imshow(cv2.cvtColor(img_rotated, cv2.COLOR_BGR2RGB)) 67 | plt.subplot(2, 4, 5) 68 | plt.title("Flipped") 69 | plt.imshow(cv2.cvtColor(img_flipped, cv2.COLOR_BGR2RGB)) 70 | plt.subplot(2, 4, 6) 71 | plt.title("Translated") 72 | plt.imshow(cv2.cvtColor(img_translated, cv2.COLOR_BGR2RGB)) 73 | plt.subplot(2, 4, 7) 74 | plt.title("Affine") 75 | plt.imshow(cv2.cvtColor(img_affine, cv2.COLOR_BGR2RGB)) 76 | plt.subplot(2, 4, 8) 77 | plt.title("Perspective") 78 | plt.imshow(cv2.cvtColor(img_perspective, cv2.COLOR_BGR2RGB)) 79 | plt.savefig("image_transformations_output.png") 80 | print("Visualization: Transformations saved as image_transformations_output.png") 81 | 82 | # %% [11. Interview Scenario: Image Transformations] 83 | """ 84 | Interview Scenario: Image Transformations 85 | Q: How do you rotate an image in OpenCV, and what’s the role of the rotation matrix? 86 | A: Use cv2.getRotationMatrix2D to create a 2D rotation matrix, then apply it with cv2.warpAffine. 87 | Key: The matrix defines angle, center, and scale for precise rotation. 88 | Example: cv2.getRotationMatrix2D(center, 45, 1.0) 89 | """ 90 | 91 | # Execute the demo 92 | if __name__ == "__main__": 93 | run_image_transformations_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/03 Integration/integration.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Integration] 2 | # Learn OpenCV integration with TensorFlow, ROS, and Raspberry Pi. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import tensorflow as tf 9 | from tensorflow.keras.applications import MobileNetV2 10 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 11 | 12 | def run_integration_demo(): 13 | # %% [2. Create Synthetic Image] 14 | img = np.zeros((300, 300, 3), dtype=np.uint8) 15 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car-like shape 16 | print("Synthetic Image: 300x300 with car-like shape created") 17 | 18 | # %% [3. OpenCV with TensorFlow] 19 | model = MobileNetV2(weights='imagenet') 20 | img_resized = cv2.resize(img, (224, 224)) 21 | img_array = np.expand_dims(img_resized, axis=0) 22 | img_preprocessed = preprocess_input(img_array) 23 | predictions = model.predict(img_array) 24 | decoded_preds = decode_predictions(predictions, top=1)[0] 25 | img_tf = img.copy() 26 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 27 | cv2.putText(img_tf, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 28 | print(f"OpenCV with TensorFlow: Predicted {decoded_preds[0][1]} ({decoded_preds[0][2]:.2f})") 29 | 30 | # %% [4. OpenCV with ROS (Simulated)] 31 | """ 32 | ROS Node Example (Conceptual): 33 | import rospy 34 | from sensor_msgs.msg import Image 35 | from cv_bridge import CvBridge 36 | def image_callback(msg): 37 | bridge = CvBridge() 38 | cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") 39 | edges = cv2.Canny(cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY), 100, 200) 40 | pub.publish(bridge.cv2_to_imgmsg(edges, "mono8")) 41 | rospy.init_node('vision_node') 42 | pub = rospy.Publisher('edges', Image, queue_size=10) 43 | sub = rospy.Subscriber('camera/image', Image, image_callback) 44 | rospy.spin() 45 | """ 46 | img_ros = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 200) 47 | print("OpenCV with ROS: Simulated ROS node for edge detection") 48 | 49 | # %% [5. OpenCV in Embedded Systems (Raspberry Pi, Conceptual)] 50 | """ 51 | Raspberry Pi Setup: 52 | 1. Install Raspbian and Python 3.8+. 53 | 2. Install OpenCV: pip install opencv-python. 54 | 3. Connect camera module. 55 | 4. Run lightweight vision pipeline: 56 | cap = cv2.VideoCapture(0) 57 | while True: 58 | ret, frame = cap.read() 59 | edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200) 60 | cv2.imshow('Edges', edges) 61 | if cv2.waitKey(1) & 0xFF == ord('q'): 62 | break 63 | cap.release() 64 | cv2.destroyAllWindows() 65 | """ 66 | img_pi = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 200) 67 | print("OpenCV in Raspberry Pi: Simulated lightweight edge detection") 68 | 69 | # %% [6. Visualization] 70 | plt.figure(figsize=(15, 5)) 71 | plt.subplot(1, 3, 1) 72 | plt.title("TensorFlow Integration") 73 | plt.imshow(cv2.cvtColor(img_tf, cv2.COLOR_BGR2RGB)) 74 | plt.subplot(1, 3, 2) 75 | plt.title("ROS Integration (Simulated)") 76 | plt.imshow(img_ros, cmap="gray") 77 | plt.subplot(1, 3, 3) 78 | plt.title("Raspberry Pi (Simulated)") 79 | plt.imshow(img_pi, cmap="gray") 80 | plt.savefig("integration_output.png") 81 | print("Visualization: Integration results saved as integration_output.png") 82 | 83 | # %% [7. Interview Scenario: Integration] 84 | """ 85 | Interview Scenario: Integration 86 | Q: How do you integrate OpenCV with TensorFlow for real-time inference? 87 | A: Use OpenCV for image preprocessing (e.g., resize, color conversion) and TensorFlow for model inference (e.g., MobileNetV2). 88 | Key: Ensure compatible data formats (e.g., BGR to RGB, normalized inputs). 89 | Example: cv2.resize(img, (224, 224)); model.predict(img_array) 90 | """ 91 | 92 | # Execute the demo 93 | if __name__ == "__main__": 94 | run_integration_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/02 Video_Analysis/video_analysis.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Video Analysis] 2 | # Learn frame differencing, background subtraction, motion detection, and stabilization with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_video_analysis_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | frames_gray = [cv2.cvtColor(f, cv2.COLOR_BGR2GRAY) for f in frames] 19 | print("Synthetic Video: 10 frames with moving white square created") 20 | 21 | # %% [3. Frame Differencing] 22 | diff = cv2.absdiff(frames_gray[4], frames_gray[5]) 23 | _, diff_thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY) 24 | print("Frame Differencing: Motion detected between frames 5 and 6") 25 | 26 | # %% [4. Background Subtraction (MOG2)] 27 | bg_subtractor_mog2 = cv2.createBackgroundSubtractorMOG2() 28 | mog2_mask = bg_subtractor_mog2.apply(frames[5]) 29 | print("Background Subtraction: MOG2 applied to frame 6") 30 | 31 | # %% [5. Background Subtraction (KNN)] 32 | bg_subtractor_knn = cv2.createBackgroundSubtractorKNN() 33 | knn_mask = bg_subtractor_knn.apply(frames[5]) 34 | print("Background Subtraction: KNN applied to frame 6") 35 | 36 | # %% [6. Motion Detection] 37 | contours, _ = cv2.findContours(mog2_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 38 | img_motion = frames[5].copy() 39 | for contour in contours: 40 | if cv2.contourArea(contour) > 100: 41 | x, y, w, h = cv2.boundingRect(contour) 42 | cv2.rectangle(img_motion, (x, y), (x+w, y+h), (0, 0, 255), 2) 43 | print(f"Motion Detection: {len(contours)} moving objects detected") 44 | 45 | # %% [7. Video Stabilization (Simplified)] 46 | # Simulate stabilization by aligning frame 6 to frame 5 using translation 47 | orb = cv2.ORB_create() 48 | kp1, des1 = orb.detectAndCompute(frames_gray[4], None) 49 | kp2, des2 = orb.detectAndCompute(frames_gray[5], None) 50 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 51 | matches = bf.match(des1, des2) 52 | src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) 53 | dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) 54 | M, _ = cv2.estimateAffinePartial2D(src_pts, dst_pts) 55 | img_stabilized = cv2.warpAffine(frames[5], M, (300, 300)) 56 | print("Video Stabilization: Frame 6 aligned to frame 5") 57 | 58 | # %% [8. Visualization] 59 | plt.figure(figsize=(15, 8)) 60 | plt.subplot(2, 3, 1) 61 | plt.title("Frame 6") 62 | plt.imshow(cv2.cvtColor(frames[5], cv2.COLOR_BGR2RGB)) 63 | plt.subplot(2, 3, 2) 64 | plt.title("Frame Diff") 65 | plt.imshow(diff_thresh, cmap="gray") 66 | plt.subplot(2, 3, 3) 67 | plt.title("MOG2 Mask") 68 | plt.imshow(mog2_mask, cmap="gray") 69 | plt.subplot(2, 3, 4) 70 | plt.title("KNN Mask") 71 | plt.imshow(knn_mask, cmap="gray") 72 | plt.subplot(2, 3, 5) 73 | plt.title("Motion Detection") 74 | plt.imshow(cv2.cvtColor(img_motion, cv2.COLOR_BGR2RGB)) 75 | plt.subplot(2, 3, 6) 76 | plt.title("Stabilized") 77 | plt.imshow(cv2.cvtColor(img_stabilized, cv2.COLOR_BGR2RGB)) 78 | plt.savefig("video_analysis_output.png") 79 | print("Visualization: Analysis results saved as video_analysis_output.png") 80 | 81 | # %% [9. Interview Scenario: Video Analysis] 82 | """ 83 | Interview Scenario: Video Analysis 84 | Q: How does MOG2 background subtraction work? 85 | A: MOG2 models each pixel as a mixture of Gaussians to separate foreground from background. 86 | Key: Robust to lighting changes and dynamic backgrounds. 87 | Example: cv2.createBackgroundSubtractorMOG2() 88 | """ 89 | 90 | # Execute the demo 91 | if __name__ == "__main__": 92 | run_video_analysis_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/02 Deployment/deployment.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Deployment] 2 | # Learn Flask for web apps, Docker containers, and real-time inference with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow flask 5 | # Optional: Install Docker for containerization 6 | import cv2 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | from flask import Flask, request, jsonify 10 | import tensorflow as tf 11 | from tensorflow.keras.applications import MobileNetV2 12 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 13 | import base64 14 | from io import BytesIO 15 | 16 | app = Flask(__name__) 17 | 18 | def run_deployment_demo(): 19 | # %% [2. Load Pretrained Model] 20 | model = MobileNetV2(weights='imagenet') 21 | print("Deployment: Loaded MobileNetV2 for real-time inference") 22 | 23 | # %% [3. Create Synthetic Image] 24 | img = np.zeros((300, 300, 3), dtype=np.uint8) 25 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car-like shape 26 | print("Synthetic Image: 300x300 with car-like shape created") 27 | 28 | # %% [4. Flask App for Real-Time Inference] 29 | @app.route('/predict', methods=['POST']) 30 | def predict(): 31 | data = request.json 32 | img_data = base64.b64decode(data['image']) 33 | img_array = np.frombuffer(img_data, np.uint8) 34 | img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) 35 | img_resized = cv2.resize(img, (224, 224)) 36 | img_array = np.expand_dims(img_resized, axis=0) 37 | img_preprocessed = preprocess_input(img_array) 38 | predictions = model.predict(img_array) 39 | decoded_preds = decode_predictions(predictions, top=1)[0] 40 | return jsonify({'prediction': decoded_preds[0][1], 'confidence': float(decoded_preds[0][2])}) 41 | 42 | # Simulate Flask inference with synthetic image 43 | _, img_encoded = cv2.imencode('.png', img) 44 | img_base64 = base64.b64encode(img_encoded).decode('utf-8') 45 | simulated_request = {'image': img_base64} 46 | with app.test_request_context('/predict', json=simulated_request, method='POST'): 47 | response = predict() 48 | print(f"Flask Inference: Simulated prediction: {response.json['prediction']} ({response.json['confidence']:.2f})") 49 | 50 | # %% [5. Docker Container (Conceptual)] 51 | """ 52 | Dockerfile Example: 53 | FROM python:3.8-slim 54 | WORKDIR /app 55 | COPY requirements.txt . 56 | RUN pip install -r requirements.txt 57 | COPY . . 58 | CMD ["flask", "run", "--host=0.0.0.0"] 59 | 60 | Build and Run: 61 | docker build -t vision-app . 62 | docker run -p 5000:5000 vision-app 63 | """ 64 | print("Docker: Conceptual Dockerfile provided for containerizing Flask app") 65 | 66 | # %% [6. Real-Time Inference] 67 | start_time = time.time() 68 | img_resized = cv2.resize(img, (224, 224)) 69 | img_array = np.expand_dims(img_resized, axis=0) 70 | img_preprocessed = preprocess_input(img_array) 71 | predictions = model.predict(img_array) 72 | inference_time = time.time() - start_time 73 | decoded_preds = decode_predictions(predictions, top=1)[0] 74 | img_pred = img.copy() 75 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 76 | cv2.putText(img_pred, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 77 | print(f"Real-Time Inference: Predicted {decoded_preds[0][1]} in {inference_time:.2f} seconds") 78 | 79 | # %% [7. Visualization] 80 | plt.figure(figsize=(10, 5)) 81 | plt.subplot(1, 2, 1) 82 | plt.title("Original Image") 83 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 84 | plt.subplot(1, 2, 2) 85 | plt.title("Inference Result") 86 | plt.imshow(cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)) 87 | plt.savefig("deployment_output.png") 88 | print("Visualization: Deployment results saved as deployment_output.png") 89 | 90 | # %% [8. Interview Scenario: Deployment] 91 | """ 92 | Interview Scenario: Deployment 93 | Q: What are the benefits of using Docker for deploying vision applications? 94 | A: Docker ensures consistent environments, simplifies dependency management, and enables scalability. 95 | Key: Containers isolate the app, making it portable across platforms (e.g., AWS, GCP). 96 | Example: docker build -t vision-app . 97 | """ 98 | 99 | # Execute the demo 100 | if __name__ == "__main__": 101 | run_deployment_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/01 Feature_Detection/feature_detection.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Feature Detection] 2 | # Learn corner detection, keypoint detection, feature matching, and homography with OpenCV. 3 | 4 | # Setup: pip install opencv-python opencv-contrib-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_feature_detection_demo(): 10 | # %% [2. Create Synthetic Images] 11 | # Generate a 300x300 image with a checkerboard pattern 12 | img1 = np.zeros((300, 300, 3), dtype=np.uint8) 13 | for i in range(0, 300, 50): 14 | for j in range(0, 300, 50): 15 | if (i//50 + j//50) % 2 == 0: 16 | img1[i:i+50, j:j+50] = [255, 255, 255] 17 | # Create a rotated version 18 | center = (img1.shape[1]//2, img1.shape[0]//2) 19 | matrix = cv2.getRotationMatrix2D(center, 10, 1.0) 20 | img2 = cv2.warpAffine(img1, matrix, (img1.shape[1], img1.shape[0])) 21 | img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) 22 | img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 23 | print("Synthetic Images: Checkerboard and rotated version created") 24 | 25 | # %% [3. Corner Detection (Harris)] 26 | harris = cv2.cornerHarris(img1_gray, blockSize=2, ksize=3, k=0.04) 27 | img1_harris = img1.copy() 28 | img1_harris[harris > 0.01 * harris.max()] = [0, 0, 255] 29 | print("Harris Corner Detection: Corners marked in red") 30 | 31 | # %% [4. Corner Detection (Shi-Tomasi)] 32 | corners = cv2.goodFeaturesToTrack(img1_gray, maxCorners=50, qualityLevel=0.01, minDistance=10) 33 | img1_shi = img1.copy() 34 | for corner in corners: 35 | x, y = corner.ravel() 36 | cv2.circle(img1_shi, (int(x), int(y)), 3, (0, 255, 0), -1) 37 | print("Shi-Tomasi Corner Detection: Corners marked in green") 38 | 39 | # %% [5. Keypoint Detection (ORB)] 40 | orb = cv2.ORB_create() 41 | keypoints1, descriptors1 = orb.detectAndCompute(img1_gray, None) 42 | keypoints2, descriptors2 = orb.detectAndCompute(img2_gray, None) 43 | img1_orb = cv2.drawKeypoints(img1, keypoints1, None, color=(0, 255, 0)) 44 | print(f"ORB Keypoints: {len(keypoints1)} keypoints detected in first image") 45 | 46 | # %% [6. Feature Matching (Brute-Force)] 47 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 48 | matches = bf.match(descriptors1, descriptors2) 49 | matches = sorted(matches, key=lambda x: x.distance) 50 | img_matches = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:10], None, flags=2) 51 | print("Brute-Force Matching: Top 10 matches drawn") 52 | 53 | # %% [7. Homography Estimation] 54 | src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) 55 | dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) 56 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) 57 | h, w = img1_gray.shape 58 | pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2) 59 | dst = cv2.perspectiveTransform(pts, M) 60 | img2_homography = img2.copy() 61 | cv2.polylines(img2_homography, [np.int32(dst)], True, (0, 0, 255), 3) 62 | print("Homography Estimation: Transformation outline drawn") 63 | 64 | # %% [8. Visualization] 65 | plt.figure(figsize=(15, 10)) 66 | plt.subplot(2, 3, 1) 67 | plt.title("Original Image 1") 68 | plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) 69 | plt.subplot(2, 3, 2) 70 | plt.title("Harris Corners") 71 | plt.imshow(cv2.cvtColor(img1_harris, cv2.COLOR_BGR2RGB)) 72 | plt.subplot(2, 3, 3) 73 | plt.title("Shi-Tomasi Corners") 74 | plt.imshow(cv2.cvtColor(img1_shi, cv2.COLOR_BGR2RGB)) 75 | plt.subplot(2, 3, 4) 76 | plt.title("ORB Keypoints") 77 | plt.imshow(cv2.cvtColor(img1_orb, cv2.COLOR_BGR2RGB)) 78 | plt.subplot(2, 3, 5) 79 | plt.title("Feature Matches") 80 | plt.imshow(cv2.cvtColor(img_matches, cv2.COLOR_BGR2RGB)) 81 | plt.subplot(2, 3, 6) 82 | plt.title("Homography") 83 | plt.imshow(cv2.cvtColor(img2_homography, cv2.COLOR_BGR2RGB)) 84 | plt.savefig("feature_detection_output.png") 85 | print("Visualization: Feature detection results saved as feature_detection_output.png") 86 | 87 | # %% [9. Interview Scenario: Feature Detection] 88 | """ 89 | Interview Scenario: Feature Detection 90 | Q: What’s the difference between SIFT and ORB for keypoint detection? 91 | A: SIFT is scale-invariant and robust but patented and slower; ORB is fast, open-source, and good for real-time. 92 | Key: ORB uses binary descriptors, suitable for Hamming distance matching. 93 | Example: cv2.ORB_create() 94 | """ 95 | 96 | # Execute the demo 97 | if __name__ == "__main__": 98 | run_feature_detection_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/03 Object_Tracking/object_tracking.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Object Tracking] 2 | # Learn MeanShift, CamShift, KCF, and CSRT trackers with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_object_tracking_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | print("Synthetic Video: 10 frames with moving white square created") 19 | 20 | # %% [3. Initialize ROI] 21 | roi = (100, 100, 50, 50) # (x, y, w, h) for first frame 22 | x, y, w, h = roi 23 | frame_roi = frames[0][y:y+h, x:x+w] 24 | hsv_roi = cv2.cvtColor(frame_roi, cv2.COLOR_BGR2HSV) 25 | roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180]) 26 | cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) 27 | 28 | # %% [4. MeanShift Tracking] 29 | term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) 30 | mean_shift_results = [] 31 | track_window = roi 32 | for frame in frames: 33 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 34 | dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) 35 | ret, track_window = cv2.meanShift(dst, track_window, term_crit) 36 | x, y, w, h = track_window 37 | img_ms = frame.copy() 38 | cv2.rectangle(img_ms, (x, y), (x+w, y+h), (0, 0, 255), 2) 39 | mean_shift_results.append(img_ms) 40 | print("MeanShift Tracking: Tracked square across frames") 41 | 42 | # %% [5. CamShift Tracking] 43 | cam_shift_results = [] 44 | track_window = roi 45 | for frame in frames: 46 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 47 | dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) 48 | ret, track_window = cv2.CamShift(dst, track_window, term_crit) 49 | pts = cv2.boxPoints(ret) 50 | pts = np.int0(pts) 51 | img_cs = frame.copy() 52 | cv2.polylines(img_cs, [pts], True, (0, 255, 0), 2) 53 | cam_shift_results.append(img_cs) 54 | print("CamShift Tracking: Tracked square with adaptive size") 55 | 56 | # %% [6. KCF Tracking] 57 | kcf_tracker = cv2.TrackerKCF_create() 58 | kcf_results = [] 59 | kcf_tracker.init(frames[0], roi) 60 | for frame in frames: 61 | ok, bbox = kcf_tracker.update(frame) 62 | img_kcf = frame.copy() 63 | if ok: 64 | x, y, w, h = [int(v) for v in bbox] 65 | cv2.rectangle(img_kcf, (x, y), (x+w, y+h), (255, 0, 0), 2) 66 | kcf_results.append(img_kcf) 67 | print("KCF Tracking: Tracked square with KCF") 68 | 69 | # %% [7. CSRT Tracking] 70 | csrt_tracker = cv2.TrackerCSRT_create() 71 | csrt_results = [] 72 | csrt_tracker.init(frames[0], roi) 73 | for frame in frames: 74 | ok, bbox = csrt_tracker.update(frame) 75 | img_csrt = frame.copy() 76 | if ok: 77 | x, y, w, h = [int(v) for v in bbox] 78 | cv2.rectangle(img_csrt, (x, y), (x+w, y+h), (0, 255, 255), 2) 79 | csrt_results.append(img_csrt) 80 | print("CSRT Tracking: Tracked square with CSRT") 81 | 82 | # %% [8. Visualization] 83 | plt.figure(figsize=(15, 8)) 84 | for i, idx in enumerate([0, 4, 9]): # Show frames 1, 5, 10 85 | plt.subplot(4, 3, i+1) 86 | plt.title(f"MeanShift Frame {idx+1}") 87 | plt.imshow(cv2.cvtColor(mean_shift_results[idx], cv2.COLOR_BGR2RGB)) 88 | plt.subplot(4, 3, i+4) 89 | plt.title(f"CamShift Frame {idx+1}") 90 | plt.imshow(cv2.cvtColor(cam_shift_results[idx], cv2.COLOR_BGR2RGB)) 91 | plt.subplot(4, 3, i+7) 92 | plt.title(f"KCF Frame {idx+1}") 93 | plt.imshow(cv2.cvtColor(kcf_results[idx], cv2.COLOR_BGR2RGB)) 94 | plt.subplot(4, 3, i+10) 95 | plt.title(f"CSRT Frame {idx+1}") 96 | plt.imshow(cv2.cvtColor(csrt_results[idx], cv2.COLOR_BGR2RGB)) 97 | plt.tight_layout() 98 | plt.savefig("object_tracking_output.png") 99 | print("Visualization: Tracking results saved as object_tracking_output.png") 100 | 101 | # %% [9. Interview Scenario: Object Tracking] 102 | """ 103 | Interview Scenario: Object Tracking 104 | Q: What’s the difference between MeanShift and CSRT trackers? 105 | A: MeanShift tracks based on color histograms, fast but limited to fixed-size windows; CSRT uses discriminative correlation filters, more robust to scale and rotation. 106 | Key: CSRT is slower but more accurate for complex scenes. 107 | Example: cv2.TrackerCSRT_create() 108 | """ 109 | 110 | # Execute the demo 111 | if __name__ == "__main__": 112 | run_object_tracking_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/README.md: -------------------------------------------------------------------------------- 1 | # 🎥 Video Processing with OpenCV (cv2) 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | NumPy 7 | Matplotlib 8 |
9 |

Your guide to mastering video processing with OpenCV for AI/ML and computer vision interviews

10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Video Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`) and **Advanced Image Processing** (e.g., `optical_flow.py`) sections, focusing on handling video data for motion analysis and object tracking. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, and **Advanced Image Processing**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Video Basics**: Read/write videos, extract frames, and access properties (FPS, resolution). 20 | - **Video Analysis**: Perform frame differencing, background subtraction (MOG2, KNN), motion detection, and video stabilization. 21 | - **Object Tracking**: Track objects using MeanShift, CamShift, KCF, and CSRT trackers. 22 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic video frames. 23 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 24 | 25 | ## 🔍 Who Is This For? 26 | 27 | - Computer Vision Engineers advancing video processing skills. 28 | - Machine Learning Engineers exploring motion analysis. 29 | - AI Researchers mastering object tracking. 30 | - Software Engineers deepening computer vision expertise. 31 | - Anyone preparing for video processing interviews in AI/ML or retail. 32 | 33 | ## 🗺️ Learning Roadmap 34 | 35 | This section covers three key areas, each with a dedicated `.py` file: 36 | 37 | ### 📹 Video Basics (`video_basics.py`) 38 | - Reading/Writing Videos 39 | - Frame Extraction 40 | - Video Properties (FPS, Resolution) 41 | 42 | ### 🕒 Video Analysis (`video_analysis.py`) 43 | - Frame Differencing 44 | - Background Subtraction (MOG2, KNN) 45 | - Motion Detection 46 | - Video Stabilization 47 | 48 | ### 🏃 Object Tracking (`object_tracking.py`) 49 | - MeanShift 50 | - CamShift 51 | - KCF Tracker 52 | - CSRT Tracker 53 | 54 | ## 💡 Why Master Video Processing? 55 | 56 | Video processing with OpenCV is essential for computer vision, and here’s why it matters: 57 | 1. **Dynamic Analysis**: Enables motion detection, tracking, and stabilization for real-world applications. 58 | 2. **Versatility**: Applies to retail (e.g., customer tracking), autonomous systems, and surveillance. 59 | 3. **Interview Relevance**: Tested in coding challenges (e.g., background subtraction, object tracking). 60 | 4. **Performance**: Optimized for real-time video with OpenCV’s C++ backend. 61 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 62 | 63 | This section is your roadmap to mastering OpenCV’s video processing techniques for technical interviews—let’s dive in! 64 | 65 | ## 📆 Study Plan 66 | 67 | - **Week 1**: Video Basics 68 | - **Week 2**: Video Analysis 69 | - **Week 3**: Object Tracking 70 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 71 | 72 | ## 🛠️ Setup Instructions 73 | 74 | 1. **Python Environment**: 75 | - Install Python 3.8+ and pip. 76 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 77 | - Install dependencies: `pip install opencv-python numpy matplotlib`. 78 | 2. **Datasets**: 79 | - Uses synthetic video frames (e.g., moving shapes) generated with NumPy/OpenCV. 80 | - Optional: Use real video files or webcam input (commented out; enable locally). 81 | - Download sample videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 82 | 3. **Running Code**: 83 | - Run `.py` files in a Python environment (e.g., `python video_basics.py`). 84 | - Use Google Colab for convenience or local setup. 85 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 86 | - Check terminal for errors; ensure dependencies are installed. 87 | 4. **Webcam** (Optional): 88 | - `object_tracking.py` can use webcam input for real-time tracking (commented out; enable locally). 89 | 90 | ## 🏆 Practical Tasks 91 | 92 | 1. **Video Basics**: 93 | - Extract frames from synthetic video frames and save as images. 94 | - Print FPS and resolution of a synthetic video sequence. 95 | 2. **Video Analysis**: 96 | - Detect motion using frame differencing on synthetic frames. 97 | - Apply MOG2 background subtraction to isolate moving objects. 98 | 3. **Object Tracking**: 99 | - Track a moving square with MeanShift in synthetic frames. 100 | - Use CSRT tracker to follow a synthetic object. 101 | 102 | ## 💡 Interview Tips 103 | 104 | - **Common Questions**: 105 | - How does MOG2 background subtraction work? 106 | - What’s the difference between MeanShift and CSRT trackers? 107 | - How would you stabilize a shaky video? 108 | - **Tips**: 109 | - Explain background subtraction with code (e.g., `cv2.createBackgroundSubtractorMOG2()`). 110 | - Demonstrate tracking steps (e.g., initialize ROI → track with CSRT). 111 | - Be ready to code tasks like frame differencing or MeanShift tracking. 112 | - Discuss trade-offs between trackers (e.g., speed vs. accuracy). 113 | - **Coding Tasks**: 114 | - Implement frame differencing to detect motion. 115 | - Track an object using KCF tracker on synthetic frames. 116 | - Apply KNN background subtraction. 117 | - **Conceptual Clarity**: 118 | - Explain why MOG2 is robust for dynamic backgrounds. 119 | - Describe how CamShift adapts to object size changes. 120 | 121 | ## 📚 Resources 122 | 123 | - [OpenCV Official Documentation](https://docs.opencv.org/) 124 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 125 | - [PyImageSearch: Video Processing with OpenCV](https://www.pyimagesearch.com/category/opencv/) 126 | - [NumPy Documentation](https://numpy.org/doc/) 127 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 128 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 129 | 130 | ## 🤝 Contributions 131 | 132 | Love to collaborate? Here’s how! 🌟 133 | 1. Fork the repository. 134 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 135 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 136 | 4. Push to the branch (`git push origin feature/amazing-addition`). 137 | 5. Open a Pull Request. 138 | 139 | --- 140 | 141 |
142 |

Happy Learning and Good Luck with Your Interviews! ✨

143 |
-------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/README.md: -------------------------------------------------------------------------------- 1 | # 🔍 Advanced Image Processing with OpenCV (cv2) 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | NumPy 7 | Matplotlib 8 |
9 |

Your guide to mastering advanced image processing with OpenCV for AI/ML and computer vision interviews

10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Advanced Image Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** section (e.g., `image_basics.py`), diving into sophisticated techniques like feature detection, image segmentation, and optical flow. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), and **Core Image Processing** (e.g., `image_filtering.py`)—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Feature Detection**: Detect corners (Harris, Shi-Tomasi), keypoints (ORB), match features (FLANN, Brute-Force), and estimate homography. 20 | - **Image Segmentation**: Perform contour detection, watershed algorithm, GrabCut, and K-Means clustering. 21 | - **Optical Flow**: Analyze motion with dense (Farneback) and sparse (Lucas-Kanade) optical flow, and track objects. 22 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic images and video frames. 23 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 24 | 25 | ## 🔍 Who Is This For? 26 | 27 | - Computer Vision Engineers advancing their skills. 28 | - Machine Learning Engineers exploring feature detection and segmentation. 29 | - AI Researchers mastering motion analysis. 30 | - Software Engineers deepening computer vision expertise. 31 | - Anyone preparing for advanced computer vision interviews in AI/ML or retail. 32 | 33 | ## 🗺️ Learning Roadmap 34 | 35 | This section covers three key areas, each with a dedicated `.py` file: 36 | 37 | ### 🕵️ Feature Detection (`feature_detection.py`) 38 | - Corner Detection (Harris, Shi-Tomasi) 39 | - Keypoint Detection (ORB) 40 | - Feature Matching (FLANN, Brute-Force) 41 | - Homography Estimation 42 | 43 | ### 🖲️ Image Segmentation (`image_segmentation.py`) 44 | - Contour Detection 45 | - Watershed Algorithm 46 | - GrabCut 47 | - Clustering (K-Means) 48 | 49 | ### 🛤️ Optical Flow (`optical_flow.py`) 50 | - Dense Optical Flow (Farneback) 51 | - Sparse Optical Flow (Lucas-Kanade) 52 | - Motion Tracking 53 | 54 | ## 💡 Why Master Advanced Image Processing? 55 | 56 | Advanced image processing with OpenCV is critical for computer vision, and here’s why it matters: 57 | 1. **Core Techniques**: Essential for object recognition, scene understanding, and motion analysis. 58 | 2. **Versatility**: Applies to retail (e.g., product recognition), autonomous systems, and augmented reality. 59 | 3. **Interview Relevance**: Tested in coding challenges (e.g., feature matching, segmentation). 60 | 4. **Performance**: Optimized for real-time applications with OpenCV’s C++ backend. 61 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 62 | 63 | This section is your roadmap to mastering OpenCV’s advanced techniques for technical interviews—let’s dive in! 64 | 65 | ## 📆 Study Plan 66 | 67 | - **Week 1**: Feature Detection 68 | - **Week 2**: Image Segmentation 69 | - **Week 3**: Optical Flow 70 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 71 | 72 | ## 🛠️ Setup Instructions 73 | 74 | 1. **Python Environment**: 75 | - Install Python 3.8+ and pip. 76 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 77 | - Install dependencies: `pip install opencv-python opencv-contrib-python numpy matplotlib`. 78 | 2. **Datasets**: 79 | - Uses synthetic images (e.g., shapes, patterns) and video frames generated with NumPy/OpenCV. 80 | - Optional: Download sample images/videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 81 | 3. **Running Code**: 82 | - Run `.py` files in a Python environment (e.g., `python feature_detection.py`). 83 | - Use Google Colab for convenience or local setup. 84 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 85 | - Check terminal for errors; ensure dependencies are installed. 86 | 4. **Webcam** (Optional): 87 | - `optical_flow.py` can use webcam input for real-time motion tracking (commented out; enable locally). 88 | 89 | ## 🏆 Practical Tasks 90 | 91 | 1. **Feature Detection**: 92 | - Detect Harris corners on a synthetic checkerboard pattern. 93 | - Match ORB keypoints between two synthetic images. 94 | 2. **Image Segmentation**: 95 | - Detect contours in a synthetic shape image. 96 | - Segment an object using GrabCut on a toy image. 97 | 3. **Optical Flow**: 98 | - Compute dense optical flow on synthetic video frames. 99 | - Track keypoints with Lucas-Kanade in a simulated motion sequence. 100 | 101 | ## 💡 Interview Tips 102 | 103 | - **Common Questions**: 104 | - What’s the difference between SIFT and ORB for keypoint detection? 105 | - How does the watershed algorithm work for segmentation? 106 | - What are the applications of optical flow in computer vision? 107 | - **Tips**: 108 | - Explain feature detection with code (e.g., `cv2.ORB_create()`). 109 | - Demonstrate segmentation steps (e.g., contour detection → watershed). 110 | - Be ready to code tasks like keypoint matching or motion tracking. 111 | - Discuss trade-offs between accuracy and speed (e.g., FLANN vs. Brute-Force). 112 | - **Coding Tasks**: 113 | - Implement Shi-Tomasi corner detection on a synthetic image. 114 | - Segment an object using K-Means clustering. 115 | - Compute sparse optical flow with Lucas-Kanade. 116 | - **Conceptual Clarity**: 117 | - Explain homography estimation for image alignment. 118 | - Describe how GrabCut uses graph cuts for segmentation. 119 | 120 | ## 📚 Resources 121 | 122 | - [OpenCV Official Documentation](https://docs.opencv.org/) 123 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 124 | - [PyImageSearch: Feature Detection and Segmentation](https://www.pyimagesearch.com/category/opencv/) 125 | - [NumPy Documentation](https://numpy.org/doc/) 126 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 127 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 128 | 129 | ## 🤝 Contributions 130 | 131 | Love to collaborate? Here’s how! 🌟 132 | 1. Fork the repository. 133 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 134 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 135 | 4. Push to the branch (`git push origin feature/amazing-addition`). 136 | 5. Open a Pull Request. 137 | 138 | --- 139 | 140 |
141 |

Happy Learning and Good Luck with Your Interviews! ✨

142 |
-------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/README.md: -------------------------------------------------------------------------------- 1 | # 🖌️ Core Image Processing with OpenCV (cv2) 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | NumPy 7 | Matplotlib 8 |
9 |

Your guide to mastering core image processing with OpenCV for AI/ML and computer vision interviews

10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Core Image Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder is your starting point for mastering the foundational techniques of computer vision using OpenCV, covering image basics, transformations, filtering, and color processing. Designed for hands-on learning and interview success, it builds on your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), and **NumPy** (e.g., `array_creation_properties.py`)—and aligns with your retail-themed projects (April 26, 2025). Whether you’re preparing for coding challenges or technical discussions, this section equips you with the skills to shine in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Image Basics**: Read/write images, manipulate pixels, and explore color spaces (RGB, HSV, Grayscale). 20 | - **Image Transformations**: Resize, crop, rotate, flip, and apply affine/perspective transformations. 21 | - **Image Filtering**: Apply blurring, sharpening, edge detection, thresholding, and morphological operations. 22 | - **Color Processing**: Convert color spaces, filter colors, equalize histograms, and quantize colors. 23 | - **Hands-on Code**: Four `.py` files with practical examples using synthetic images. 24 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - Computer Vision Engineers building foundational skills. 29 | - Machine Learning Engineers exploring image processing. 30 | - AI Researchers mastering OpenCV basics. 31 | - Software Engineers transitioning to vision roles. 32 | - Anyone preparing for computer vision interviews in AI/ML or retail. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This section covers four key areas, each with a dedicated `.py` file: 37 | 38 | ### 📷 Image Basics (`image_basics.py`) 39 | - Reading/Writing Images 40 | - Color Spaces (RGB, HSV, Grayscale) 41 | - Pixel Manipulation 42 | - Image Properties (Shape, Size, Channels) 43 | 44 | ### 🖼️ Image Transformations (`image_transformations.py`) 45 | - Resizing 46 | - Cropping 47 | - Rotation 48 | - Flipping 49 | - Translation 50 | - Affine Transformations 51 | - Perspective Transformations 52 | 53 | ### 🎨 Image Filtering (`image_filtering.py`) 54 | - Blurring (Gaussian, Median, Bilateral) 55 | - Sharpening 56 | - Edge Detection (Sobel, Canny) 57 | - Thresholding (Binary, Adaptive) 58 | - Morphological Operations (Dilation, Erosion) 59 | 60 | ### 🌈 Color Processing (`color_processing.py`) 61 | - Color Space Conversion 62 | - Color Filtering 63 | - Histogram Equalization 64 | - Color Quantization 65 | 66 | ## 💡 Why Master Core Image Processing? 67 | 68 | Core image processing with OpenCV is the foundation of computer vision, and here’s why it matters: 69 | 1. **Fundamental Skills**: Essential for preprocessing images in AI/ML pipelines. 70 | 2. **Versatility**: Applies to retail, autonomous systems, medical imaging, and more. 71 | 3. **Interview Relevance**: Tested in coding challenges (e.g., edge detection, color filtering). 72 | 4. **Performance**: Optimized for real-time applications with OpenCV’s C++ backend. 73 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 74 | 75 | This section is your roadmap to mastering OpenCV’s core techniques for technical interviews—let’s dive in! 76 | 77 | ## 📆 Study Plan 78 | 79 | - **Week 1**: Image Basics and Transformations 80 | - **Week 2**: Image Filtering and Color Processing 81 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 82 | 83 | ## 🛠️ Setup Instructions 84 | 85 | 1. **Python Environment**: 86 | - Install Python 3.8+ and pip. 87 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 88 | - Install dependencies: `pip install opencv-python numpy matplotlib`. 89 | 2. **Datasets**: 90 | - Uses synthetic images generated with NumPy/OpenCV (e.g., rectangles, gradients). 91 | - Optional: Download sample images from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 92 | 3. **Running Code**: 93 | - Run `.py` files in a Python environment (e.g., `python image_basics.py`). 94 | - Use Google Colab for convenience or local setup. 95 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 96 | - Check terminal for errors; ensure dependencies are installed. 97 | 98 | ## 🏆 Practical Tasks 99 | 100 | 1. **Image Basics**: 101 | - Load a synthetic image and convert it to Grayscale and HSV. 102 | - Modify pixel values to create a red square. 103 | 2. **Image Transformations**: 104 | - Resize a synthetic image to half its size and rotate it by 45 degrees. 105 | - Apply an affine transformation to skew the image. 106 | 3. **Image Filtering**: 107 | - Apply Gaussian blur and Canny edge detection to a synthetic pattern. 108 | - Perform binary thresholding on a grayscale image. 109 | 4. **Color Processing**: 110 | - Filter the blue channel from a synthetic RGB image. 111 | - Apply histogram equalization to enhance contrast. 112 | 113 | ## 💡 Interview Tips 114 | 115 | - **Common Questions**: 116 | - What’s the difference between RGB and HSV color spaces? 117 | - How does Canny edge detection work, and what are its parameters? 118 | - Why use morphological operations in image processing? 119 | - **Tips**: 120 | - Explain color space conversions with code (e.g., `cv2.cvtColor(img, cv2.COLOR_BGR2HSV)`). 121 | - Demonstrate edge detection with clear steps (e.g., blur → Canny). 122 | - Be ready to code tasks like resizing or thresholding. 123 | - Discuss trade-offs between filter types (e.g., Gaussian vs. Bilateral). 124 | - **Coding Tasks**: 125 | - Convert an RGB image to Grayscale and display it. 126 | - Implement Sobel edge detection on a synthetic image. 127 | - Apply dilation to a binary image. 128 | - **Conceptual Clarity**: 129 | - Explain why HSV is useful for color filtering. 130 | - Describe how adaptive thresholding differs from binary thresholding. 131 | 132 | ## 📚 Resources 133 | 134 | - [OpenCV Official Documentation](https://docs.opencv.org/) 135 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 136 | - [PyImageSearch: Image Processing with OpenCV](https://www.pyimagesearch.com/category/opencv/) 137 | - [NumPy Documentation](https://numpy.org/doc/) 138 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 139 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 140 | 141 | ## 🤝 Contributions 142 | 143 | Love to collaborate? Here’s how! 🌟 144 | 1. Fork the repository. 145 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 146 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 147 | 4. Push to the branch (`git push origin feature/amazing-addition`). 148 | 5. Open a Pull Request. 149 | 150 | --- 151 | 152 |
153 |

Happy Learning and Good Luck with Your Interviews! ✨

154 |
-------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/README.md: -------------------------------------------------------------------------------- 1 | # 🛠️ Optimization and Deployment with OpenCV (cv2) 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | TensorFlow 7 | Flask 8 | Docker 9 |
10 |

Your guide to optimizing and deploying OpenCV applications for AI/ML and computer vision interviews

11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Optimization and Deployment** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`), **Advanced Image Processing** (e.g., `feature_detection.py`), **Video Processing** (e.g., `video_basics.py`), and **Object Detection and Recognition** (e.g., `deep_learning_detection.py`) sections, focusing on optimizing performance and deploying computer vision applications. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, **Advanced Image Processing**, **Video Processing**, and **Object Detection and Recognition**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Performance Optimization**: Optimize OpenCV with parallel processing, GPU acceleration, memory management, and algorithm efficiency. 21 | - **Deployment**: Deploy vision apps using Flask, Docker, and cloud platforms (AWS, GCP), with real-time inference. 22 | - **Integration**: Integrate OpenCV with TensorFlow, ROS, and embedded systems (Raspberry Pi). 23 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic images and lightweight models. 24 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - Computer Vision Engineers optimizing and deploying vision systems. 29 | - Machine Learning Engineers integrating deep learning with OpenCV. 30 | - AI Researchers mastering real-time vision applications. 31 | - Software Engineers deploying vision solutions in production. 32 | - Anyone preparing for optimization and deployment interviews in AI/ML or retail. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This section covers three key areas, each with a dedicated `.py` file: 37 | 38 | ### ⚡ Performance Optimization (`performance_optimization.py`) 39 | - Parallel Processing 40 | - GPU Acceleration (CUDA, Simulated) 41 | - Memory Management 42 | - Algorithm Efficiency 43 | 44 | ### 📦 Deployment (`deployment.py`) 45 | - Flask for Web Apps 46 | - Docker Containers (Conceptual) 47 | - Cloud Deployment (AWS, GCP, Conceptual) 48 | - Real-Time Inference 49 | 50 | ### 🔗 Integration (`integration.py`) 51 | - OpenCV with TensorFlow/PyTorch 52 | - OpenCV with ROS (Simulated) 53 | - OpenCV in Embedded Systems (Raspberry Pi, Conceptual) 54 | 55 | ## 💡 Why Master Optimization and Deployment? 56 | 57 | Optimization and deployment with OpenCV are critical for computer vision, and here’s why they matter: 58 | 1. **Performance**: Enables real-time vision applications with low latency. 59 | 2. **Scalability**: Supports production-ready deployment for retail (e.g., inventory tracking) and robotics. 60 | 3. **Interview Relevance**: Tested in system design and coding challenges (e.g., optimize OpenCV pipeline, deploy Flask app). 61 | 4. **Integration**: Combines OpenCV with modern ML frameworks and embedded systems. 62 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 63 | 64 | This section is your roadmap to mastering OpenCV optimization and deployment for technical interviews—let’s dive in! 65 | 66 | ## 📆 Study Plan 67 | 68 | - **Week 1**: Performance Optimization 69 | - **Week 2**: Deployment 70 | - **Week 3**: Integration 71 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 72 | 73 | ## 🛠️ Setup Instructions 74 | 75 | 1. **Python Environment**: 76 | - Install Python 3.8+ and pip. 77 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 78 | - Install dependencies: `pip install opencv-python numpy matplotlib tensorflow flask multiprocess`. 79 | 2. **Docker** (Optional for `deployment.py`): 80 | - Install Docker: [Docker Installation](https://docs.docker.com/get-docker/). 81 | - Use provided `Dockerfile` instructions in `deployment.py`. 82 | 3. **CUDA** (Optional for `performance_optimization.py`): 83 | - Requires NVIDIA GPU and OpenCV compiled with CUDA (not standard `opencv-python`). 84 | - Simulated in code; follow comments for local setup. 85 | 4. **ROS and Raspberry Pi** (Optional for `integration.py`): 86 | - Simulated; requires ROS Noetic and Raspberry Pi hardware for full setup. 87 | - Follow comments for conceptual steps. 88 | 5. **Datasets**: 89 | - Uses synthetic images (e.g., shapes) generated with NumPy/OpenCV. 90 | - MobileNetV2 uses pretrained weights from TensorFlow. 91 | 6. **Running Code**: 92 | - Run `.py` files in a Python environment (e.g., `python performance_optimization.py`). 93 | - For `deployment.py`, run Flask app locally (`flask run`) and access `http://127.0.0.1:5000`. 94 | - Use Google Colab for optimization and integration demos; local setup for Flask/Docker. 95 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 96 | - Check terminal for errors; ensure dependencies are installed. 97 | 98 | ## 🏆 Practical Tasks 99 | 100 | 1. **Performance Optimization**: 101 | - Parallelize image processing on synthetic images. 102 | - Optimize memory usage in an OpenCV pipeline. 103 | 2. **Deployment**: 104 | - Deploy a Flask app for real-time object detection. 105 | - Create a Docker container for a vision app (conceptual). 106 | 3. **Integration**: 107 | - Integrate OpenCV with TensorFlow for classification. 108 | - Simulate ROS node for vision processing. 109 | 110 | ## 💡 Interview Tips 111 | 112 | - **Common Questions**: 113 | - How do you optimize OpenCV for real-time performance? 114 | - What are the benefits of Docker for deploying vision apps? 115 | - How do you integrate OpenCV with TensorFlow for inference? 116 | - **Tips**: 117 | - Explain parallel processing with code (e.g., `multiprocess.Pool`). 118 | - Demonstrate Flask app setup (e.g., `Flask(__name__)`). 119 | - Be ready to code tasks like optimizing a detection pipeline or deploying a web app. 120 | - Discuss trade-offs (e.g., CPU vs. GPU, Flask vs. FastAPI). 121 | - **Coding Tasks**: 122 | - Parallelize face detection on multiple images. 123 | - Deploy a Flask app for image classification. 124 | - Integrate OpenCV with TensorFlow for object detection. 125 | - **Conceptual Clarity**: 126 | - Explain CUDA’s role in accelerating OpenCV. 127 | - Describe Docker’s containerization for vision apps. 128 | 129 | ## 📚 Resources 130 | 131 | - [OpenCV Official Documentation](https://docs.opencv.org/) 132 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 133 | - [PyImageSearch: OpenCV Optimization](https://www.pyimagesearch.com/category/opencv/) 134 | - [TensorFlow Documentation](https://www.tensorflow.org/api_docs) 135 | - [Flask Documentation](https://flask.palletsprojects.com/) 136 | - [Docker Documentation](https://docs.docker.com/) 137 | - [ROS Documentation](http://wiki.ros.org/) 138 | - [NumPy Documentation](https://numpy.org/doc/) 139 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 140 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 141 | 142 | ## 🤝 Contributions 143 | 144 | Love to collaborate? Here’s how! 🌟 145 | 1. Fork the repository. 146 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 147 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 148 | 4. Push to the branch (`git push origin feature/amazing-addition`). 149 | 5. Open a Pull Request. 150 | 151 | --- 152 | 153 |
154 |

Happy Learning and Good Luck with Your Interviews! ✨

155 |
-------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/README.md: -------------------------------------------------------------------------------- 1 | # 🕵️‍♂️ Object Detection and Recognition with OpenCV (cv2) 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | TensorFlow 7 | NumPy 8 | Matplotlib 9 |
10 |

Your guide to mastering object detection and recognition with OpenCV and deep learning for AI/ML interviews

11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Object Detection and Recognition** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`), **Advanced Image Processing** (e.g., `feature_detection.py`), and **Video Processing** (e.g., `video_basics.py`) sections, focusing on detecting and classifying objects in images. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, **Advanced Image Processing**, and **Video Processing**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Template Matching**: Detect single and multiple objects using template matching. 21 | - **Haar Cascades**: Perform face and eye detection with pretrained cascades. 22 | - **Deep Learning-Based Detection**: Use YOLOv3 for object detection. 23 | - **Image Classification**: Classify images with pretrained MobileNetV2 and transfer learning. 24 | - **Hands-on Code**: Four `.py` files with practical examples using synthetic images and CIFAR-10. 25 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - Computer Vision Engineers advancing detection and recognition skills. 30 | - Machine Learning Engineers exploring deep learning-based detection. 31 | - AI Researchers mastering object classification. 32 | - Software Engineers deepening computer vision expertise. 33 | - Anyone preparing for object detection interviews in AI/ML or retail. 34 | 35 | ## 🗺️ Learning Roadmap 36 | 37 | This section covers four key areas, each with a dedicated `.py` file: 38 | 39 | ### 🔲 Template Matching (`template_matching.py`) 40 | - Single Object Matching 41 | - Multi-Object Matching 42 | 43 | ### 🧠 Haar Cascades (`haar_cascades.py`) 44 | - Face Detection 45 | - Eye Detection 46 | - Custom Cascade Training (Overview) 47 | 48 | ### 🤖 Deep Learning-Based Detection (`deep_learning_detection.py`) 49 | - YOLO (You Only Look Once) 50 | - SSD and Faster R-CNN (Discussed in Interview Scenario) 51 | - Model Integration (TensorFlow) 52 | 53 | ### 🏷️ Image Classification (`image_classification.py`) 54 | - Pretrained Models (MobileNetV2) 55 | - Transfer Learning 56 | - Custom Classification 57 | 58 | ## 💡 Why Master Object Detection and Recognition? 59 | 60 | Object detection and recognition with OpenCV and deep learning are critical for computer vision, and here’s why they matter: 61 | 1. **Core Techniques**: Essential for identifying and classifying objects in images. 62 | 2. **Versatility**: Applies to retail (e.g., product detection), autonomous systems, and surveillance. 63 | 3. **Interview Relevance**: Tested in coding challenges (e.g., face detection, YOLO implementation). 64 | 4. **Performance**: Combines OpenCV’s efficiency with deep learning’s accuracy. 65 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 66 | 67 | This section is your roadmap to mastering OpenCV and deep learning for object detection in technical interviews—let’s dive in! 68 | 69 | ## 📆 Study Plan 70 | 71 | - **Week 1**: Template Matching and Haar Cascades 72 | - **Week 2**: Deep Learning-Based Detection 73 | - **Week 3**: Image Classification 74 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 75 | 76 | ## 🛠️ Setup Instructions 77 | 78 | 1. **Python Environment**: 79 | - Install Python 3.8+ and pip. 80 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 81 | - Install dependencies: `pip install opencv-python numpy matplotlib tensorflow`. 82 | 2. **Datasets**: 83 | - Uses synthetic images (e.g., shapes, faces) generated with NumPy/OpenCV. 84 | - CIFAR-10 is used for classification (loaded via TensorFlow). 85 | - Optional: Download sample images from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 86 | 3. **YOLOv3 Weights**: 87 | - Download YOLOv3 weights and config: 88 | - `yolov3.weights`: [YOLOv3 Weights](https://pjreddie.com/media/files/yolov3.weights) 89 | - `yolov3.cfg`: [YOLOv3 Config](https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg) 90 | - `coco.names`: [COCO Names](https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names) 91 | - Place in the working directory for `deep_learning_detection.py`. 92 | 4. **Haar Cascades**: 93 | - Uses OpenCV’s pretrained XML files (included with `opencv-python`). 94 | 5. **Running Code**: 95 | - Run `.py` files in a Python environment (e.g., `python template_matching.py`). 96 | - Use Google Colab for convenience or local setup. 97 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 98 | - Check terminal for errors; ensure dependencies and YOLOv3 files are available. 99 | 100 | ## 🏆 Practical Tasks 101 | 102 | 1. **Template Matching**: 103 | - Detect a synthetic shape in an image using single template matching. 104 | - Find multiple instances of a template in a synthetic scene. 105 | 2. **Haar Cascades**: 106 | - Detect faces and eyes in a synthetic face image. 107 | - Explore custom cascade training steps (conceptual). 108 | 3. **Deep Learning-Based Detection**: 109 | - Run YOLOv3 on a synthetic image to detect objects. 110 | - Experiment with confidence thresholds in YOLOv3. 111 | 4. **Image Classification**: 112 | - Classify CIFAR-10 images using MobileNetV2. 113 | - Fine-tune MobileNetV2 for a custom retail dataset (simulated). 114 | 115 | ## 💡 Interview Tips 116 | 117 | - **Common Questions**: 118 | - How does template matching handle scale and rotation variations? 119 | - What are the limitations of Haar cascades for face detection? 120 | - Compare YOLO, SSD, and Faster R-CNN for object detection. 121 | - How does transfer learning improve image classification? 122 | - **Tips**: 123 | - Explain template matching with code (e.g., `cv2.matchTemplate()`). 124 | - Demonstrate YOLOv3 detection steps (e.g., load model → detect objects). 125 | - Be ready to code tasks like face detection or image classification. 126 | - Discuss trade-offs between detection methods (e.g., speed vs. accuracy). 127 | - **Coding Tasks**: 128 | - Implement multi-object template matching. 129 | - Detect faces using Haar cascades on a synthetic image. 130 | - Classify images with a pretrained MobileNetV2. 131 | - **Conceptual Clarity**: 132 | - Explain why YOLO is faster than Faster R-CNN. 133 | - Describe how Haar cascades use integral images for efficiency. 134 | 135 | ## 📚 Resources 136 | 137 | - [OpenCV Official Documentation](https://docs.opencv.org/) 138 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 139 | - [PyImageSearch: Object Detection](https://www.pyimagesearch.com/category/object-detection/) 140 | - [TensorFlow Documentation](https://www.tensorflow.org/api_docs) 141 | - [YOLO Official Site](https://pjreddie.com/darknet/yolo/) 142 | - [NumPy Documentation](https://numpy.org/doc/) 143 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 144 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 145 | 146 | ## 🤝 Contributions 147 | 148 | Love to collaborate? Here’s how! 🌟 149 | 1. Fork the repository. 150 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 151 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 152 | 4. Push to the branch (`git push origin feature/amazing-addition`). 153 | 5. Open a Pull Request. 154 | 155 | --- 156 | 157 |
158 |

Happy Learning and Good Luck with Your Interviews! ✨

159 |
-------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🖼️ Computer Vision with OpenCV (cv2) Interview Preparation 2 | 3 |
4 | Python Logo 5 | OpenCV 6 | NumPy 7 | Matplotlib 8 | TensorFlow 9 | PyTorch 10 |
11 |

Your comprehensive guide to mastering OpenCV (cv2) for AI/ML and computer vision interviews

12 | 13 | --- 14 | 15 | ## 📖 Introduction 16 | 17 | Welcome to my Computer Vision prep with OpenCV (`cv2`) for AI/ML interviews! 🚀 This repository is your essential guide for mastering OpenCV, the leading library for computer vision, with hands-on coding and interview-focused practice. From core image processing to advanced object detection and deep learning integration, it’s crafted to help you excel in technical interviews and computer vision projects with clarity and confidence. Building on your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), and **NumPy** (e.g., `array_creation_properties.py`)—this section prepares you for roles in AI/ML, retail (aligned with April 26, 2025 projects), and computer vision. 18 | 19 | ## 🌟 What’s Inside? 20 | 21 | - **Core OpenCV Mastery**: Dive into image processing, filtering, and transformations. 22 | - **Advanced Techniques**: Explore feature detection, object tracking, and deep learning integration. 23 | - **Hands-on Practice**: Solve curated coding problems with detailed solutions. 24 | - **Interview Question Bank**: Tackle common computer vision questions with clear answers. 25 | - **Performance Optimization**: Learn tips for efficient, interview-ready OpenCV code. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - Computer Vision Engineers prepping for technical interviews. 30 | - Machine Learning Engineers strengthening vision skills. 31 | - AI Researchers enhancing image processing expertise. 32 | - Software Engineers transitioning to computer vision roles. 33 | - Anyone mastering OpenCV for AI/ML and retail applications. 34 | 35 | ## 🗺️ Comprehensive Learning Roadmap 36 | 37 | --- 38 | 39 | ### 🖌️ Core Image Processing 40 | 41 | #### 📷 Image Basics 42 | - Reading/Writing Images 43 | - Color Spaces (RGB, HSV, Grayscale) 44 | - Pixel Manipulation 45 | - Image Properties (Shape, Size, Channels) 46 | 47 | #### 🖼️ Image Transformations 48 | - Resizing 49 | - Cropping 50 | - Rotation 51 | - Flipping 52 | - Translation 53 | - Affine Transformations 54 | - Perspective Transformations 55 | 56 | #### 🎨 Image Filtering 57 | - Blurring (Gaussian, Median, Bilateral) 58 | - Sharpening 59 | - Edge Detection (Sobel, Canny) 60 | - Thresholding (Binary, Adaptive) 61 | - Morphological Operations (Dilation, Erosion) 62 | 63 | #### 🌈 Color Processing 64 | - Color Space Conversion 65 | - Color Filtering 66 | - Histogram Equalization 67 | - Color Quantization 68 | 69 | --- 70 | 71 | ### 🔍 Advanced Image Processing 72 | 73 | #### 🕵️ Feature Detection 74 | - Corner Detection (Harris, Shi-Tomasi) 75 | - Keypoint Detection (SIFT, SURF, ORB) 76 | - Feature Matching (FLANN, Brute-Force) 77 | - Homography Estimation 78 | 79 | #### 🖲️ Image Segmentation 80 | - Contour Detection 81 | - Watershed Algorithm 82 | - GrabCut 83 | - Clustering (K-Means) 84 | 85 | #### 🛤️ Optical Flow 86 | - Dense Optical Flow (Farneback) 87 | - Sparse Optical Flow (Lucas-Kanade) 88 | - Motion Tracking 89 | 90 | --- 91 | 92 | ### 🎥 Video Processing 93 | 94 | #### 📹 Video Basics 95 | - Reading/Writing Videos 96 | - Frame Extraction 97 | - Video Properties (FPS, Resolution) 98 | 99 | #### 🕒 Video Analysis 100 | - Frame Differencing 101 | - Background Subtraction (MOG2, KNN) 102 | - Motion Detection 103 | - Video Stabilization 104 | 105 | #### 🏃 Object Tracking 106 | - MeanShift 107 | - CamShift 108 | - KCF Tracker 109 | - CSRT Tracker 110 | 111 | --- 112 | 113 | ### 🕵️‍♂️ Object Detection and Recognition 114 | 115 | #### 🔲 Template Matching 116 | - Single Object Matching 117 | - Multi-Object Matching 118 | 119 | #### 🧠 Haar Cascades 120 | - Face Detection 121 | - Eye Detection 122 | - Custom Cascade Training 123 | 124 | #### 🤖 Deep Learning-Based Detection 125 | - YOLO (You Only Look Once) 126 | - SSD (Single Shot MultiBox Detector) 127 | - Faster R-CNN 128 | - Model Integration (TensorFlow, PyTorch) 129 | 130 | #### 🏷️ Image Classification 131 | - Pretrained Models (ResNet, VGG, MobileNet) 132 | - Transfer Learning 133 | - Custom Classification 134 | 135 | --- 136 | 137 | ### 🛠️ Optimization and Deployment 138 | 139 | #### ⚡ Performance Optimization 140 | - Parallel Processing 141 | - GPU Acceleration (CUDA) 142 | - Memory Management 143 | - Algorithm Efficiency 144 | 145 | #### 📦 Deployment 146 | - Flask for Web Apps 147 | - Docker Containers 148 | - Cloud Deployment (AWS, GCP) 149 | - Real-Time Inference 150 | 151 | #### 🔗 Integration 152 | - OpenCV with TensorFlow/PyTorch 153 | - OpenCV with ROS (Robot Operating System) 154 | - OpenCV in Embedded Systems (Raspberry Pi) 155 | 156 | --- 157 | 158 | ## 💡 Why Master OpenCV for Computer Vision? 159 | 160 | OpenCV (`cv2`) is the cornerstone of computer vision, and here’s why: 161 | 1. **Versatility**: Supports the full vision pipeline—from preprocessing to deployment. 162 | 2. **Rich Functionality**: Packed with tools for image processing, detection, and tracking. 163 | 3. **Performance**: Optimized for real-time applications with C++ backend. 164 | 4. **Industry Demand**: A must-have skill for 6 LPA+ computer vision roles. 165 | 5. **Community Support**: Tap into a vast network of experts and resources. 166 | 167 | This repo is my roadmap to mastering OpenCV for technical interviews and computer vision careers—let’s build that skill set together! 168 | 169 | ## 📆 Study Plan 170 | 171 | - **Week 1-2**: Core Image Processing 172 | - **Week 3-4**: Advanced Image Processing and Feature Detection 173 | - **Week 5-6**: Video Processing and Object Tracking 174 | - **Week 7-8**: Object Detection and Deep Learning Integration 175 | - **Week 9-10**: Practical Applications and Deployment 176 | 177 | ## 🤝 Contributions 178 | 179 | Love to collaborate? Here’s how! 🌟 180 | 1. Fork the repository. 181 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 182 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 183 | 4. Push to the branch (`git push origin feature/amazing-addition`). 184 | 5. Open a Pull Request. 185 | 186 | ## 📚 Resources 187 | 188 | - [OpenCV Official Documentation](https://docs.opencv.org/) 189 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 190 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 191 | - [“Deep Learning for Computer Vision with Python” by Adrian Rosebrock](https://www.pyimagesearch.com/) 192 | - [PyImageSearch Blog](https://www.pyimagesearch.com/) 193 | - [NumPy Documentation](https://numpy.org/doc/) 194 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 195 | - [TensorFlow Documentation](https://www.tensorflow.org/) 196 | - [PyTorch Documentation](https://pytorch.org/docs/stable/index.html) 197 | 198 | ## 🛠️ Setup Instructions 199 | 200 | 1. **Python Environment**: 201 | - Install Python 3.8+ and pip. 202 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 203 | - Install dependencies: `pip install opencv-python opencv-contrib-python numpy matplotlib tensorflow torch`. 204 | 2. **Datasets**: 205 | - Use synthetic images (e.g., generated patterns) or public datasets (e.g., MNIST, COCO-like). 206 | - Download sample images/videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 207 | 3. **Running Code**: 208 | - Run `.py` files in a Python environment (e.g., `python image_basics.py`). 209 | - Use Google Colab for GPU access if needed (e.g., deep learning models). 210 | - Check terminal for logs and errors; view images with Matplotlib or OpenCV windows. 211 | 4. **Web Deployment** (Optional): 212 | - Use Flask for web-based demos (e.g., `pip install flask`). 213 | - Deploy with Docker for production (e.g., `docker build -t cv-app .`). 214 | 5. **Hardware** (Optional): 215 | - Use a webcam for video processing demos. 216 | - Test on Raspberry Pi for embedded applications. 217 | 218 | ## 🏆 Practical Tasks 219 | 220 | 1. **Core Image Processing**: 221 | - Apply Gaussian blur and Canny edge detection to a synthetic image. 222 | - Convert an RGB image to HSV and filter a specific color. 223 | 2. **Advanced Image Processing**: 224 | - Detect keypoints with ORB and match features across two images. 225 | - Segment an object using GrabCut on a toy image. 226 | 3. **Video Processing**: 227 | - Extract frames from a sample video and detect motion. 228 | - Track an object with CSRT tracker in a webcam feed. 229 | 4. **Object Detection**: 230 | - Implement face detection with Haar cascades. 231 | - Use YOLO for real-time object detection on a toy dataset. 232 | 5. **Practical Applications**: 233 | - Build a retail product recognition system with template matching. 234 | - Detect lanes in a synthetic driving video. 235 | 6. **Optimization and Deployment**: 236 | - Optimize an edge detection pipeline for real-time performance. 237 | - Deploy a Flask app for webcam-based face detection. 238 | 239 | ## 💡 Interview Tips 240 | 241 | - **Common Questions**: 242 | - How does Canny edge detection work, and what are its parameters? 243 | - What’s the difference between Haar cascades and deep learning-based detection? 244 | - How would you optimize OpenCV for real-time video processing? 245 | - **Tips**: 246 | - Explain image processing pipelines with code snippets (e.g., Gaussian blur → Canny). 247 | - Demonstrate deep learning integration (e.g., YOLO with OpenCV). 248 | - Be ready to code tasks like contour detection or object tracking. 249 | - Discuss trade-offs between accuracy and speed in vision systems. 250 | - **Coding Tasks**: 251 | - Implement Sobel edge detection on a synthetic image. 252 | - Build a face detection system with Haar cascades. 253 | - Track an object in a video using KCF tracker. 254 | - **System Design**: 255 | - Design a real-time object detection pipeline with YOLO and Flask. 256 | - Explain GPU acceleration with OpenCV’s CUDA module. 257 | 258 | --- 259 | 260 |
261 |

Happy Learning and Good Luck with Your Interviews! ✨

262 |
-------------------------------------------------------------------------------- /Computer Vision Interview Questions/README.md: -------------------------------------------------------------------------------- 1 | # OpenCV (cv2) Interview Questions for AI/ML Roles 2 | 3 | ## Core OpenCV Foundations 4 | 5 | ### Image Basics 6 | 7 | #### Basic 8 | 1. **What is an image in OpenCV, and how is it represented? Provide an example of loading an image.** 9 | An image in OpenCV is a NumPy array where each pixel is represented by intensity values (grayscale) or RGB/BGR channels (color). Used for all computer vision tasks like object detection. 10 | ```python 11 | import cv2 12 | img = cv2.imread('image.jpg') # Load image as BGR 13 | ``` 14 | 15 | 2. **How do you access and modify pixel values in an OpenCV image? Give an example.** 16 | Pixel values are accessed via NumPy array indexing, useful for image preprocessing (e.g., adjusting brightness). 17 | ```python 18 | import cv2 19 | img = cv2.imread('image.jpg') 20 | pixel = img[100, 100] # Access BGR at (100, 100) 21 | img[100, 100] = [0, 255, 0] # Set to green 22 | ``` 23 | 24 | 3. **What is the difference between grayscale and color images in OpenCV?** 25 | Grayscale images are single-channel (intensity), while color images (BGR in OpenCV) have three channels. Grayscale is used for simpler tasks like edge detection. 26 | ```python 27 | import cv2 28 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) # Grayscale 29 | img_color = cv2.imread('image.jpg') # BGR 30 | ``` 31 | 32 | 4. **Explain how to save an image in OpenCV.** 33 | Saving images preserves processed results (e.g., filtered images) for further analysis or model input. 34 | ```python 35 | import cv2 36 | img = cv2.imread('image.jpg') 37 | cv2.imwrite('output.jpg', img) 38 | ``` 39 | 40 | #### Intermediate 41 | 5. **How do you convert an image between color spaces (e.g., BGR to grayscale or HSV)? Provide an example.** 42 | Color space conversion is critical for tasks like color-based segmentation (HSV) or simplifying processing (grayscale). 43 | ```python 44 | import cv2 45 | img = cv2.imread('image.jpg') 46 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 47 | hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 48 | ``` 49 | 50 | 6. **Write a function to resize an image in OpenCV while maintaining aspect ratio.** 51 | Resizing is essential for preparing images for neural networks with fixed input sizes. 52 | ```python 53 | import cv2 54 | def resize_image(img, width): 55 | ratio = width / img.shape[1] 56 | height = int(img.shape[0] * ratio) 57 | return cv2.resize(img, (width, height)) 58 | img = cv2.imread('image.jpg') 59 | resized = resize_image(img, 300) 60 | ``` 61 | 62 | 7. **How do you crop an image in OpenCV? Give an example.** 63 | Cropping isolates regions of interest (ROI) for focused analysis, like face detection. 64 | ```python 65 | import cv2 66 | img = cv2.imread('image.jpg') 67 | cropped = img[50:150, 100:200] # Crop (y1:y2, x1:x2) 68 | ``` 69 | 70 | #### Advanced 71 | 8. **Explain image channels and how to split and merge them in OpenCV.** 72 | Channels (e.g., B, G, R) represent color components, split for individual analysis or merged to reconstruct images. 73 | ```python 74 | import cv2 75 | img = cv2.imread('image.jpg') 76 | b, g, r = cv2.split(img) # Split channels 77 | merged = cv2.merge([b, g, r]) # Merge back 78 | ``` 79 | 80 | 9. **Write a function to normalize pixel values in an image.** 81 | Normalization scales pixel values (e.g., to [0, 1]) for consistent input to ML models. 82 | ```python 83 | import cv2 84 | import numpy as np 85 | def normalize_image(img): 86 | return img / 255.0 87 | img = cv2.imread('image.jpg') 88 | normalized = normalize_image(img) 89 | ``` 90 | 91 | 10. **Implement a function to rotate an image without cropping using OpenCV.** 92 | Rotation is used in data augmentation for training robust vision models. 93 | ```python 94 | import cv2 95 | import numpy as np 96 | def rotate_image(img, angle): 97 | h, w = img.shape[:2] 98 | center = (w // 2, h // 2) 99 | M = cv2.getRotationMatrix2D(center, angle, 1.0) 100 | cos, sin = np.abs(M[0, 0]), np.abs(M[0, 1]) 101 | new_w = int((h * sin) + (w * cos)) 102 | new_h = int((h * cos) + (w * sin)) 103 | M[0, 2] += (new_w / 2) - center[0] 104 | M[1, 2] += (new_h / 2) - center[1] 105 | return cv2.warpAffine(img, M, (new_w, new_h)) 106 | img = cv2.imread('image.jpg') 107 | rotated = rotate_image(img, 45) 108 | ``` 109 | 110 | ### Image Processing 111 | 112 | #### Basic 113 | 11. **What is image thresholding in OpenCV? Provide an example.** 114 | Thresholding converts images to binary (e.g., for segmenting objects). 115 | ```python 116 | import cv2 117 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 118 | _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) 119 | ``` 120 | 121 | 12. **How do you apply a Gaussian blur to an image in OpenCV? Give an example.** 122 | Gaussian blur reduces noise, aiding feature detection. 123 | ```python 124 | import cv2 125 | img = cv2.imread('image.jpg') 126 | blurred = cv2.GaussianBlur(img, (5, 5), 0) 127 | ``` 128 | 129 | 13. **What is the purpose of the `cv2.imshow` function, and how is it used?** 130 | Displays images for debugging or visualization during development. 131 | ```python 132 | import cv2 133 | img = cv2.imread('image.jpg') 134 | cv2.imshow('Image', img) 135 | cv2.waitKey(0) 136 | cv2.destroyAllWindows() 137 | ``` 138 | 139 | #### Intermediate 140 | 14. **Explain adaptive thresholding and provide an example.** 141 | Adaptive thresholding adjusts thresholds locally, ideal for uneven lighting in images. 142 | ```python 143 | import cv2 144 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 145 | adaptive = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) 146 | ``` 147 | 148 | 15. **Write a function to apply a sharpening filter to an image.** 149 | Sharpening enhances edges for better feature detection. 150 | ```python 151 | import cv2 152 | import numpy as np 153 | def sharpen_image(img): 154 | kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) 155 | return cv2.filter2D(img, -1, kernel) 156 | img = cv2.imread('image.jpg') 157 | sharpened = sharpen_image(img) 158 | ``` 159 | 160 | 16. **How do you perform histogram equalization in OpenCV? Provide an example.** 161 | Histogram equalization enhances contrast, useful for low-light images. 162 | ```python 163 | import cv2 164 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 165 | equalized = cv2.equalizeHist(img) 166 | ``` 167 | 168 | #### Advanced 169 | 17. **Implement a function to apply a custom convolution kernel.** 170 | Custom kernels enable specialized filtering (e.g., edge detection). 171 | ```python 172 | import cv2 173 | import numpy as np 174 | def apply_kernel(img, kernel): 175 | return cv2.filter2D(img, -1, kernel) 176 | img = cv2.imread('image.jpg') 177 | kernel = np.array([[0, -1, 0], [-1, 4, -1], [0, -1, 0]]) 178 | filtered = apply_kernel(img, kernel) 179 | ``` 180 | 181 | 18. **Write a function to perform morphological operations (e.g., dilation).** 182 | Morphological operations like dilation enhance shapes for segmentation. 183 | ```python 184 | import cv2 185 | import numpy as np 186 | def dilate_image(img, kernel_size=3): 187 | kernel = np.ones((kernel_size, kernel_size), np.uint8) 188 | return cv2.dilate(img, kernel, iterations=1) 189 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 190 | dilated = dilate_image(img) 191 | ``` 192 | 193 | 19. **Explain how to handle image noise using median filtering in OpenCV.** 194 | Median filtering removes salt-and-pepper noise while preserving edges. 195 | ```python 196 | import cv2 197 | img = cv2.imread('image.jpg') 198 | denoised = cv2.medianBlur(img, 5) 199 | ``` 200 | 201 | ### Feature Detection 202 | 203 | #### Basic 204 | 20. **What is edge detection in OpenCV, and how is it performed using Canny?** 205 | Edge detection identifies boundaries, crucial for object recognition. 206 | ```python 207 | import cv2 208 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 209 | edges = cv2.Canny(img, 100, 200) 210 | ``` 211 | 212 | 21. **How do you detect corners in an image using OpenCV? Give an example.** 213 | Corner detection (e.g., Harris) identifies keypoints for tracking or matching. 214 | ```python 215 | import cv2 216 | import numpy as np 217 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 218 | corners = cv2.cornerHarris(img, 2, 3, 0.04) 219 | ``` 220 | 221 | 22. **What are contours in OpenCV, and how do you find them?** 222 | Contours are curves joining continuous points, used for shape analysis. 223 | ```python 224 | import cv2 225 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 226 | _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) 227 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 228 | ``` 229 | 230 | #### Intermediate 231 | 23. **Write a function to detect and draw contours in an image.** 232 | Visualizes object boundaries for segmentation tasks. 233 | ```python 234 | import cv2 235 | def draw_contours(img): 236 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 237 | _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) 238 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 239 | return cv2.drawContours(img.copy(), contours, -1, (0, 255, 0), 2) 240 | img = cv2.imread('image.jpg') 241 | contoured = draw_contours(img) 242 | ``` 243 | 244 | 24. **How do you use SIFT for feature detection in OpenCV? Provide an example.** 245 | SIFT detects scale-invariant keypoints for image matching. 246 | ```python 247 | import cv2 248 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 249 | sift = cv2.SIFT_create() 250 | keypoints, descriptors = sift.detectAndCompute(img, None) 251 | ``` 252 | 253 | 25. **Explain Hough Transform for line detection and provide an example.** 254 | Hough Transform detects lines, useful for structural analysis. 255 | ```python 256 | import cv2 257 | import numpy as np 258 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 259 | edges = cv2.Canny(img, 100, 200) 260 | lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) 261 | ``` 262 | 263 | #### Advanced 264 | 26. **Implement a function to match features between two images using ORB.** 265 | Feature matching aligns images for tasks like panorama stitching. 266 | ```python 267 | import cv2 268 | def match_features(img1, img2): 269 | orb = cv2.ORB_create() 270 | kp1, des1 = orb.detectAndCompute(img1, None) 271 | kp2, des2 = orb.detectAndCompute(img2, None) 272 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 273 | matches = bf.match(des1, des2) 274 | return sorted(matches, key=lambda x: x.distance) 275 | img1 = cv2.imread('image1.jpg', cv2.IMREAD_GRAYSCALE) 276 | img2 = cv2.imread('image2.jpg', cv2.IMREAD_GRAYSCALE) 277 | matches = match_features(img1, img2) 278 | ``` 279 | 280 | 27. **Write a function to detect circles using Hough Transform.** 281 | Circle detection is used in applications like pupil tracking. 282 | ```python 283 | import cv2 284 | import numpy as np 285 | def detect_circles(img): 286 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 287 | blurred = cv2.medianBlur(gray, 5) 288 | circles = cv2.HoughCircles(blurred, cv2.HOUGH_GRADIENT, 1, 20, param1=50, param2=30, minRadius=0, maxRadius=0) 289 | if circles is not None: 290 | circles = np.uint16(np.around(circles)) 291 | for i in circles[0, :]: 292 | cv2.circle(img, (i[0], i[1]), i[2], (0, 255, 0), 2) 293 | return img 294 | img = cv2.imread('image.jpg') 295 | circled = detect_circles(img) 296 | ``` 297 | 298 | 28. **Explain how to use SURF for robust feature detection.** 299 | SURF is faster than SIFT and robust to scale/rotation, used for object recognition (requires `opencv-contrib-python`). 300 | ```python 301 | import cv2 302 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 303 | surf = cv2.xfeatures2d.SURF_create() 304 | keypoints, descriptors = surf.detectAndCompute(img, None) 305 | ``` 306 | 307 | ### Image Transformations 308 | 309 | #### Basic 310 | 29. **What is an affine transformation in OpenCV? Provide an example.** 311 | Affine transformations (e.g., translation, rotation) preserve lines, used for image alignment. 312 | ```python 313 | import cv2 314 | import numpy as np 315 | img = cv2.imread('image.jpg') 316 | h, w = img.shape[:2] 317 | M = np.float32([[1, 0, 50], [0, 1, 20]]) # Translate 318 | translated = cv2.warpAffine(img, M, (w, h)) 319 | ``` 320 | 321 | 30. **How do you flip an image in OpenCV? Give an example.** 322 | Flipping augments data for training vision models. 323 | ```python 324 | import cv2 325 | img = cv2.imread('image.jpg') 326 | flipped = cv2.flip(img, 1) # Horizontal flip 327 | ``` 328 | 329 | 31. **Explain perspective transformation in OpenCV.** 330 | Perspective transformation corrects distortions (e.g., for document scanning). 331 | ```python 332 | import cv2 333 | import numpy as np 334 | img = cv2.imread('image.jpg') 335 | pts1 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]]) 336 | pts2 = np.float32([[0, 0], [400, 0], [0, 400], [400, 400]]) 337 | M = cv2.getPerspectiveTransform(pts1, pts2) 338 | warped = cv2.warpPerspective(img, M, (400, 400)) 339 | ``` 340 | 341 | #### Intermediate 342 | 32. **Write a function to apply a shear transformation to an image.** 343 | Shear transformations augment data or simulate distortions. 344 | ```python 345 | import cv2 346 | import numpy as np 347 | def shear_image(img, shear_factor=0.2): 348 | h, w = img.shape[:2] 349 | M = np.float32([[1, shear_factor, 0], [0, 1, 0]]) 350 | return cv2.warpAffine(img, M, (w, h)) 351 | img = cv2.imread('image.jpg') 352 | sheared = shear_image(img) 353 | ``` 354 | 355 | 33. **How do you perform image translation in OpenCV? Provide an example.** 356 | Translation shifts images, used in data augmentation. 357 | ```python 358 | import cv2 359 | import numpy as np 360 | img = cv2.imread('image.jpg') 361 | h, w = img.shape[:2] 362 | M = np.float32([[1, 0, 100], [0, 1, 50]]) 363 | translated = cv2.warpAffine(img, M, (w, h)) 364 | ``` 365 | 366 | 34. **Implement a function to scale an image non-uniformly.** 367 | Non-uniform scaling adjusts dimensions differently, useful for specific model inputs. 368 | ```python 369 | import cv2 370 | def scale_image(img, scale_x, scale_y): 371 | return cv2.resize(img, None, fx=scale_x, fy=scale_y) 372 | img = cv2.imread('image.jpg') 373 | scaled = scale_image(img, 1.5, 0.8) 374 | ``` 375 | 376 | #### Advanced 377 | 35. **Write a function to apply a homography transformation between two images.** 378 | Homography aligns images with different perspectives (e.g., for stitching). 379 | ```python 380 | import cv2 381 | import numpy as np 382 | def apply_homography(img1, img2, pts1, pts2): 383 | H, _ = cv2.findHomography(pts1, pts2, cv2.RANSAC) 384 | h, w = img2.shape[:2] 385 | return cv2.warpPerspective(img1, H, (w, h)) 386 | img1 = cv2.imread('image1.jpg') 387 | img2 = cv2.imread('image2.jpg') 388 | pts1 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]]) 389 | pts2 = np.float32([[10, 10], [310, 0], [10, 310], [310, 310]]) 390 | warped = apply_homography(img1, img2, pts1, pts2) 391 | ``` 392 | 393 | 36. **Explain how to use optical flow for motion tracking in OpenCV.** 394 | Optical flow tracks pixel motion between frames, used in video analysis. 395 | ```python 396 | import cv2 397 | import numpy as np 398 | frame1 = cv2.imread('frame1.jpg', cv2.IMREAD_GRAYSCALE) 399 | frame2 = cv2.imread('frame2.jpg', cv2.IMREAD_GRAYSCALE) 400 | flow = cv2.calcOpticalFlowFarneback(frame1, frame2, None, 0.5, 3, 15, 3, 5, 1.2, 0) 401 | ``` 402 | 403 | 37. **Implement a function to stabilize video frames using feature matching.** 404 | Stabilization aligns frames for smoother video processing. 405 | ```python 406 | import cv2 407 | import numpy as np 408 | def stabilize_frame(prev_frame, curr_frame): 409 | orb = cv2.ORB_create() 410 | kp1, des1 = orb.detectAndCompute(prev_frame, None) 411 | kp2, des2 = orb.detectAndCompute(curr_frame, None) 412 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 413 | matches = bf.match(des1, des2) 414 | pts1 = np.float32([kp1[m.queryIdx].pt for m in matches]) 415 | pts2 = np.float32([kp2[m.trainIdx].pt for m in matches]) 416 | H, _ = cv2.findHomography(pts2, pts1, cv2.RANSAC) 417 | h, w = prev_frame.shape[:2] 418 | return cv2.warpPerspective(curr_frame, H, (w, h)) 419 | ``` 420 | 421 | ### Video Processing 422 | 423 | #### Basic 424 | 38. **How do you read a video file in OpenCV? Provide an example.** 425 | Video reading enables frame-by-frame analysis for tasks like object tracking. 426 | ```python 427 | import cv2 428 | cap = cv2.VideoCapture('video.mp4') 429 | while cap.isOpened(): 430 | ret, frame = cap.read() 431 | if not ret: 432 | break 433 | cv2.imshow('Frame', frame) 434 | if cv2.waitKey(1) & 0xFF == ord('q'): 435 | break 436 | cap.release() 437 | cv2.destroyAllWindows() 438 | ``` 439 | 440 | 39. **What is the `cv2.VideoWriter` class, and how is it used?** 441 | `VideoWriter` saves processed video frames (e.g., for annotated outputs). 442 | ```python 443 | import cv2 444 | cap = cv2.VideoCapture('video.mp4') 445 | fourcc = cv2.VideoWriter_fourcc(*'XVID') 446 | out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480)) 447 | while cap.isOpened(): 448 | ret, frame = cap.read() 449 | if not ret: 450 | break 451 | out.write(frame) 452 | cap.release() 453 | out.release() 454 | ``` 455 | 456 | 40. **How do you capture webcam input in OpenCV?** 457 | Webcam input is used for real-time vision applications like face detection. 458 | ```python 459 | import cv2 460 | cap = cv2.VideoCapture(0) 461 | while True: 462 | ret, frame = cap.read() 463 | cv2.imshow('Webcam', frame) 464 | if cv2.waitKey(1) & 0xFF == ord('q'): 465 | break 466 | cap.release() 467 | cv2.destroyAllWindows() 468 | ``` 469 | 470 | #### Intermediate 471 | 41. **Write a function to extract every nth frame from a video.** 472 | Frame extraction reduces data for efficient processing. 473 | ```python 474 | import cv2 475 | def extract_frames(video_path, n): 476 | cap = cv2.VideoCapture(video_path) 477 | frames = [] 478 | count = 0 479 | while cap.isOpened(): 480 | ret, frame = cap.read() 481 | if not ret: 482 | break 483 | if count % n == 0: 484 | frames.append(frame) 485 | count += 1 486 | cap.release() 487 | return frames 488 | ``` 489 | 490 | 42. **How do you compute the difference between consecutive video frames?** 491 | Frame differencing detects motion for tracking or event detection. 492 | ```python 493 | import cv2 494 | cap = cv2.VideoCapture('video.mp4') 495 | ret, prev_frame = cap.read() 496 | prev_gray = cv2.cvtColor(prev_frame, cv2.COLOR_BGR2GRAY) 497 | while cap.isOpened(): 498 | ret, frame = cap.read() 499 | if not ret: 500 | break 501 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 502 | diff = cv2.absdiff(prev_gray, gray) 503 | prev_gray = gray 504 | cap.release() 505 | ``` 506 | 507 | 43. **Implement a function to convert a video to grayscale.** 508 | Grayscale conversion simplifies video processing for tasks like edge detection. 509 | ```python 510 | import cv2 511 | def grayscale_video(input_path, output_path): 512 | cap = cv2.VideoCapture(input_path) 513 | fourcc = cv2.VideoWriter_fourcc(*'XVID') 514 | out = cv2.VideoWriter(output_path, fourcc, 20.0, (int(cap.get(3)), int(cap.get(4))), isColor=False) 515 | while cap.isOpened(): 516 | ret, frame = cap.read() 517 | if not ret: 518 | break 519 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 520 | out.write(gray) 521 | cap.release() 522 | out.release() 523 | ``` 524 | 525 | #### Advanced 526 | 44. **Write a function to track an object in a video using background subtraction.** 527 | Background subtraction isolates moving objects for tracking. 528 | ```python 529 | import cv2 530 | def track_object(video_path): 531 | cap = cv2.VideoCapture(video_path) 532 | fgbg = cv2.createBackgroundSubtractorMOG2() 533 | while cap.isOpened(): 534 | ret, frame = cap.read() 535 | if not ret: 536 | break 537 | fgmask = fgbg.apply(frame) 538 | cv2.imshow('Tracking', fgmask) 539 | if cv2.waitKey(1) & 0xFF == ord('q'): 540 | break 541 | cap.release() 542 | cv2.destroyAllWindows() 543 | ``` 544 | 545 | 45. **Implement a function to detect faces in a video stream using Haar cascades.** 546 | Face detection is a common real-time vision task. 547 | ```python 548 | import cv2 549 | def detect_faces(video_path): 550 | cap = cv2.VideoCapture(video_path) 551 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 552 | while cap.isOpened(): 553 | ret, frame = cap.read() 554 | if not ret: 555 | break 556 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 557 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 558 | for (x, y, w, h) in faces: 559 | cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2) 560 | cv2.imshow('Faces', frame) 561 | if cv2.waitKey(1) & 0xFF == ord('q'): 562 | break 563 | cap.release() 564 | cv2.destroyAllWindows() 565 | ``` 566 | 567 | 46. **Explain how to use Deep Neural Networks (DNN) module in OpenCV for object detection.** 568 | OpenCV’s DNN module runs pre-trained models (e.g., YOLO) for real-time detection, integrating with frameworks like TensorFlow. 569 | ```python 570 | import cv2 571 | net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights') 572 | img = cv2.imread('image.jpg') 573 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True) 574 | net.setInput(blob) 575 | outputs = net.forward(net.getUnconnectedOutLayersNames()) 576 | ``` 577 | 578 | ### Object Detection and Recognition 579 | 580 | #### Basic 581 | 47. **What is Haar cascade in OpenCV, and how is it used for face detection?** 582 | Haar cascades are classifiers for detecting objects like faces using features. 583 | ```python 584 | import cv2 585 | img = cv2.imread('image.jpg') 586 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 587 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 588 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 589 | ``` 590 | 591 | 48. **How do you draw bounding boxes around detected objects? Give an example.** 592 | Bounding boxes visualize detected objects for annotation. 593 | ```python 594 | import cv2 595 | img = cv2.imread('image.jpg') 596 | boxes = [(50, 50, 100, 100)] # (x, y, w, h) 597 | for (x, y, w, h) in boxes: 598 | cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) 599 | ``` 600 | 601 | 49. **Explain template matching in OpenCV.** 602 | Template matching finds a small image (template) in a larger image, used for pattern recognition. 603 | ```python 604 | import cv2 605 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 606 | template = cv2.imread('template.jpg', cv2.IMREAD_GRAYSCALE) 607 | result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) 608 | ``` 609 | 610 | #### Intermediate 611 | 50. **Write a function to perform template matching and draw the result.** 612 | Visualizes matched regions for object localization. 613 | ```python 614 | import cv2 615 | import numpy as np 616 | def template_match(img, template): 617 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 618 | temp_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY) 619 | result = cv2.matchTemplate(gray, temp_gray, cv2.TM_CCOEFF_NORMED) 620 | min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) 621 | top_left = max_loc 622 | h, w = temp_gray.shape 623 | cv2.rectangle(img, top_left, (top_left[0] + w, top_left[1] + h), (0, 255, 0), 2) 624 | return img 625 | img = cv2.imread('image.jpg') 626 | template = cv2.imread('template.jpg') 627 | matched = template_match(img, template) 628 | ``` 629 | 630 | 51. **How do you use HOG descriptors for pedestrian detection?** 631 | HOG (Histogram of Oriented Gradients) extracts features for detecting objects like pedestrians. 632 | ```python 633 | import cv2 634 | img = cv2.imread('image.jpg') 635 | hog = cv2.HOGDescriptor() 636 | hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) 637 | boxes, _ = hog.detectMultiScale(img, winStride=(8, 8)) 638 | ``` 639 | 640 | 52. **Implement a function to detect eyes in an image using Haar cascades.** 641 | Eye detection is used in facial analysis applications. 642 | ```python 643 | import cv2 644 | def detect_eyes(img): 645 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 646 | eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml') 647 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 648 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 649 | for (x, y, w, h) in faces: 650 | roi_gray = gray[y:y+h, x:x+w] 651 | eyes = eye_cascade.detectMultiScale(roi_gray) 652 | for (ex, ey, ew, eh) in eyes: 653 | cv2.rectangle(img, (x+ex, y+ey), (x+ex+ew, y+ey+eh), (0, 255, 0), 2) 654 | return img 655 | img = cv2.imread('image.jpg') 656 | eyed = detect_eyes(img) 657 | ``` 658 | 659 | #### Advanced 660 | 53. **Write a function to perform YOLO object detection using OpenCV DNN.** 661 | YOLO provides real-time object detection with high accuracy. 662 | ```python 663 | import cv2 664 | import numpy as np 665 | def yolo_detect(img, config_path, weights_path, classes_path): 666 | net = cv2.dnn.readNetFromDarknet(config_path, weights_path) 667 | with open(classes_path, 'r') as f: 668 | classes = [line.strip() for line in f] 669 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True) 670 | net.setInput(blob) 671 | outputs = net.forward(net.getUnconnectedOutLayersNames()) 672 | boxes, confidences, class_ids = [], [], [] 673 | h, w = img.shape[:2] 674 | for output in outputs: 675 | for detection in output: 676 | scores = detection[5:] 677 | class_id = np.argmax(scores) 678 | confidence = scores[class_id] 679 | if confidence > 0.5: 680 | box = detection[0:4] * np.array([w, h, w, h]) 681 | (center_x, center_y, width, height) = box.astype("int") 682 | x = int(center_x - width / 2) 683 | y = int(center_y - height / 2) 684 | boxes.append([x, y, int(width), int(height)]) 685 | confidences.append(float(confidence)) 686 | class_ids.append(class_id) 687 | indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) 688 | for i in indices: 689 | box = boxes[i] 690 | x, y, w, h = box 691 | label = str(classes[class_ids[i]]) 692 | cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) 693 | cv2.putText(img, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) 694 | return img 695 | img = cv2.imread('image.jpg') 696 | detected = yolo_detect(img, 'yolov3.cfg', 'yolov3.weights', 'coco.names') 697 | ``` 698 | 699 | 54. **Explain how to fine-tune a pre-trained model using OpenCV DNN.** 700 | Fine-tuning adapts models like MobileNet for specific tasks, using OpenCV to load and run inference. 701 | ```python 702 | import cv2 703 | net = cv2.dnn.readNet('model.onnx') 704 | img = cv2.imread('image.jpg') 705 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (224, 224), swapRB=True) 706 | net.setInput(blob) 707 | output = net.forward() 708 | ``` 709 | 710 | 55. **Implement a function for real-time object tracking using OpenCV trackers.** 711 | Trackers like CSRT follow objects across frames in videos. 712 | ```python 713 | import cv2 714 | def track_object(video_path, bbox): 715 | cap = cv2.VideoCapture(video_path) 716 | tracker = cv2.TrackerCSRT_create() 717 | ret, frame = cap.read() 718 | tracker.init(frame, bbox) 719 | while cap.isOpened(): 720 | ret, frame = cap.read() 721 | if not ret: 722 | break 723 | success, box = tracker.update(frame) 724 | if success: 725 | x, y, w, h = [int(v) for v in box] 726 | cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2) 727 | cv2.imshow('Tracking', frame) 728 | if cv2.waitKey(1) & 0xFF == ord('q'): 729 | break 730 | cap.release() 731 | cv2.destroyAllWindows() 732 | ``` 733 | 734 | ### Image Segmentation 735 | 736 | #### Basic 737 | 56. **What is image segmentation, and how is it performed in OpenCV?** 738 | Segmentation partitions images into regions (e.g., objects vs. background). 739 | ```python 740 | import cv2 741 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 742 | _, segmented = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) 743 | ``` 744 | 745 | 57. **How do you use watershed algorithm for segmentation in OpenCV?** 746 | Watershed separates touching objects based on markers. 747 | ```python 748 | import cv2 749 | import numpy as np 750 | img = cv2.imread('image.jpg') 751 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 752 | _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) 753 | markers = cv2.watershed(img, np.zeros_like(gray, dtype=np.int32)) 754 | ``` 755 | 756 | 58. **Explain GrabCut for foreground-background segmentation.** 757 | GrabCut iteratively separates foreground from background using a bounding box. 758 | ```python 759 | import cv2 760 | import numpy as np 761 | img = cv2.imread('image.jpg') 762 | mask = np.zeros(img.shape[:2], np.uint8) 763 | bgdModel = np.zeros((1, 65), np.float64) 764 | fgdModel = np.zeros((1, 65), np.float64) 765 | rect = (50, 50, 200, 200) 766 | cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) 767 | ``` 768 | 769 | #### Intermediate 770 | 59. **Write a function to perform k-means clustering for color-based segmentation.** 771 | K-means groups pixels by color, segmenting images into regions. 772 | ```python 773 | import cv2 774 | import numpy as np 775 | def kmeans_segmentation(img, k=3): 776 | pixels = img.reshape((-1, 3)).astype(np.float32) 777 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 778 | _, labels, centers = cv2.kmeans(pixels, k, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) 779 | centers = np.uint8(centers) 780 | segmented = centers[labels.flatten()] 781 | return segmented.reshape(img.shape) 782 | img = cv2.imread('image.jpg') 783 | segmented = kmeans_segmentation(img) 784 | ``` 785 | 786 | 60. **How do you use contour-based segmentation in OpenCV?** 787 | Contours define object boundaries for segmentation. 788 | ```python 789 | import cv2 790 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 791 | _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) 792 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 793 | mask = np.zeros_like(img) 794 | cv2.drawContours(mask, contours, -1, 255, -1) 795 | ``` 796 | 797 | 61. **Implement a function for semantic segmentation using a pre-trained model.** 798 | Semantic segmentation assigns class labels to pixels using DNN models. 799 | ```python 800 | import cv2 801 | import numpy as np 802 | def semantic_segmentation(img, model_path): 803 | net = cv2.dnn.readNet(model_path) 804 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (512, 512), swapRB=True) 805 | net.setInput(blob) 806 | output = net.forward() 807 | return np.argmax(output[0], axis=0) 808 | img = cv2.imread('image.jpg') 809 | segmented = semantic_segmentation(img, 'model.onnx') 810 | ``` 811 | 812 | #### Advanced 813 | 62. **Write a function to combine watershed and GrabCut for robust segmentation.** 814 | Combining methods improves accuracy for complex scenes. 815 | ```python 816 | import cv2 817 | import numpy as np 818 | def combined_segmentation(img, rect): 819 | mask = np.zeros(img.shape[:2], np.uint8) 820 | bgdModel = np.zeros((1, 65), np.float64) 821 | fgdModel = np.zeros((1, 65), np.float64) 822 | cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) 823 | mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') 824 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 825 | _, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU) 826 | markers = cv2.watershed(img, mask2.astype(np.int32)) 827 | img[markers == -1] = [255, 0, 0] 828 | return img 829 | img = cv2.imread('image.jpg') 830 | rect = (50, 50, 200, 200) 831 | segmented = combined_segmentation(img, rect) 832 | ``` 833 | 834 | 63. **Explain how to use DeepLab for segmentation in OpenCV.** 835 | DeepLab models provide high-accuracy semantic segmentation, run via OpenCV’s DNN module. 836 | ```python 837 | import cv2 838 | net = cv2.dnn.readNet('deeplabv3.onnx') 839 | img = cv2.imread('image.jpg') 840 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (513, 513), swapRB=True) 841 | net.setInput(blob) 842 | output = net.forward() 843 | ``` 844 | 845 | 64. **Implement a function for instance segmentation using Mask R-CNN.** 846 | Instance segmentation identifies and masks individual objects. 847 | ```python 848 | import cv2 849 | import numpy as np 850 | def mask_rcnn_segmentation(img, config_path, weights_path): 851 | net = cv2.dnn.readNetFromTensorflow(weights_path, config_path) 852 | blob = cv2.dnn.blobFromImage(img, swapRB=True) 853 | net.setInput(blob) 854 | boxes, masks = net.forward(['detection_out_final', 'detection_masks']) 855 | return boxes, masks 856 | img = cv2.imread('image.jpg') 857 | boxes, masks = mask_rcnn_segmentation(img, 'mask_rcnn.cfg', 'mask_rcnn.weights') 858 | ``` 859 | 860 | ### Camera Calibration 861 | 862 | #### Basic 863 | 65. **What is camera calibration in OpenCV, and why is it important?** 864 | Calibration corrects lens distortions, critical for accurate 3D reconstruction. 865 | ```python 866 | import cv2 867 | import numpy as np 868 | objp = np.zeros((6*7, 3), np.float32) 869 | objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2) 870 | ``` 871 | 872 | 66. **How do you find chessboard corners for calibration? Provide an example.** 873 | Chessboard corners provide points for computing camera parameters. 874 | ```python 875 | import cv2 876 | img = cv2.imread('chessboard.jpg', cv2.IMREAD_GRAYSCALE) 877 | ret, corners = cv2.findChessboardCorners(img, (7, 6), None) 878 | ``` 879 | 880 | 67. **Explain the `cv2.calibrateCamera` function.** 881 | Computes intrinsic/extrinsic parameters from object points and image points. 882 | ```python 883 | import cv2 884 | import numpy as np 885 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera([objp], [corners], img.shape[::-1], None, None) 886 | ``` 887 | 888 | #### Intermediate 889 | 68. **Write a function to undistort an image using camera calibration parameters.** 890 | Undistortion corrects lens effects for accurate analysis. 891 | ```python 892 | import cv2 893 | import numpy as np 894 | def undistort_image(img, mtx, dist): 895 | h, w = img.shape[:2] 896 | newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h)) 897 | undistorted = cv2.undistort(img, mtx, dist, None, newcameramtx) 898 | x, y, w, h = roi 899 | return undistorted[y:y+h, x:x+w] 900 | img = cv2.imread('image.jpg') 901 | mtx = np.array([[fx, 0, cx], [0, fy, cy], [0, 0, 1]]) 902 | dist = np.array([k1, k2, p1, p2, k3]) 903 | undistorted = undistort_image(img, mtx, dist) 904 | ``` 905 | 906 | 69. **How do you compute the reprojection error in camera calibration?** 907 | Reprojection error measures calibration accuracy. 908 | ```python 909 | import cv2 910 | import numpy as np 911 | mean_error = 0 912 | for i in range(len(objpoints)): 913 | imgpoints2, _ = cv2.projectPoints(objpoints[i], rvecs[i], tvecs[i], mtx, dist) 914 | error = cv2.norm(imgpoints[i], imgpoints2, cv2.NORM_L2) / len(imgpoints2) 915 | mean_error += error 916 | print("Mean reprojection error:", mean_error / len(objpoints)) 917 | ``` 918 | 919 | 70. **Implement a function to calibrate a camera using multiple images.** 920 | Uses multiple chessboard images for robust calibration. 921 | ```python 922 | import cv2 923 | import numpy as np 924 | def calibrate_camera(images, pattern_size=(7, 6)): 925 | objp = np.zeros((pattern_size[0] * pattern_size[1], 3), np.float32) 926 | objp[:, :2] = np.mgrid[0:pattern_size[0], 0:pattern_size[1]].T.reshape(-1, 2) 927 | objpoints, imgpoints = [], [] 928 | for img in images: 929 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 930 | ret, corners = cv2.findChessboardCorners(gray, pattern_size, None) 931 | if ret: 932 | objpoints.append(objp) 933 | imgpoints.append(corners) 934 | ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) 935 | return mtx, dist 936 | ``` 937 | 938 | #### Advanced 939 | 71. **Explain stereo calibration in OpenCV.** 940 | Stereo calibration computes relative positions of two cameras for 3D reconstruction. 941 | ```python 942 | import cv2 943 | ret, mtx1, dist1, mtx2, dist2, R, T, E, F = cv2.stereoCalibrate(objpoints, imgpoints1, imgpoints2, mtx1, dist1, mtx2, dist2, gray.shape[::-1]) 944 | ``` 945 | 946 | 72. **Write a function for 3D reconstruction using stereo images.** 947 | Reconstructs 3D points from stereo pairs. 948 | ```python 949 | import cv2 950 | import numpy as np 951 | def reconstruct_3d(img1, img2, mtx1, dist1, mtx2, dist2, R, T): 952 | img1 = cv2.undistort(img1, mtx1, dist1) 953 | img2 = cv2.undistort(img2, mtx2, dist2) 954 | stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15) 955 | disparity = stereo.compute(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)) 956 | Q = cv2.stereoRectify(mtx1, dist1, mtx2, dist2, img1.shape[:2], R, T)[4] 957 | points_3d = cv2.reprojectImageTo3D(disparity, Q) 958 | return points_3d 959 | ``` 960 | 961 | 73. **Implement a function to estimate camera pose using solvePnP.** 962 | Pose estimation determines camera orientation relative to an object. 963 | ```python 964 | import cv2 965 | import numpy as np 966 | def estimate_pose(obj_points, img_points, mtx, dist): 967 | ret, rvec, tvec = cv2.solvePnP(obj_points, img_points, mtx, dist) 968 | return rvec, tvec 969 | ``` 970 | 971 | ### Machine Learning Integration 972 | 973 | #### Basic 974 | 74. **How do you prepare OpenCV images for machine learning models?** 975 | Images are resized, normalized, and converted to arrays for ML input. 976 | ```python 977 | import cv2 978 | import numpy as np 979 | img = cv2.imread('image.jpg') 980 | img = cv2.resize(img, (224, 224)) # Resize 981 | img = img / 255.0 # Normalize 982 | img = np.expand_dims(img, axis=0) # Add batch dimension 983 | ``` 984 | 985 | 75. **What is the role of OpenCV in data augmentation for ML?** 986 | OpenCV applies transformations like rotation, flipping, and cropping to augment datasets. 987 | ```python 988 | import cv2 989 | img = cv2.imread('image.jpg') 990 | flipped = cv2.flip(img, 1) 991 | rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE) 992 | ``` 993 | 994 | 76. **How do you extract features from images for traditional ML models?** 995 | Features like HOG or SIFT are extracted for models like SVM. 996 | ```python 997 | import cv2 998 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 999 | sift = cv2.SIFT_create() 1000 | keypoints, descriptors = sift.detectAndCompute(img, None) 1001 | ``` 1002 | 1003 | #### Intermediate 1004 | 77. **Write a function to generate augmented images for ML training.** 1005 | Augmentation increases dataset diversity. 1006 | ```python 1007 | import cv2 1008 | import numpy as np 1009 | def augment_image(img): 1010 | augmented = [] 1011 | augmented.append(cv2.flip(img, 1)) # Horizontal flip 1012 | augmented.append(cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)) 1013 | h, w = img.shape[:2] 1014 | M = np.float32([[1, 0, 50], [0, 1, 20]]) 1015 | augmented.append(cv2.warpAffine(img, M, (w, h))) # Translate 1016 | return augmented 1017 | img = cv2.imread('image.jpg') 1018 | augmented = augment_image(img) 1019 | ``` 1020 | 1021 | 78. **How do you integrate OpenCV with scikit-learn for image classification?** 1022 | Extract features with OpenCV and train with scikit-learn. 1023 | ```python 1024 | import cv2 1025 | from sklearn.svm import SVC 1026 | img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE) 1027 | hog = cv2.HOGDescriptor() 1028 | features = hog.compute(img) 1029 | clf = SVC() 1030 | clf.fit([features], [1]) # Example training 1031 | ``` 1032 | 1033 | 79. **Implement a function to preprocess images for a CNN.** 1034 | Prepares images for deep learning frameworks like TensorFlow. 1035 | ```python 1036 | import cv2 1037 | import numpy as np 1038 | def preprocess_for_cnn(img, size=(224, 224)): 1039 | img = cv2.resize(img, size) 1040 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert to RGB 1041 | img = img / 255.0 # Normalize 1042 | return np.expand_dims(img, axis=0) 1043 | img = cv2.imread('image.jpg') 1044 | processed = preprocess_for_cnn(img) 1045 | ``` 1046 | 1047 | #### Advanced 1048 | 80. **Write a function to use OpenCV with a pre-trained TensorFlow model.** 1049 | Runs inference on images using OpenCV and TensorFlow. 1050 | ```python 1051 | import cv2 1052 | import tensorflow as tf 1053 | def run_inference(img, model_path): 1054 | model = tf.keras.models.load_model(model_path) 1055 | img = preprocess_for_cnn(img) 1056 | return model.predict(img) 1057 | img = cv2.imread('image.jpg') 1058 | predictions = run_inference(img, 'model.h5') 1059 | ``` 1060 | 1061 | 81. **Explain how to use OpenCV for real-time inference with deep learning models.** 1062 | OpenCV’s DNN module enables real-time inference with low latency. 1063 | ```python 1064 | import cv2 1065 | net = cv2.dnn.readNet('model.onnx') 1066 | cap = cv2.VideoCapture(0) 1067 | while True: 1068 | ret, frame = cap.read() 1069 | blob = cv2.dnn.blobFromImage(frame, 1/255.0, (224, 224), swapRB=True) 1070 | net.setInput(blob) 1071 | output = net.forward() 1072 | cv2.imshow('Inference', frame) 1073 | if cv2.waitKey(1) & 0xFF == ord('q'): 1074 | break 1075 | cap.release() 1076 | cv2.destroyAllWindows() 1077 | ``` 1078 | 1079 | 82. **Implement a function for active learning with OpenCV.** 1080 | Selects uncertain samples for labeling using model predictions. 1081 | ```python 1082 | import cv2 1083 | import numpy as np 1084 | def active_learning(images, model): 1085 | uncertainties = [] 1086 | for img in images: 1087 | processed = preprocess_for_cnn(img) 1088 | pred = model.predict(processed) 1089 | uncertainty = -np.sum(pred * np.log(pred), axis=1) # Entropy 1090 | uncertainties.append(uncertainty) 1091 | return np.argsort(uncertainties)[-10:] # Top 10 uncertain 1092 | ``` 1093 | 1094 | ### Performance Optimization 1095 | 1096 | #### Basic 1097 | 83. **How do you optimize image processing in OpenCV?** 1098 | Use efficient functions (e.g., `cv2.resize` over loops) and smaller image sizes. 1099 | ```python 1100 | import cv2 1101 | img = cv2.imread('image.jpg') 1102 | resized = cv2.resize(img, (100, 100)) # Faster processing 1103 | ``` 1104 | 1105 | 84. **What is the role of NumPy in OpenCV performance?** 1106 | OpenCV uses NumPy for fast array operations, avoiding slow Python loops. 1107 | ```python 1108 | import cv2 1109 | import numpy as np 1110 | img = cv2.imread('image.jpg') 1111 | img = np.clip(img + 50, 0, 255).astype(np.uint8) # Brighten 1112 | ``` 1113 | 1114 | 85. **How do you use multi-threading with OpenCV for video processing?** 1115 | Multi-threading parallelizes frame processing for real-time applications. 1116 | ```python 1117 | import cv2 1118 | import threading 1119 | cap = cv2.VideoCapture('video.mp4') 1120 | def process_frame(frame): 1121 | return cv2.GaussianBlur(frame, (5, 5), 0) 1122 | while cap.isOpened(): 1123 | ret, frame = cap.read() 1124 | if not ret: 1125 | break 1126 | t = threading.Thread(target=process_frame, args=(frame,)) 1127 | t.start() 1128 | cap.release() 1129 | ``` 1130 | 1131 | #### Intermediate 1132 | 86. **Write a function to process large images in chunks.** 1133 | Chunking reduces memory usage for high-resolution images. 1134 | ```python 1135 | import cv2 1136 | import numpy as np 1137 | def process_in_chunks(img, chunk_size=1000): 1138 | h, w = img.shape[:2] 1139 | for y in range(0, h, chunk_size): 1140 | for x in range(0, w, chunk_size): 1141 | chunk = img[y:y+chunk_size, x:x+chunk_size] 1142 | chunk = cv2.GaussianBlur(chunk, (5, 5), 0) 1143 | img[y:y+chunk_size, x:x+chunk_size] = chunk 1144 | return img 1145 | img = cv2.imread('image.jpg') 1146 | processed = process_in_chunks(img) 1147 | ``` 1148 | 1149 | 87. **How do you use OpenCV with GPU acceleration?** 1150 | OpenCV’s CUDA module accelerates operations on compatible GPUs. 1151 | ```python 1152 | import cv2 1153 | img = cv2.imread('image.jpg') 1154 | gpu_img = cv2.cuda_GpuMat() 1155 | gpu_img.upload(img) 1156 | gpu_blurred = cv2.cuda_GaussianBlur(gpu_img, (5, 5), 0) 1157 | blurred = gpu_blurred.download() 1158 | ``` 1159 | 1160 | 88. **Implement a function to parallelize feature detection across multiple images.** 1161 | Parallel processing speeds up batch tasks. 1162 | ```python 1163 | import cv2 1164 | from multiprocessing import Pool 1165 | def detect_features(img): 1166 | orb = cv2.ORB_create() 1167 | kp, des = orb.detectAndCompute(img, None) 1168 | return kp, des 1169 | def parallel_feature_detection(images): 1170 | with Pool() as pool: 1171 | results = pool.map(detect_features, images) 1172 | return results 1173 | images = [cv2.imread(f'image{i}.jpg', cv2.IMREAD_GRAYSCALE) for i in range(1, 5)] 1174 | features = parallel_feature_detection(images) 1175 | ``` 1176 | 1177 | #### Advanced 1178 | 89. **Explain how to optimize real-time video processing in OpenCV.** 1179 | Use lower resolutions, skip frames, and leverage GPU or multi-threading. 1180 | ```python 1181 | import cv2 1182 | cap = cv2.VideoCapture(0) 1183 | while True: 1184 | ret, frame = cap.read() 1185 | if not ret: 1186 | break 1187 | frame = cv2.resize(frame, (320, 240)) # Lower resolution 1188 | cv2.imshow('Optimized', frame) 1189 | if cv2.waitKey(1) & 0xFF == ord('q'): 1190 | break 1191 | cap.release() 1192 | cv2.destroyAllWindows() 1193 | ``` 1194 | 1195 | 90. **Write a function to use OpenCV’s CUDA module for fast filtering.** 1196 | CUDA accelerates image filtering for large datasets. 1197 | ```python 1198 | import cv2 1199 | def cuda_filter(img): 1200 | gpu_img = cv2.cuda_GpuMat() 1201 | gpu_img.upload(img) 1202 | gpu_filtered = cv2.cuda_bilateralFilter(gpu_img, 5, 50, 50) 1203 | return gpu_filtered.download() 1204 | img = cv2.imread('image.jpg') 1205 | filtered = cuda_filter(img) 1206 | ``` 1207 | 1208 | 91. **Implement a function to benchmark OpenCV operations.** 1209 | Measures performance for optimization decisions. 1210 | ```python 1211 | import cv2 1212 | import time 1213 | def benchmark_operation(img, func, iterations=100): 1214 | start = time.time() 1215 | for _ in range(iterations): 1216 | func(img) 1217 | return (time.time() - start) / iterations 1218 | img = cv2.imread('image.jpg') 1219 | time_taken = benchmark_operation(img, lambda x: cv2.GaussianBlur(x, (5, 5), 0)) 1220 | print(f"Average time: {time_taken} seconds") 1221 | ``` 1222 | 1223 | ### Integration with Other Libraries 1224 | 1225 | #### Basic 1226 | 92. **How do you use OpenCV with NumPy for efficient image processing?** 1227 | NumPy enables fast array operations for OpenCV images. 1228 | ```python 1229 | import cv2 1230 | import numpy as np 1231 | img = cv2.imread('image.jpg') 1232 | img = np.clip(img * 1.5, 0, 255).astype(np.uint8) # Increase brightness 1233 | ``` 1234 | 1235 | 93. **What is the role of Matplotlib in visualizing OpenCV results?** 1236 | Matplotlib displays images and plots for analysis. 1237 | ```python 1238 | import cv2 1239 | import matplotlib.pyplot as plt 1240 | img = cv2.imread('image.jpg') 1241 | img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 1242 | plt.imshow(img_rgb) 1243 | plt.savefig('plot.png') 1244 | ``` 1245 | 1246 | 94. **How do you integrate OpenCV with Pandas for dataset preparation?** 1247 | Pandas organizes image metadata for ML pipelines. 1248 | ```python 1249 | import cv2 1250 | import pandas as pd 1251 | images = ['image1.jpg', 'image2.jpg'] 1252 | data = {'path': images, 'width': [cv2.imread(img).shape[1] for img in images]} 1253 | df = pd.DataFrame(data) 1254 | ``` 1255 | 1256 | #### Intermediate 1257 | 95. **Write a function to visualize OpenCV edge detection with Matplotlib.** 1258 | Visualizes edges for analysis. 1259 | ```python 1260 | import cv2 1261 | import matplotlib.pyplot as plt 1262 | def visualize_edges(img): 1263 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 1264 | edges = cv2.Canny(gray, 100, 200) 1265 | plt.imshow(edges, cmap='gray') 1266 | plt.savefig('edges.png') 1267 | img = cv2.imread('image.jpg') 1268 | visualize_edges(img) 1269 | ``` 1270 | 1271 | 96. **How do you use OpenCV with scikit-learn for clustering image pixels?** 1272 | Clusters pixels for segmentation. 1273 | ```python 1274 | import cv2 1275 | from sklearn.cluster import KMeans 1276 | img = cv2.imread('image.jpg') 1277 | pixels = img.reshape(-1, 3) 1278 | kmeans = KMeans(n_clusters=3) 1279 | labels = kmeans.fit_predict(pixels) 1280 | segmented = kmeans.cluster_centers_[labels].reshape(img.shape).astype(np.uint8) 1281 | ``` 1282 | 1283 | 97. **Implement a function to load and preprocess images with OpenCV and TensorFlow.** 1284 | Prepares images for deep learning. 1285 | ```python 1286 | import cv2 1287 | import tensorflow as tf 1288 | def load_and_preprocess(img_path): 1289 | img = cv2.imread(img_path) 1290 | img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 1291 | img = cv2.resize(img, (224, 224)) 1292 | img = img / 255.0 1293 | return tf.convert_to_tensor(img) 1294 | ``` 1295 | 1296 | #### Advanced 1297 | 98. **Write a function to combine OpenCV and Dlib for facial landmark detection.** 1298 | Integrates OpenCV with Dlib for precise face analysis. 1299 | ```python 1300 | import cv2 1301 | import dlib 1302 | def facial_landmarks(img): 1303 | detector = dlib.get_frontal_face_detector() 1304 | predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') 1305 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 1306 | faces = detector(gray) 1307 | for face in faces: 1308 | landmarks = predictor(gray, face) 1309 | for n in range(0, 68): 1310 | x, y = landmarks.part(n).x, landmarks.part(n).y 1311 | cv2.circle(img, (x, y), 2, (0, 255, 0), -1) 1312 | return img 1313 | img = cv2.imread('image.jpg') 1314 | landmarked = facial_landmarks(img) 1315 | ``` 1316 | 1317 | 99. **Explain how to use OpenCV with PyTorch for real-time inference.** 1318 | OpenCV preprocesses frames, and PyTorch runs the model. 1319 | ```python 1320 | import cv2 1321 | import torch 1322 | model = torch.load('model.pt') 1323 | model.eval() 1324 | cap = cv2.VideoCapture(0) 1325 | while True: 1326 | ret, frame = cap.read() 1327 | if not ret: 1328 | break 1329 | img = preprocess_for_cnn(frame) 1330 | with torch.no_grad(): 1331 | output = model(torch.tensor(img, dtype=torch.float32)) 1332 | cv2.imshow('Inference', frame) 1333 | if cv2.waitKey(1) & 0xFF == ord('q'): 1334 | break 1335 | cap.release() 1336 | cv2.destroyAllWindows() 1337 | ``` 1338 | 1339 | 100. **Implement a function to generate a dataset with OpenCV and Pandas.** 1340 | Creates structured datasets for ML. 1341 | ```python 1342 | import cv2 1343 | import pandas as pd 1344 | def create_dataset(image_paths, labels): 1345 | data = [] 1346 | for path, label in zip(image_paths, labels): 1347 | img = cv2.imread(path) 1348 | h, w, c = img.shape 1349 | data.append({'path': path, 'label': label, 'width': w, 'height': h}) 1350 | return pd.DataFrame(data) 1351 | ``` 1352 | 1353 | ### Error Handling 1354 | 1355 | #### Basic 1356 | 101. **How do you handle file loading errors in OpenCV?** 1357 | Check if the image is loaded correctly to avoid crashes. 1358 | ```python 1359 | import cv2 1360 | img = cv2.imread('image.jpg') 1361 | if img is None: 1362 | raise FileNotFoundError("Image not found") 1363 | ``` 1364 | 1365 | 102. **What happens if you pass an invalid parameter to an OpenCV function?** 1366 | OpenCV raises exceptions (e.g., `cv2.error`). 1367 | ```python 1368 | import cv2 1369 | try: 1370 | img = cv2.imread('image.jpg') 1371 | cv2.resize(img, (0, 0)) # Invalid size 1372 | except cv2.error as e: 1373 | print("OpenCV error:", e) 1374 | ``` 1375 | 1376 | 103. **How do you handle video capture errors in OpenCV?** 1377 | Verify capture initialization and frame reading. 1378 | ```python 1379 | import cv2 1380 | cap = cv2.VideoCapture('video.mp4') 1381 | if not cap.isOpened(): 1382 | raise RuntimeError("Cannot open video") 1383 | ``` 1384 | 1385 | #### Intermediate 1386 | 104. **Write a function with error handling for image processing.** 1387 | Ensures robust processing pipelines. 1388 | ```python 1389 | import cv2 1390 | def process_image(img_path): 1391 | try: 1392 | img = cv2.imread(img_path) 1393 | if img is None: 1394 | raise FileNotFoundError("Image not found") 1395 | return cv2.GaussianBlur(img, (5, 5), 0) 1396 | except Exception as e: 1397 | print(f"Error: {e}") 1398 | return None 1399 | ``` 1400 | 1401 | 105. **How do you handle memory issues with large images in OpenCV?** 1402 | Use smaller resolutions or chunking to manage memory. 1403 | ```python 1404 | import cv2 1405 | try: 1406 | img = cv2.imread('large_image.jpg') 1407 | img = cv2.resize(img, (1000, 1000)) # Reduce size 1408 | except MemoryError: 1409 | print("Image too large") 1410 | ``` 1411 | 1412 | 106. **Implement a function to retry failed video frame reads.** 1413 | Retries handle transient errors in video streams. 1414 | ```python 1415 | import cv2 1416 | def read_frame_with_retry(cap, max_attempts=3): 1417 | for _ in range(max_attempts): 1418 | ret, frame = cap.read() 1419 | if ret: 1420 | return frame 1421 | raise RuntimeError("Failed to read frame") 1422 | ``` 1423 | 1424 | #### Advanced 1425 | 107. **Create a custom exception class for OpenCV errors.** 1426 | Defines specific errors for vision tasks. 1427 | ```python 1428 | class OpenCVError(Exception): 1429 | pass 1430 | def process_image(img_path): 1431 | img = cv2.imread(img_path) 1432 | if img is None: 1433 | raise OpenCVError("Failed to load image") 1434 | return img 1435 | ``` 1436 | 1437 | 108. **Write a function to handle cascading errors in a processing pipeline.** 1438 | Manages multiple potential failures. 1439 | ```python 1440 | import cv2 1441 | def process_pipeline(img_path): 1442 | try: 1443 | img = cv2.imread(img_path) 1444 | if img- is None: 1445 | raise OpenCVError("Image not found") 1446 | img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 1447 | edges = cv2.Canny(img, 100, 200) 1448 | return edges 1449 | except OpenCVError as e: 1450 | print(f"Pipeline error: {e}") 1451 | return None 1452 | except cv2.error as e: 1453 | print(f"OpenCV error: {e}") 1454 | return None 1455 | ``` 1456 | 1457 | 109. **Explain how to log errors in OpenCV applications.** 1458 | Use Python’s `logging` module to track issues in vision pipelines. 1459 | ```python 1460 | import cv2 1461 | import logging 1462 | logging.basicConfig(level=logging.ERROR) 1463 | try: 1464 | img = cv2.imread('image.jpg') 1465 | cv2.resize(img, (0, 0)) 1466 | except cv2.error as e: 1467 | logging.error(f"OpenCV error: {e}") 1468 | ``` 1469 | 1470 | ### Advanced Techniques 1471 | 1472 | #### Basic 1473 | 110. **What is image inpainting in OpenCV, and how is it used?** 1474 | Inpainting repairs damaged image regions, useful for preprocessing. 1475 | ```python 1476 | import cv2 1477 | img = cv2.imread('image.jpg') 1478 | mask = cv2.imread('mask.jpg', cv2.IMREAD_GRAYSCALE) 1479 | inpainted = cv2.inpaint(img, mask, 3, cv2.INPAINT_TELEA) 1480 | ``` 1481 | 1482 | 111. **How do you perform image stitching in OpenCV?** 1483 | Stitching combines images into panoramas. 1484 | ```python 1485 | import cv2 1486 | images = [cv2.imread(f'image{i}.jpg') for i in range(1, 3)] 1487 | stitcher = cv2.Stitcher_create() 1488 | status, pano = stitcher.stitch(images) 1489 | ``` 1490 | 1491 | 112. **Explain the `cv2.calcHist` function for histogram computation.** 1492 | Computes histograms for analyzing pixel distributions. 1493 | ```python 1494 | import cv2 1495 | img = cv2.imread('image.jpg') 1496 | hist = cv2.calcHist([img], [0], None, [256], [0, 256]) # Blue channel 1497 | ``` 1498 | 1499 | #### Intermediate 1500 | 113. **Write a function to perform image inpainting with a dynamic mask.** 1501 | Repairs specific regions based on user input. 1502 | ```python 1503 | import cv2 1504 | import numpy as np 1505 | def inpaint_image(img, points): 1506 | mask = np.zeros(img.shape[:2], np.uint8) 1507 | for (x, y) in points: 1508 | cv2.circle(mask, (x, y), 5, 255, -1) 1509 | return cv2.inpaint(img, mask, 3, cv2.INPAINT_NS) 1510 | img = cv2.imread('image.jpg') 1511 | points = [(100, 100), (150, 150)] 1512 | inpainted = inpaint_image(img, points) 1513 | ``` 1514 | 1515 | 114. **How do you use OpenCV for augmented reality?** 1516 | Overlay virtual objects using pose estimation and transformations. 1517 | ```python 1518 | import cv2 1519 | import numpy as np 1520 | img = cv2.imread('image.jpg') 1521 | obj_points = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 0]], dtype=np.float32) 1522 | img_points = np.array([[100, 100], [200, 100], [100, 200], [200, 200]], dtype=np.float32) 1523 | ret, rvec, tvec = cv2.solvePnP(obj_points, img_points, mtx, dist) 1524 | ``` 1525 | 1526 | 115. **Implement a function to compute and visualize image histograms.** 1527 | Analyzes color/intensity distributions. 1528 | ```python 1529 | import cv2 1530 | import matplotlib.pyplot as plt 1531 | def plot_histogram(img): 1532 | colors = ('b', 'g', 'r') 1533 | for i, col in enumerate(colors): 1534 | hist = cv2.calcHist([img], [i], None, [256], [0, 256]) 1535 | plt.plot(hist, color=col) 1536 | plt.savefig('histogram.png') 1537 | img = cv2.imread('image.jpg') 1538 | plot_histogram(img) 1539 | ``` 1540 | 1541 | #### Advanced 1542 | 116. **Write a function for real-time augmented reality with OpenCV.** 1543 | Overlays objects in video streams. 1544 | ```python 1545 | import cv2 1546 | import numpy as np 1547 | def ar_overlay(video_path, obj_img, obj_points, img_points, mtx, dist): 1548 | cap = cv2.VideoCapture(video_path) 1549 | while cap.isOpened(): 1550 | ret, frame = cap.read() 1551 | if not ret: 1552 | break 1553 | ret, rvec, tvec = cv2.solvePnP(obj_points, img_points, mtx, dist) 1554 | imgpts, _ = cv2.projectPoints(np.float32([[0, 0, 0]]), rvec, tvec, mtx, dist) 1555 | cv2.warpPerspective(obj_img, frame, imgpts) 1556 | cv2.imshow('AR', frame) 1557 | if cv2.waitKey(1) & 0xFF == ord('q'): 1558 | break 1559 | cap.release() 1560 | cv2.destroyAllWindows() 1561 | ``` 1562 | 1563 | 117. **Explain how to use OpenCV for SLAM (Simultaneous Localization and Mapping).** 1564 | SLAM combines feature detection, tracking, and pose estimation for robotic navigation. 1565 | ```python 1566 | import cv2 1567 | orb = cv2.ORB_create() 1568 | cap = cv2.VideoCapture(0) 1569 | while True: 1570 | ret, frame = cap.read() 1571 | if not ret: 1572 | break 1573 | kp, des = orb.detectAndCompute(frame, None) 1574 | # Process for SLAM 1575 | ``` 1576 | 1577 | 118. **Implement a function for depth estimation using stereo vision.** 1578 | Estimates depth from stereo image pairs. 1579 | ```python 1580 | import cv2 1581 | def depth_map(img1, img2): 1582 | stereo = cv2.StereoBM_create(numDisparities=16, blockSize=15) 1583 | disparity = stereo.compute(cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY), cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)) 1584 | return disparity / 16.0 1585 | img1 = cv2.imread('left.jpg') 1586 | img2 = cv2.imread('right.jpg') 1587 | depth = depth_map(img1, img2) 1588 | ``` 1589 | 1590 | ### Additional Coding Questions 1591 | 1592 | 119. **Write a function to detect and remove a green screen background.** 1593 | Chroma keying for video editing. 1594 | ```python 1595 | import cv2 1596 | import numpy as np 1597 | def remove_green_screen(img, lower_green=(0, 100, 0), upper_green=(100, 255, 100)): 1598 | hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 1599 | mask = cv2.inRange(hsv, lower_green, upper_green) 1600 | mask = cv2.bitwise_not(mask) 1601 | return cv2.bitwise_and(img, img, mask=mask) 1602 | img = cv2.imread('image.jpg') 1603 | result = remove_green_screen(img) 1604 | ``` 1605 | 1606 | 120. **Implement a function to compute image gradients using Sobel operators.** 1607 | Gradients highlight edges for feature extraction. 1608 | ```python 1609 | import cv2 1610 | def compute_gradients(img): 1611 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 1612 | sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=5) 1613 | sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=5) 1614 | return sobelx, sobely 1615 | img = cv2.imread('image.jpg') 1616 | grad_x, grad_y = compute_gradients(img) 1617 | ``` 1618 | 1619 | 121. **Write a function to detect text in an image using EAST detector.** 1620 | Text detection for OCR applications. 1621 | ```python 1622 | import cv2 1623 | import numpy as np 1624 | def detect_text(img, model_path): 1625 | net = cv2.dnn.readNet(model_path) 1626 | blob = cv2.dnn.blobFromImage(img, 1.0, (320, 320), (123.68, 116.78, 103.94), swapRB=True) 1627 | net.setInput(blob) 1628 | (scores, geometry) = net.forward(['feature_fusion/Conv_7/Sigmoid', 'feature_fusion/concat_3']) 1629 | return scores, geometry 1630 | img = cv2.imread('image.jpg') 1631 | scores, geometry = detect_text(img, 'east_text_detection.pb') 1632 | ``` 1633 | 1634 | 122. **Implement a function to create a mosaic effect on an image.** 1635 | Mosaics anonymize or stylize images. 1636 | ```python 1637 | import cv2 1638 | def mosaic_effect(img, block_size=10): 1639 | h, w = img.shape[:2] 1640 | for y in range(0, h, block_size): 1641 | for x in range(0, w, block_size): 1642 | block = img[y:y+block_size, x:x+block_size] 1643 | avg_color = block.mean(axis=(0, 1)).astype(np.uint8) 1644 | img[y:y+block_size, x:x+block_size] = avg_color 1645 | return img 1646 | img = cv2.imread('image.jpg') 1647 | mosaiced = mosaic_effect(img) 1648 | ``` 1649 | 1650 | 123. **Write a function to perform image blending using OpenCV.** 1651 | Blending combines images for effects or overlays. 1652 | ```python 1653 | import cv2 1654 | def blend_images(img1, img2, alpha=0.5): 1655 | return cv2.addWeighted(img1, alpha, img2, 1-alpha, 0.0) 1656 | img1 = cv2.imread('image1.jpg') 1657 | img2 = cv2.imread('image2.jpg') 1658 | blended = blend_images(img1, img2) 1659 | ``` 1660 | 1661 | 124. **Implement a function to detect and count objects in an image.** 1662 | Counts segmented objects for analysis. 1663 | ```python 1664 | import cv2 1665 | import numpy as np 1666 | def count_objects(img): 1667 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 1668 | _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) 1669 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 1670 | return len(contours) 1671 | img = cv2.imread('image.jpg') 1672 | count = count_objects(img) 1673 | ``` --------------------------------------------------------------------------------