0
0
Computer Visionml~15 mins

First image processing program in Computer Vision - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - First image processing program
Problem:You want to create a simple program that loads an image, converts it to grayscale, and shows the result. This helps you understand basic image processing.
Current Metrics:No current metrics because the program is not yet created.
Issue:No image processing done yet. Need to write code to load and transform the image.
Your Task
Write a program that loads a color image, converts it to grayscale, and displays both the original and grayscale images side by side.
Use Python with OpenCV library.
Do not use any advanced image processing techniques beyond grayscale conversion.
The program must run without errors and display images in windows.
Hint 1
Hint 2
Hint 3
Hint 4
Solution
Computer Vision
import cv2

# Load the color image
image = cv2.imread('sample.jpg')  # Replace 'sample.jpg' with your image file path

# Check if image loaded successfully
if image is None:
    print('Error: Image not found or unable to load.')
    exit()

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

# Show original image
cv2.imshow('Original Image', image)

# Show grayscale image
cv2.imshow('Grayscale Image', gray_image)

# Wait until a key is pressed
cv2.waitKey(0)

# Close all windows
cv2.destroyAllWindows()
Added code to load an image using OpenCV.
Converted the loaded image to grayscale.
Displayed both original and grayscale images in separate windows.
Added error handling if image file is not found.
Results Interpretation

Before: No image processing done, no output.

After: Program loads an image, converts it to grayscale, and shows both images. You see the original colors and the black-and-white version side by side.

This experiment shows how to perform basic image loading and transformation. Converting to grayscale is a simple but important step in many image processing tasks.
Bonus Experiment
Now try to resize the image to half its original size before converting to grayscale and displaying.
💡 Hint
Use cv2.resize() with appropriate width and height parameters before the grayscale conversion.