Tutorial: OpenCV Basics

Tutorial: OpenCV (C++) Basics

Tutorial: OpenCV (C++) Basics

Deep Learning Image Processing. Updated. 2024.3

Introduction

The OpenCV Library has >2500 algorithms, extensive documentation, and sample code for real-time computer vision. You can see basic information about OpenCV at the following sites,

In this tutorial, you will learn fundamental concepts of the C++ language to use the OpenCV API. You will learn namespace, class, C++ syntax to use image reading, writing and displaying.

Project Workspace Setting

Create the lecture workspace as C:\Users\yourID\source\repos

  • e.g. C:\Users\ykkim\source\repos

Then, create sub-directories such as :

  • C:\Users\yourID\source\repos\DLIP

  • C:\Users\yourID\source\repos\DLIP\Tutorial

  • C:\Users\yourID\source\repos\DLIP\Include

  • C:\Users\yourID\source\repos\DLIP\Assignment

  • C:\Users\yourID\source\repos\DLIP\LAB

  • C:\Users\yourID\source\repos\DLIP\Image

  • C:\Users\yourID\source\repos\DLIP\Props

Basic Image Processing

Example 1. Read / Write / Display

You can use the OpenCV C++ library to read, write, and display images/videos. Here is a related example.

You must Read Documentation!! link

  1. Configuration OpenCV 4.9.0 debug, release project property sheet. Link

  2. Download HGU logo image and rename HGU_logo.jpg

    • Image Link: HGU_logo

    • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

  3. Create a new C++ project in Visual Studio Community

    • Project Name: DLIP_Tutorial_OpenCV_Image

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  4. Create a new C+ source file

    • File Name: DLIP_Tutorial_OpenCV_Image.cpp or DLIP_Tutorial_OpenCV_Video.cpp

  5. Compile and run.

Basic Image Container: Mat Class

Mat Class

The image data are in forms of 1D, 2D, 3D arrays with values 0~255 or 0~1

OpenCV provides the Mat class for operating multi-dimensional images

Example 2. Matrix Operation: Create / Copy

Basic Image Operation: Crop, Rotate, Resize, Color Convert

The methods for performing tasks such as image crop, rotate, resize, and color conversion (such as converting to grayscale) are as follows. If you want to learn more about the functions below, refer to the OpenCV documentation.

Example 3. Basic Image Operation

Exercise 1

Flip horizontally, Rotate, Resize and Crop of the original image

Everytime you use OpenCV, you must use documentation!!** link

For flipping an image, find more details about cv::flip()

Crop (ROI) Image

Rotate (45deg CW) Image

Resize (by half) Image

  1. Configuration OpenCV 4.9.0 debug, release project property sheet. Link

  2. Download HGU logo image and rename HGU_logo.jpg

    • Image Link: HGU_logo

    • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

  3. Create a new C++ project in Visual Studio Community

    • Project Name: DLIP_Tutorial_OpenCV_EX1

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  4. Create a new C+ source file

    • File Name: DLIP_Tutorial_OpenCV_EX1.cpp

  5. Compile and run.

+Extra Exercise 1

The flip function is useful when working with videos. Implement a program that flips the webcam feed horizontally when the h key is pressed using waitKey() function. Hint: flag vs delay time of waitKey

Shallow Copy vs Deep Copy

Shallow Copy

Shallow Copy means copying only the memory addresses in the memory. Since it copies pointers pointing to the same object or data, the original and the copy end up sharing the same data. This can lead to issues, as modifications to one object or array will affect the other as well.

Deep Copy

Deep Copy means creating a copy of an object or data in a new memory space. The original and the copy are independent, having separate memory spaces, so modifications made to one side do not affect the other.

Example 4. Shallow_Deep_Copy

  • Compile and run the code below and see what happens

  • Before you execute this code, try to understand what it does

Accessing Pixel value

An image is composed of small units called pixels. Each pixel can be considered as the smallest unit of an image. Pixel intensity represents the brightness of a pixel. For grayscale images, pixel intensity ranges from 0 (black) to 255 (white). In color images, each channel (e.g., Red, Green, Blue) has its intensity value.

Rows and columns define an image's structure. Rows represent the vertical direction of the image, and columns represent the horizontal direction. The position of a pixel is denoted as (row, column) or (v, u), where v represents the row index and u represents the column index.

OpenCV provides different methods to access the intensity values of pixels in an image. Two common methods are using at<type>(v, u) and using pointers for faster operations.

Method 2. Using Pointer for faster operation

Example 5. Access pixel intensity of Gray-Scale Image(1D image)

Exercise 2

Calculate the average intensity value using at<type>(v,u)

Calculate the summation of the pixel intensity and calculate the average intensity value. Use cv::Mat::rows, cv::Mat::cols.

You must Read Documentation!! link

  1. Configuration OpenCV 4.9.0 debug, release project property sheet. Link

  2. Download HGU logo image and rename HGU_logo.jpg

    • Image Link: HGU_logo

    • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

  3. Create a new C++ project in Visual Studio Community

    • Project Name: DLIP_Tutorial_OpenCV_EX2

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  4. Create a new C+ source file

    • File Name: DLIP_Tutorial_OpenCV_EX2.cpp

  5. Compile and run.

Result

Exercise 3

Intensity Inversion in Grayscale Images

Write a code to invert the colors of this Grayscale image. The resulting image should look like the following. For example, a pixel with an intensity of 100 should become a value of 255 - 100, which is 155 after the color inversion. Use Mat::zeros, .at<type>(v,u)

You must Read Documentation!! link

  1. Configuration OpenCV 4.9.0 debug, release project property sheet. Link

  2. Download HGU logo image and rename HGU_logo.jpg

    • Image Link: HGU_logo

    • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

  3. Create a new C++ project in Visual Studio Community

    • Project Name: DLIP_Tutorial_OpenCV_EX3

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\

  4. Create a new C+ source file

    • File Name: DLIP_Tutorial_OpenCV_EX3.cpp

  5. Compile and run.

Result

Assignment (1 week)

Creating a new dataset of training images

In pre-processings of generating datasets of images, we usually maintain the same image size as the original but apply various geometric transformations such as cropping, translation, and resizing.

Write a code to create the following image datasets from the original image (464x480 px).

  • All three output images for this assignment must be the same sized(464x480 px).

  • Try to use pixel accesssing to create output images.

  • HINT: Use the outputs from the previous Exercises.

Assignment Setup

  • Project Name: DLIP_Assignment_OpenCV_Basics

  • Project Folder: ~\DLIP\Assignment\

  • File Name: DLIP_Assignment_OpenCV_Basics.cpp

Submit

Last updated

Was this helpful?