Jump to main content

Mars Terrain Exploration: A Hands-On Coding Investigation

Abstract

Have you ever wondered what the surface of Mars is really like close up? Are you wondering what the Mars rovers get to see and experience? Some parts of Mars look smooth and sandy, whereas others are covered in rocks and rough terrain. In this project, you’ll step into the role of a planetary scientist and use real Mars rover images and Python code to detect, measure, and compare rocks across different types of Martian terrain. Here you’ll explore how image processing can help scientists study planetary surfaces, learn how statistics can reveal patterns in rock size, and use data to make conclusions about Mars geology. With coding, visual comparisons, and real NASA images, this project gives you hands-on insight into how computer science can help us investigate another world.

Summary

Areas of Science
Difficulty
Method
Time Required
Short (2-5 days)
Prerequisites

None

Material Availability

Readily available

Cost
Very Low (under $20)
Safety

No issues

Credits
Hannah James, Cornell University
Science Buddies is committed to creating content authored by scientists and educators. Learn more about our process and how we use AI.

Objective

In this project, you’ll use Python and real Mars rover images to detect rocks, measure their size, and compare rock distributions across different terrain types. You’ll learn how image processing and statistics can be used to study planetary surfaces.

Introduction

Mars has long fascinated planetary scientists, astronomers, and the public alike. It has an incredibly diverse surface with some regions covered in smooth dust/sand, while others are covered with pebbles, stones, and larger boulders (Figure 1). By studying the size and distribution of rocks on Mars, scientists can learn more about the processes that shaped the planet over time, including wind erosion, impacts, volcanic activity, and sediment transport. 

Composite image of the Martian surface taken from the Pathfinder rover, showing the Sojourner roverImage Credit: NASA / Public Domain
Figure 1. A composite image taken from the Pathfinder rover showing the Sojourner rover in the foreground.

Today, much of what we know about the Martian surface comes from rover missions like NASA’s Perseverance rover. Perseverance takes thousands of photos as it explores the Jezero Crater, which gives scientists and engineers an up-close view of the ground. However, looking at these images one by one takes time, and it can be difficult to compare terrain consistently by eye alone. That is where computer science becomes especially useful. 

In this project, you’ll act like a planetary data scientist. Instead of measuring rocks by hand, you’ll use Python and computer vision tools to process rover images automatically. The code crops away the sky, converts the terrain image into greyscale, blurs the image to reduce noise, and then identifies dark regions that may represent rocks. It then measures the area of each detected rock-like region in pixels and compares those measurements across terrain groups. 

To do this, the code uses a few important ideas from image processing and statistics. First, the image is converted from color to grayscale, meaning that each pixel is represented by brightness rather than color. This makes it easier for the computer to compare light and dark regions. Next, the code applies a Gaussian blur, which smooths the image slightly and reduces random visual noise. Then, the code uses thresholding to separate darker areas from lighter background regions. In many terrain images, darker objects or shadows can help identify where rocks may be located. After thresholding, the code finds contours, which are outlines around connected regions in the image. These contours can be measured to estimate the size of each possible rock. The contour area is reported in square pixels. The code then compares rock sizes across terrain type using summary statistics like the mean, median, and standard deviation to describe the typical rock size, the spread of the data, and how much the measurements vary within each group. These statistics help reveal patterns that may not be obvious just by looking at the images. The project also uses a statistical test called the Mann-Whitney U test to determine whether the differences between terrain groups are likely to be meaningful rather than due to random chance.

In this project, you’ll use real Mars images, write and run code, compare results, and answer a big question: do different types of Martian terrain show different rock-size patterns?

Let’s find out!

Terms and Concepts

Questions

Bibliography

To learn more about Mars rovers, read the following:

To learn more about Mars’ terrain, read the following:

Materials and Equipment

Experimental Procedure

This project follows the Scientific Method. Review the steps before you begin.

Setting Up the Google Colab Environment

  1. You will need a Google account. If you don't have one, you can create one here.
  2. Download the code file and images from Science Buddies. This is the code you will run and the initial images you will analyze for this project. The images came from NASA’s publicly available Perseverance raw image archive.
  3. Extract the zipped folder. Inside the extracted folder, there should be a file called "mars_terrain.ipynb" and a folder called "images." Note: depending on your operating system, you may need to go down into a subfolder to find the ipynb file and the image folder.
  4. Within your Google Drive, click on ‘MyDrive,’ then create a new folder and rename it “Mars_Terrain”. Inside the folder, upload the mars_terrain.ipynb file and the "images" folder.
    1. Make sure the mars_terrain.ipynb file is directly inside the Mars_Terrain folder and not in a subfolder. 
    2. Make sure the image files are in a single subfolder called "images", not in the same top-level Mars_Terrain folder as the ipynb file or buried in multiple subfolders.
  5. Double-click on the mars_terrain.ipynb file. This should automatically open in Google Colab.

