OpenCV Tutorial for Image Processing and Computer Vision

OpenCV is an open-source computer vision library used for image processing, video analysis, object detection, feature extraction, camera input, and machine learning based vision tasks. This OpenCV tutorial introduces the main concepts with practical Python examples, so that you can start from reading an image and gradually move to real-time computer vision workflows.

The official OpenCV documentation is available at docs.opencv.org, and the project source code is available from github.com/opencv/opencv. Use this page as a guided starting point before moving into individual OpenCV functions and modules.

What you learn in this OpenCV tutorial

  • Install OpenCV for Python and verify the installed version.
  • Read, display, resize, crop, and save images using OpenCV.
  • Convert images between BGR, grayscale, RGB, HSV, and other color spaces.
  • Apply blur, thresholding, edge detection, and contour detection.
  • Capture frames from a video file or webcam.
  • Understand where OpenCV fits in real computer vision projects.

OpenCV installation for Python projects

For most beginner Python projects, install OpenCV using pip. The commonly used package is opencv-python. If you are working on a server without display support, use opencv-python-headless instead.

Command

</>
Copy
pip install opencv-python

After installation, import cv2 and print the OpenCV version.

</>
Copy
import cv2

print(cv2.__version__)

Example output

4.x.x

The exact version number depends on the package installed in your environment.

OpenCV image reading, display, and saving with cv2.imread()

The first step in most OpenCV programs is loading an image. OpenCV reads images as NumPy arrays. By default, color images are loaded in BGR channel order, not RGB.

Python Program – read_image.py

</>
Copy
import cv2

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

if image is None:
    print("Image not found")
