DEV Community

Irakoze Deborah
Irakoze Deborah

Posted on

My First Steps into Computer Vision: Teaching a Computer to Understand Images

My First Steps into Computer Vision: Teaching a Computer to Understand Images

Have you ever wondered how your phone recognises a face, how a self-driving car detects pedestrians, or how a computer can identify objects in a photograph?

These are examples of computer vision, a field of artificial intelligence that helps computers extract meaningful information from images and videos.

During my introduction to computer vision, I learned that computers do not see images the same way humans do. They process numerical data, discover patterns, and use those patterns to make predictions.

In this article, I will explore how computers represent images, how filters detect edges, how neural networks classify images, and how object detection can locate objects in a scene. I will also discuss some challenges that arise when applying computer vision to real-world problems.

  1. How Does a Computer See an Image?

When humans look at a photograph, we can immediately recognise colours, people, vehicles, and other objects. A computer, however, processes an image as a collection of pixels.

Each pixel contains numerical information. In a typical RGB colour image, each pixel has three values representing red, green, and blue intensity.

For example, the following Python code loads an image and examines its pixel values.

Python example: Reading image pixels
from PIL import Image
import numpy as np

Load an image

image = Image.open("my_image.jpg").convert("RGB")

Convert the image into a NumPy array

pixels = np.array(image)

Display image information

print("Image shape:", pixels.shape)
print("First pixel:", pixels[0, 0])
print("Minimum value:", pixels.min())
print("Maximum value:", pixels.max())

Install the required libraries using:

pip install pillow numpy

The image shape gives us its height, width, and colour channels. For example, an image with dimensions 600 Γ— 400 pixels and three colour channels contains 720,000 individual channel values.

This demonstrates an important concept: an image that looks simple to us can contain a large amount of numerical information.

  1. How Do Image Filters Find Edges?

Images contain different patterns, textures, and boundaries. Sometimes, we want to highlight particular features to make an image easier to analyse.

Image filters can help us reduce noise, smooth images, sharpen details, or detect edges.

One useful technique is edge detection. It identifies areas where pixel intensity changes significantly, helping reveal the boundaries of objects.

For example, edge detection can highlight the outline of a vehicle, a building, or a person.

Python example: Detecting edges

import cv2
import matplotlib.pyplot as plt

Load the image

image = cv2.imread("my_image.jpg")

if image is None:
raise FileNotFoundError("Image not found. Check the filename.")

Convert BGR to RGB for correct display

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Convert the image to grayscale

gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

Detect edges using the Canny algorithm

edges = cv2.Canny(gray, 100, 200)

Display the original image and its edges

plt.figure(figsize=(10, 4))

plt.subplot(1, 2, 1)
plt.imshow(image)
plt.title("Original Image")
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(edges, cmap="gray")
plt.title("Detected Edges")
plt.axis("off")

plt.tight_layout()
plt.show()

Install the required libraries:

pip install opencv-python matplotlib

This example shows how a computer can transform an ordinary photograph into a representation that highlights its boundaries.

  1. What Is a Convolutional Neural Network?

Image filters can reveal useful features, but modern computer vision also relies heavily on deep learning.

A Convolutional Neural Network (CNN) is a neural network architecture commonly used to process images. It learns patterns from training examples rather than depending entirely on manually designed rules.

Early layers may learn to respond to edges and simple textures. Deeper layers can combine these features into more complex patterns.

For example, a trained model may learn visual patterns that help distinguish a bus from a car.

We can experiment with a pretrained image-classification model using TensorFlow.

Python example: Classifying an image
import tensorflow as tf

Load a pretrained MobileNetV2 model

model = tf.keras.applications.MobileNetV2(
weights="imagenet"
)

Load and resize the image

image = tf.keras.utils.load_img(
"my_image.jpg",
target_size=(224, 224)
)

Convert the image to an array

image_array = tf.keras.utils.img_to_array(image)
image_array = tf.expand_dims(image_array, axis=0)

Prepare the image for MobileNetV2

image_array = tf.keras.applications.mobilenet_v2.preprocess_input(
image_array
)

Predict the image category

predictions = model.predict(image_array)

Display the three most likely categories

results = tf.keras.applications.mobilenet_v2.decode_predictions(
predictions,
top=3
)

for _, label, score in results[0]:
print(f"{label}: {score:.2%}")

Install TensorFlow using:

pip install tensorflow

The model uses pretrained ImageNet weights and returns its top three predicted categories with scores. Those scores are model outputs, not guarantees that a prediction is correct.

This experiment demonstrates how a pretrained model can use learned visual patterns to make predictions about an image.

  1. Classification, Object Detection, and Segmentation

Computer vision includes several tasks that may appear similar but serve different purposes.

Image classification

