Homework 01: Working with Images

Set up the course cv environment and practice basic image operations with OpenCV and NumPy.

Total: 5 points
Submission: Gradescope Programming Assignment
Work: Individual

Objective

The objective of this homework is to verify that your course cv Conda environment is working and to practice basic image operations with OpenCV and NumPy.

For this course, use OpenCV (cv2) for image loading and image processing. Do not use skimage, PIL/Pillow, or another image-processing library to implement the required operations in this homework.

Do not use generative AI to solve this homework. The purpose of this first assignment is to make sure you can work directly with the course Python environment and basic image-processing operations.

Personal image requirement

Use a personal headshot or selfie in which your face is clearly visible. Your original color photo is not submitted to Gradescope. Instead, your program will save a grayscale result called hw01_output.png, and that grayscale result will be checked manually.

Starter file

Complete the provided hw01.py file. Do not rename the required functions or change their arguments because the Gradescope autograder calls them directly.

Download hw01.py

Required tasks

Complete the following functions:

  1. load_image(filename)
    • Load the image from disk using OpenCV.
    • Return the loaded color image as a NumPy array.
  2. convert_to_grayscale(image)
    • Convert the OpenCV color image to grayscale using OpenCV.
    • Return a 2-D NumPy array.
    • Convert the result to numpy.float32.
    • Scale the grayscale values to the range [0, 1].
  3. top_left_3x5(gray_image)
    • Return the pixel values from the top-left corner as a matrix with exactly 3 rows and 5 columns.
  4. get_pixel_value(gray_image, row, col)
    • Return the requested grayscale pixel value on row 1, column 2, using standard NumPy indexing.
    • Row/column indexing starts at 0.
  5. save_grayscale_image(gray_image, output_path)
    • Save a viewable grayscale image using OpenCV.
    • The program must produce hw01_output.png.

Run your program

Activate the course environment and run the script with your own image:

conda activate cv
python hw01.py my_image.png

You may use JPG/JPEG or PNG. If your phone stores images as HEIC, first convert the image to JPG or PNG.

A successful run must:

  • load your personal headshot;
  • convert it to grayscale float32 in the [0, 1] range;
  • print the top-left 3-row × 5-column grayscale matrix;
  • print the grayscale pixel value at row 1, column 2 using zero-based indexing; and
  • save the grayscale image as hw01_output.png.

Before submitting, open hw01_output.png and make sure your face is clearly visible and the image is grayscale.

What to submit

Submit exactly these two files to Gradescope:

  1. hw01.py
  2. hw01_output.png

Do not submit a PDF, screenshots of your code, or your original color headshot.

Submission limit — important

You may make at most 3 Gradescope submissions for this homework. Use your local cv environment to test and debug your code before submitting.

  • Submissions 1–3 are graded normally.
  • Submission 4 and every later submission receive a score of 0 for the homework.
  • A Gradescope infrastructure failure marked as an autograder error does not count against the three-submission limit.

Grading

The homework is worth 5 points:

  • 4.0 points: automatic programming tests.
  • 1.0 point: manual verification that hw01_output.png is the grayscale result of your personal headshot/selfie and that your face is clearly visible.

The autograder uses instructor-provided images and arrays in addition to your submitted output. Do not hard-code values from your personal image.

Some basic tests are public and provide feedback when you submit. More substantive correctness tests are private. Passing all public tests does not guarantee full credit.

Notes

  • Use the course cv Conda environment.
  • Use OpenCV (cv2) for the required image operations.
  • Do not change the required function names or arguments.
  • Do not hard-code the 3×5 matrix or the row-1, column-2 pixel value.
  • Test locally before using one of your three Gradescope submissions.