Previewing and Exploring the Images

After the necessary imports, the first part of the notebook loads all the image files from your Mars image folder and displays one example image after cropping away the top portion. By default, the code uses files[0], which selects the first image in the folder. If you want to visually inspect a different image, you can edit that line in the code. For example: example_img = cv2.imread(os.path.join(IMAGE_DIR, files[3])). This would display the fourth image instead. The full rock-detection analysis is then run on all images in the folder.

Cropping away to the top portion helps remove the sky and keep the analysis focused on the ground.

The notebook uses the following tools:

  • CROP_START controls how much of the top of the image is removed
  • MIN_AREA sets the minimum contour size to count as a rock
  • MAX_AREA sets the maximum contour size to avoid counting large false detections
  • THRESH_VAL is a fallback threshold value if automatic thresholding is not used

The first preview step helps you confirm that the code is looking at the terrain and not the sky or rover hardware. 

Detecting Rock Candidates Across All Images

The following cell processes all images together to explore the dataset before grouping them. For each image, the code:

  • Loads the image
  • Crops the top part away
  • Converts the image to grayscale
  • Applies a Gaussian blur
    • It is important to note that although the effect may be difficult to see in the example image, the Gaussian blur is still being applied. This step slightly smooths the image to reduce small pixel-level variations in brightness and helps reduce visual noise before thresholding.
  • Use thresholding to isolate darker regions
  • Finds contours
  • Measures each contour’s area
  • Keeps only contours within the size range you selected

The output of this code block is a large list of rock candidate areas measured in pixels. This step gives you an initial look at the entire dataset before you separate the images into terrain categories.

Visualizing the Overall Rock Size Distribution

The code then creates distribution plots of the detected rock sizes across all images.

The notebook displays:

  • A histogram on a linear scale and a log scale
  • Summary statistics including mean (average rock area), median (middle rock area), standard deviation (how spread out the rock sizes are), minimum (smallest detected rock area), and maximum (largest detected rock area).

This is helpful because rock sizes are often not evenly distributed. A few large detected regions can make the mean much larger than the median. Comparing the two helps you determine whether the image dataset is skewed.

Checking What the Algorithm is Detecting

After that, the code overlays contour outlines on a single cropped example image so you can visually inspect what the algorithm is counting as rocks. The detected rocks are outlined in green. The full rock-detection analysis is then run on all images in the folder. 

This is an important quality-control step as it lets you see whether the code is identifying actual rock-like regions or whether it may also be counting things such as shadows.

Before you start Task 1, look carefully at the example image overlay and ask:

  • Are most of the outlines actually around real rocks?
  • Are shadows being counted by mistake?
  • Are some small rocks being missed?

This will help you understand the strengths and limitations of this method.

Defining Your Terrain Groups

Task 1 asks you to sort your images into terrain groups. Look through the images in your folder and decide which terrain category best fits each image you plan to analyze. You do not need to study every image in equal detail, but you should review the full set and classify the images you include in your groups.

Suggested categories are:

  • Sandy: smooth terrain with relatively few visible rocks
  • Rocky: terrain covered with many distinct rocks
  • Mixed: terrain that shows both sandy and rocky features

Make sure to place at least three images into each group for more meaningful statistics. To do this, do not move the image files into new folders. Instead, look at the filenames of your images and type the filenames into the correct Python list in the notebook: sandy_images, rocky_images, and, if you are using it, mixed_images.

This code block introduces an important part of the investigation, which is classification. You decide which images belong in each terrain category based on visible features. In the notebook, you are also prompted to explain your reasoning for how you grouped the images.

Running the Analysis for Each Terrain Group

In the earlier code block, all images were processed together to give you an initial insight into the full dataset. In this step, you run the same analysis again, but now on your terrain groups separately. This allows the notebook to create separate rock-area datasets for sandy, rocky, and mixed terrain so that you can compare them.

Task 2 contains the extract_rock_areas() function, which applies the same image-processing steps to any list of images.

For each terrain group, the function:

  • Crops the ground region
  • Converts it to grayscale
  • Blurs the image
  • Thresholds the image
  • Finds contours
  • Filters them by area
  • Stores the remaining rock candidate areas

You then run the function separately for sandy, rocky, and mixed terrain images. The output tells you how many rock regions were detected in each group. 