Classification predicts a category for an image or image region.

For example, a model might predict that a photograph contains a bus.

Object detection

Object detection identifies objects and estimates their locations, commonly using rectangular bounding boxes.

For example, a model might identify several vehicles and people in a street photograph.

Image segmentation

Segmentation assigns labels to individual pixels or regions, depending on the task. It can help distinguish the road from vehicles, pedestrians, and other parts of a scene.

These tasks answer different questions:

Classification: What is in the image?
Detection: What objects are present, and where are they?
Segmentation: Which pixels belong to each object or region?
Python example: Detecting objects with YOLO

YOLO is a family of object-detection models designed to identify objects and their locations in images.

from ultralytics import YOLO

Load a pretrained object-detection model

model = YOLO("yolo11n.pt")

Run detection on an image

results = model("my_image.jpg")

Display the detection results

results[0].show()

Print each detected object's label and confidence

for box in results[0].boxes:
class_id = int(box.cls[0])
confidence = float(box.conf[0])
label = model.names[class_id]

print(f"{label}: {confidence:.2%}")
Enter fullscreen mode Exit fullscreen mode

Install Ultralytics using:

pip install ultralytics

The pretrained model weights may download when you run the example for the first time.

Seeing those boxes makes the difference between classification and object detection easier to understand. The model does not simply predict what might be in the photograph; it also estimates where detected objects are located.

  1. Why Do Computer Vision Models Make Mistakes?

One important lesson is that computer vision models are not perfect.

A model might confuse objects that look similar, miss an object in poor lighting, or perform poorly on images that differ from its training data.

For example, an object detector trained on clear photographs may struggle with a blurry image taken at night.

Confidence thresholds also affect detection results. A lower threshold can produce more detections, including incorrect ones. A higher threshold can remove weak predictions but may also cause genuine objects to be missed.

This means that a model's output should be evaluated rather than accepted automatically.

When developing computer vision applications, it is important to test different conditions, examine incorrect predictions, and understand the limitations of the model.

  1. Why Does Local Data Matter?

Computer vision can support many useful applications in African communities.

In agriculture, models could help researchers identify plant diseases. In transportation, detection systems could help analyse traffic. In healthcare, computer vision could assist qualified professionals in examining medical images.

However, a model trained mainly on images from other regions may not perform equally well on local data.

Differences in lighting, road conditions, clothing, crop varieties, building styles, and camera quality can affect its predictions.

This makes local data collection and testing important. A model should be evaluated using representative examples from the environment where it will actually be used.

For sensitive applications such as healthcare, predictions also need appropriate professional oversight.

  1. What I Learned

My introduction to computer vision helped me connect several important ideas.

First, images can be represented as numerical arrays. Second, filters can highlight useful visual features. Third, CNNs learn patterns from data, while classification, detection, and segmentation solve different problems.

I also learned that computer vision is not only about building a model. It involves preparing data, testing predictions, identifying mistakes, and understanding how the system behaves in real-world situations.

The most interesting part is seeing how mathematical operations and code can turn image pixels into useful information.

Conclusion

Computer vision is an exciting field because it gives computers the ability to analyse images and videos in increasingly useful ways.

From examining individual pixel values to detecting objects with pretrained models, each step reveals another part of how machines process visual information.

There is still much to learn, especially about improving model accuracy, working with diverse datasets, and developing responsible applications.

I look forward to exploring more computer vision projects and understanding how these techniques can help solve practical problems in my community.

What would you choose for your first computer vision project: image classification, object detection, or segmentation? Share your thoughts in the comments!

Top comments (3)

Collapse
 
yvette_kazeneza_853bb8ef5 profile image
Yvette Kazeneza •

What’s good: I like how you break down computer vision concepts into simple explanations, especially the differences between classification, object detection, and segmentation. The Python examples also make the blog practical and beginner-friendly.

What could be improved: The blog would be even stronger with more visual results from the code, such as before-and-after images showing the effects of filters or examples of YOLO detecting objects. Briefly explaining the results and any challenges you encountered would also help readers connect the theory to real-world applications.

Overall, this is a good introduction to computer vision, and adding more practical demonstrations would make it even more engaging and informative!

Collapse
 
anaxelle profile image
Axelle Toni •

I appreciate that you mentioned computer vision models can make mistakes due to poor lighting, blurry images, and differences in training data. It would be interesting to see a small experiment showing how the same model performs on different images and why some predictions fail. That would make this section even more useful. Great job!

Collapse
 
marie_claireuwiringiyima profile image
Marie Claire Uwiringiyimana •

Great work! πŸ‘ This is a clear and insightful introduction to computer vision. It shows how pixels, filters, and AI models help computers understand images and solve real-world problems. Keep learning and building! πŸš€