0
0
Computer Visionml~20 mins

Cropping images in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Cropping images
Problem:You have a set of images, but some contain unwanted borders or background. You want to crop these images to focus on the main subject.
Current Metrics:Images are processed without cropping, so irrelevant parts remain visible.
Issue:The images include unnecessary areas that can confuse a machine learning model or reduce visual clarity.
Your Task
Crop images to remove unwanted borders and focus on the main subject, improving image quality for further ML tasks.
Use Python and OpenCV or PIL libraries only.
Do not resize images, only crop.
Crop coordinates must be dynamically calculated based on image content.
Hint 1
Hint 2
Hint 3
Solution
Computer Vision
import cv2
import numpy as np

# Load image
image = cv2.imread('input.jpg')

# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Threshold to get binary image
_, thresh = cv2.threshold(gray, 10, 255, cv2.THRESH_BINARY)

# Find contours
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# Find bounding box around largest contour
if contours:
    largest_contour = max(contours, key=cv2.contourArea)
    x, y, w, h = cv2.boundingRect(largest_contour)
    cropped_image = image[y:y+h, x:x+w]
    # Save cropped image
    cv2.imwrite('cropped_output.jpg', cropped_image)
else:
    cropped_image = image
    cv2.imwrite('cropped_output.jpg', cropped_image)

print(f'Cropped image saved with shape: {cropped_image.shape}')
Added grayscale conversion and thresholding to isolate the main subject.
Used contour detection to find the largest object in the image.
Calculated bounding box coordinates dynamically based on the largest contour.
Cropped the image using these coordinates to remove unwanted borders.
Results Interpretation

Before: Images include full frame with unwanted borders or background.

After: Images are cropped tightly around the main subject, removing unnecessary parts.

Cropping images based on content helps focus on important parts, improving data quality for machine learning and reducing noise.
Bonus Experiment
Try cropping images automatically using a deep learning model like a pre-trained object detector to find the subject.
💡 Hint
Use models like YOLO or SSD to detect objects and crop images based on detected bounding boxes.