else:
    cv2.imshow("Input Image", image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

cv2.imread() returns None when the file path is wrong or the image cannot be loaded. Always check this condition before processing the image.

To save an image, use cv2.imwrite().

</>
Copy
cv2.imwrite("output.jpg", image)

OpenCV image resize, crop, and shape basics

An OpenCV image is a NumPy array. For a color image, image.shape usually returns height, width, and number of channels. This is important because OpenCV size arguments often use (width, height), while array shape returns (height, width, channels).

Python Program – resize_crop.py

</>
Copy
import cv2

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

height, width, channels = image.shape
print("Width:", width)
print("Height:", height)
print("Channels:", channels)

resized = cv2.resize(image, (400, 300))
cropped = image[50:250, 100:350]

cv2.imwrite("resized.jpg", resized)
cv2.imwrite("cropped.jpg", cropped)

In the crop expression image[50:250, 100:350], the first range selects rows, and the second range selects columns. In image terms, that means image[y1:y2, x1:x2].

OpenCV color conversion from BGR to grayscale, RGB, and HSV

OpenCV loads color images in BGR format. Many plotting libraries expect RGB, and many image processing tasks work better after converting to grayscale or HSV.

Python Program – color_spaces.py

</>
Copy
import cv2

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

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

cv2.imwrite("gray.jpg", gray)
cv2.imwrite("hsv.jpg", hsv)

Use grayscale for operations such as thresholding, edge detection, and many contour workflows. Use HSV when you want to isolate objects based on color because hue is easier to separate than raw BGR channel values.

OpenCV thresholding for separating foreground and background

Thresholding converts a grayscale image into a binary image. Pixels above a selected threshold become one value, and pixels below it become another value. This is useful when objects have clear contrast from the background.

Python Program – threshold.py

</>
Copy
import cv2

image = cv2.imread("input.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

threshold_value, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

print("Threshold used:", threshold_value)
cv2.imwrite("binary.jpg", binary)

For images with uneven lighting, fixed thresholding may not work well. In such cases, adaptive thresholding or preprocessing with blur can produce better segmentation.

OpenCV blur and smoothing filters for noise reduction

Image smoothing reduces noise before applying edge detection, thresholding, or contour detection. OpenCV provides several filters, including average blur, Gaussian blur, and median blur.

Python Program – blur.py

</>
Copy
import cv2

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

average_blur = cv2.blur(image, (5, 5))
gaussian_blur = cv2.GaussianBlur(image, (5, 5), 0)
median_blur = cv2.medianBlur(image, 5)

cv2.imwrite("average_blur.jpg", average_blur)
cv2.imwrite("gaussian_blur.jpg", gaussian_blur)
cv2.imwrite("median_blur.jpg", median_blur)

Gaussian blur is often used before Canny edge detection. Median blur is useful when an image contains salt-and-pepper type noise.

OpenCV Canny edge detection example

Canny edge detection finds sharp intensity changes in an image. It is commonly used before shape detection, contour detection, and object boundary analysis.

Python Program – canny_edges.py

</>
Copy
import cv2

image = cv2.imread("input.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

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

cv2.imwrite("edges.jpg", edges)

The two threshold values in cv2.Canny() control which gradients are treated as edges. You may need to tune them for each image type.

OpenCV contour detection for object outlines

Contours are curves that join continuous boundary points. In OpenCV, contours are often found from a binary image or an edge image. They help in detecting object outlines, counting objects, and measuring shapes.

Python Program – contours.py

</>
Copy
import cv2

image = cv2.imread("input.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

_, binary = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

output = image.copy()
cv2.drawContours(output, contours, -1, (0, 255, 0), 2)

print("Number of contours:", len(contours))
cv2.imwrite("contours.jpg", output)

Use cv2.RETR_EXTERNAL when you only need outer contours. Use other retrieval modes when inner contours and hierarchy matter.

OpenCV webcam and video capture with cv2.VideoCapture()

OpenCV can process individual images, video files, or live camera frames. The cv2.VideoCapture() function opens a camera or video source and returns frames one by one.

Python Program – webcam.py

</>
Copy
import cv2

cap = cv2.VideoCapture(0)

if not cap.isOpened():
    print("Cannot open camera")
else:
    while True:
        ret, frame = cap.read()

        if not ret:
            print("Cannot receive frame")
            break

        cv2.imshow("Webcam", frame)

        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

cap.release()
cv2.destroyAllWindows()

Use 0 for the default camera. For a video file, pass the file path instead of 0. Press q in the display window to stop the loop in the example above.

OpenCV Python workflow for a small computer vision task

A practical OpenCV workflow is usually built as a sequence of small image operations. For example, to count simple high-contrast objects in an image, you may convert the image to grayscale, blur it, threshold it, find contours, and draw the detected outlines.

Python Program – object_outline_workflow.py

</>
Copy
import cv2

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

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
_, binary = cv2.threshold(blurred, 120, 255, cv2.THRESH_BINARY)

contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

result = image.copy()

for contour in contours:
    area = cv2.contourArea(contour)

    if area > 500:
        x, y, w, h = cv2.boundingRect(contour)
        cv2.rectangle(result, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.imwrite("detected_objects.jpg", result)
print("Detected objects:", len(contours))

This example is intentionally simple. Real images may need better lighting control, color filtering, morphology, calibration, or machine learning depending on the task.

Important OpenCV modules for beginners

OpenCV areaCommon functionsBeginner use case
Image input and outputimread(), imshow(), imwrite()Load, view, and save images
Image transformationresize(), rotate(), warpAffine()Change image size, direction, or perspective
Color conversioncvtColor()Convert BGR images to grayscale, RGB, or HSV
FilteringGaussianBlur(), medianBlur()Reduce noise before detection
Thresholding and edgesthreshold(), Canny()Separate regions and find boundaries
Contours and shapesfindContours(), drawContours(), boundingRect()Find object outlines and boxes
Video processingVideoCapture(), VideoWriter()Read camera frames and video files

Common OpenCV errors and practical fixes

  • cv2.imread() returns None: check the file path, file name, extension, and current working directory.
  • Image colors look wrong: remember that OpenCV uses BGR order by default. Convert to RGB when using libraries that expect RGB.
  • cv2.imshow() does not work on a server: use a local desktop environment or install/use a headless workflow that writes images to files instead.
  • Webcam does not open: check camera permissions, camera index, and whether another application is already using the camera.
  • Contour count is incorrect: improve thresholding, blur the image before detection, or filter contours by area.

OpenCV learning path after this tutorial

After learning the basics, continue with focused topics such as image filtering, morphological operations, template matching, feature detection, camera calibration, object tracking, face detection, optical flow, and deep learning inference with OpenCV DNN. A steady path is to master image arrays and preprocessing first, then move to detection and video workflows.

OpenCV tutorial FAQ

What is OpenCV used for?

OpenCV is used for image processing and computer vision tasks such as reading images, filtering noise, detecting edges, finding contours, processing video, detecting objects, and working with camera input.

Which language is best for starting OpenCV?

Python is the easiest language for most beginners because OpenCV Python examples are short and work well with NumPy. C++ is also widely used when performance and direct control are important.

Why does OpenCV use cv2 in Python?

The Python package is imported as cv2 for historical compatibility with the OpenCV Python bindings. Even in modern OpenCV versions, Python code normally starts with import cv2.

Why are my OpenCV image colors different from expected?

OpenCV reads color images in BGR order. If you display the same image with a library that expects RGB, colors may look swapped. Convert the image with cv2.cvtColor(image, cv2.COLOR_BGR2RGB) before using RGB-based display tools.

Can OpenCV process live webcam video?

Yes. OpenCV can capture live camera frames using cv2.VideoCapture(0). Each frame can be processed like a normal image inside a loop.

OpenCV tutorial editorial QA checklist

  • Check that all Python examples import cv2 before using OpenCV functions.
  • Verify that code using file input checks whether cv2.imread() returned None when appropriate.
  • Confirm that BGR, RGB, grayscale, and HSV explanations are not mixed up.
  • Make sure output-only blocks use the output class and new syntax-only blocks use a PrismJS language class with syntax.
  • Run added code snippets with sample images before publishing function-specific child tutorials.

OpenCV tutorial conclusion

This OpenCV tutorial covered the core workflow for Python computer vision: install OpenCV, read images, inspect image arrays, resize and crop images, convert color spaces, apply thresholding and blur, detect edges and contours, and capture webcam frames. These basics are the foundation for more specific OpenCV tutorials on object detection, face detection, video processing, and image analysis.