Comparing Statistics Between Terrain Types

Task 3 summarizes the rock-size data for each terrain group. For each group, the notebook calculates:

  • Number of rocks detected
  • Mean rock area
  • Median rock area
  • Standard deviation
  • Minimum rock area
  • Maximum rock area

An additional output is the comparison of the sandy and rocky groups using a Mann-Whitney U test. This statistical test helps determine whether the difference between the two groups is likely to be real rather than random or coincidental. The notebook also calculates an effect size, which gives you a sense of how large that difference is. 

When you examine the results, you should think about the following:

  • Which group has the larger median rock area?
  • Which group has the larger mean?
  • Is the mean much larger than the median?
  • What does the p-value suggest?
  • Does the effect size support the same conclusion?

Creating Visual Comparisons

Task 4 asks you to create two visual comparisons:

  • A histogram showing the distribution of rock sizes for each terrain type
  • A box plot showing the median and spread for each group

You can also experiment with plotting the histogram on a log scale. In many rock-size datasets, a log scale makes the data clearer because there are many small rocks and fewer large ones. Make sure the graph titles and axis labels clearly describe what you are showing.

Answering the Reflection Questions 

Task 5 includes reflection questions that help you interpret the science behind your results. These questions ask you to think about:

  • Which terrain type had larger rocks overall?
  • Does the visual appearance match the data?
  • What do the p-value and effect size mean?
  • What Mars geological processes might explain the differences?
  • What sources of uncertainty may have affected the results?
  • How the project could be improved

This is where it all comes together, and you connect your coding results back to real planetary science. You can also extend this investigation by changing one image-processing setting and rerunning the analysis to see how your results change. For example, try switching between Otsu thresholding and a fixed threshold, or adjust the MIN_AREA and MAX_AREA settings to determine which contours count as rocks. Then compare how those choices affect the number of rocks detected, the summary statistics, and your conclusions about differences between terrain types.

icon scientific method

Ask an Expert

Do you have specific questions about your science project? Our team of volunteer scientists can help. Our Experts won't do the work for you, but they will make suggestions, offer guidance, and help you troubleshoot.

Variations

  • Add more terrain categories, such as crater-edge terrain or dust-covered terrain.
  • Compare images from different rover cameras, such as Navcam and Mastcam-Z. Use the camera filters on the left side of NASA’s publicly available Perseverance raw image archive page to select images from Navigation Camera-Left, Navigation Camera-Right, Mastcam-Z Left, or Mastcam-Z Right.
  • Test how changing MIN_AREA or MAX_AREA affects your results.
  • Compare manual rock counting with automated contour detection.
  • Use machine learning to classify rocks more accurately.
  • Try estimating real-world rock sizes using rover camera scale information.
  • Analyze whether lighting conditions or shadows affect the number of detected rocks.
  • How does distance from the camera affect the measured rock size? Is it possible that the code is detecting "small" rocks that are actually just large rocks farther away from the camera? How does the camera angle (i.e. looking out at the horizon vs. down at the ground) influence this?

Careers

If you like this project, you might enjoy exploring these related careers:

Career Profile
Astronomers think big! They want to understand the entire universe—the nature of the Sun, Moon, planets, stars, galaxies, and everything in between. An astronomer's work can be pure science—gathering and analyzing data from instruments and creating theories about the nature of cosmic objects—or the work can be applied to practical problems in space flight and navigation, or satellite communications. Read more
Career Profile
Many aspects of peoples' daily lives can be summarized using data, from what is the most popular new video game to where people like to go for a summer vacation. Data scientists (sometimes called data analysts) are experts at organizing and analyzing large sets of data (often called "big data"). By doing this, data scientists make conclusions that help other people or companies. For example, data scientists could help a video game company make a more profitable video game based on players'… Read more

News Feed on This Topic

 
, ,

Cite This Page

General citation information is provided here. Be sure to check the formatting, including capitalization, for the method you are using and update your citation, as needed.

MLA Style

James, Hannah. "Mars Terrain Exploration: A Hands-On Coding Investigation." Science Buddies, 6 May 2026, https://www.sciencebuddies.org/science-fair-projects/project-ideas/SpaceEx_p061/space-exploration/mars-terrain-rocks. Accessed 5 June 2026.

APA Style

James, H. (2026, May 6). Mars Terrain Exploration: A Hands-On Coding Investigation. Retrieved from https://www.sciencebuddies.org/science-fair-projects/project-ideas/SpaceEx_p061/space-exploration/mars-terrain-rocks


Last edit date: 2026-05-06
Top
Free science fair projects.