Tutorial: OpenCV (C++) 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,
Homepage: https://opencv.org
Documentation: https://docs.opencv.org
Source code: https://github.com/opencv
Tutorial: https://docs.opencv.org/master
Books: https://opencv.org/books
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
Configuration OpenCV 4.9.0 debug, release project property sheet. Link
Download HGU logo image and rename HGU_logo.jpg
Image Link: HGU_logo
Image Folder:
C:\Users\yourID\source\repos\DLIP\Image\
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_OpenCV_Image
Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_OpenCV_Image.cpp
orDLIP_Tutorial_OpenCV_Video.cpp
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 of the original image
Here's the code to flip the original HGU_logo image horizontally using the OpenCV flip function. Please refer to the documentation below to find more details about cv::flip()
You must Read Documentation!! link
Configuration OpenCV 4.9.0 debug, release project property sheet. Link
Download HGU logo image and rename HGU_logo.jpg
Image Link: HGU_logo
Image Folder:
C:\Users\yourID\source\repos\DLIP\Image\
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_OpenCV_EX1
Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_OpenCV_EX1.cpp
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 1. Accessing using at<type>(v,u)
(Recommended)
at<type>(v,u)
(Recommended)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)
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
Configuration OpenCV 4.9.0 debug, release project property sheet. Link
Download HGU logo image and rename HGU_logo.jpg
Image Link: HGU_logo
Image Folder:
C:\Users\yourID\source\repos\DLIP\Image\
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_OpenCV_EX2
Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_OpenCV_EX2.cpp
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
Configuration OpenCV 4.9.0 debug, release project property sheet. Link
Download HGU logo image and rename HGU_logo.jpg
Image Link: HGU_logo
Image Folder:
C:\Users\yourID\source\repos\DLIP\Image\
Create a new C++ project in Visual Studio Community
Project Name:
DLIP_Tutorial_OpenCV_EX3
Project Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\
Create a new C+ source file
File Name:
DLIP_Tutorial_OpenCV_EX3.cpp
Compile and run.
Result
Last updated