[Getting Started with OpenCV] Some basic image processing


My personal blog: Mou Ren·Blog
Wechat Public Account: Mou Ren's Sack


some concepts

color space

In order to represent a specific color, we usually establish one-dimensional, two-dimensional, three-dimensional or even four-dimensional space coordinates to represent, and the color range that this coordinate system can define is the color space. Common color spaces are listed below:

  • RGB model
    The RGB model encodes colors into (R, G, B) that is (Red red, Green green, Blue blue). The range of each component is [0,255] with a total of 256 levels. Therefore, the RGB model can represent 256×256×256≈16.7 million colors.
    Note: The storage order in OpenCV is not RGB, but BGR.

  • Single-channel model
    A single-channel image is a grayscale image. The pixel color of the image is represented by a one-dimensional value, and the component range is [0,255]. 0 is black, 255 is white, and there are varying degrees of gray in between.

  • Binary model
    Binary image is a black and white image, the color of each pixel is represented by a one-dimensional value, the component range is [0,1], 0 represents black, 1 represents white.

  • HSV model
    The HSV model represents color by three parameters: hue (H), saturation (S), and lightness (V).
    Hue (H) is represented by angle, the range is [0°,360°], red is 0°, and it is calculated counterclockwise from red. (As shown in the figure below)
    Saturation (S) indicates the degree to which the color is close to the spectral color. The higher the saturation, the darker and brighter the color. The value range is [0%, 100%].
    Brightness (V) indicates the brightness of the color, the value range is [0%, 100%], 0% is black, 100% is white.
    HSV model illustration

  • The L component in the Lab color space of the Lab model
    is used to represent the brightness of the pixel, and the value range is [0,100], representing from pure black to pure white; a represents the range from red to green, and the value range is [127,-128 ]; b represents the range from yellow to blue, and the value range is [127,-128]. The diagram is as follows:
    Lab model

image depth

The value of the image used to store the pixel occupies a number of bits (bits) (that is, the number of bits converted to binary), which is the depth of the image. For example: in the binary model, the value of each pixel is one-dimensional, and the value range is [0,1], so it can be satisfied with only one bit, and the image depth is 1; in the RGB model, each The pixel is represented by three components of R, G, and B. Each component has a value range of [0,255] and can be represented by 8 bits, so the pixel depth is 24 bits in total.

image channel

In order to store pixel data, each pixel uses one or more components to store data, and the number of components is the image channel. For example, in the RGB mode, each pixel is represented by three components of Red, Dreen, and Blue, each value range is [0,255], and this image channel is 3. The grayscale image is represented by only one value with a range of [0,255], and the image channel of the grayscale image is 1.

Color space conversion - cvtColor

function cvtColor

The function of cv::cvtColor() is to convert an image from one color space to another. And the data type can be guaranteed to remain unchanged during the conversion process, that is, the data type and bit depth of the converted image are consistent with the source image.
Function definition:

void cv::cvtColor(
      cv::InputArray src, // 输入图像
      cv::OutputArray dst, // 输出图像
      int code, // 模式转换的映射码
      int dstCn = 0 // 输出的通道数 (0='automatic')
     );
  • src: Mat class, input the image to be converted
  • dst: Mat class, the new image after conversion is placed here
  • code: The converted code or logo, that is, the image converted format (mode 1→mode 2)
    Mapping provided by OpenCV:
  • dstCn: (default is 0) the number of target image channels, if the value is 0, it is determined by src and code

Example (BGR converted to grayscale image)

source code:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world453d.lib")
#else
#pragma comment(lib,"opencv_world453.lib")
#endif // _DEBUG

int main()
{
    
    
    Mat img = imread("D:\\My Bags\\图片\\Test.jpg");
    Mat imgGray;
    cvtColor(img, imgGray, COLOR_BGR2GRAY);
    imshow("原图", img);
    imshow("灰度图", imgGray);
    waitKey(0);
    return 0;
}

operation result:

Gaussian filter-GaussianBlur

FunctionGaussianBlur

Function definition:

void GaussianBlur( InputArray src, //输入的图像
				   OutputArray dst, //接收输出的图像
				   Size ksize,//高斯内核大小
                   double sigmaX, 
                   double sigmaY = 0,
                   int borderType = BORDER_DEFAULT);
  • src: Mat class, input image to be processed
  • dst: Mat class, output an image with the same size and type as the original image
  • ksize: The size of the Gaussian kernel determines the degree of filtering. Where ksize.width and ksize.height can be different, but they must be positive and odd. Alternatively, they can be zero, both of which are calculated from sigma
  • sigmaX: the standard deviation of the Gaussian kernel function in the X direction
  • sigmaY: The standard deviation of the Gaussian kernel function in the Y direction. If sigmaY is 0, the function will automatically set the value of sigmaY to the same value as sigmaX. If both sigmaX and sigmaY are 0, these two values ​​will be determined by ksize.width Calculated from ksize.height
  • borderType: A convenient mode to infer the pixels outside the image, with a default value of BORDER_DEFAULT, do not change if there is no special need

example

source code:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world453d.lib")
#else
#pragma comment(lib,"opencv_world453.lib")
#endif // _DEBUG

int main()
{
    
    
    Mat img = imread("D:\\My Bags\\图片\\Test.jpg");
    Mat imgGaus;
    GaussianBlur(img, imgGaus, Size(7, 7), 7);
    imshow("原图", img);
    imshow("高斯滤波图", imgGaus);
    waitKey(0);
    return 0;
}

operation result:

Image Edge Detection - Canny

Function Canny

function definition

Overload 1:

void Canny( InputArray image,//输入图像
		    OutputArray edges,//输出图像
            double threshold1, //阈值1
            double threshold2,//阈值2
            int apertureSize = 3, //Sober算子大小
            bool L2gradient = false //是否采用更精确的方式计算图像梯度
            );

Two threshold parameters:

  • Pixels below the threshold 1 are considered not to be edges;
  • Pixels above threshold 2 are considered edges;
  • Pixels between threshold 1 and threshold 2 are considered to be edges if they are adjacent to the edge pixels obtained in step 2, otherwise they are not considered to be edges
  • It is recommended that the upper limit be 2 or 3 times the lower limit

apertureSize parameter:

  • Provide kernel size for Sobel() operation, default value is 3

overload 2:

void Canny( InputArray dx, 
			InputArray dy,
            OutputArray edges,//输出图像
            double threshold1, //下阈值
            double threshold2,//上阈值
            bool L2gradient = false 
            );
  • InputArray dx: The derivative of the input image in the x direction
  • InputArray dy: The derivative of the input image in the y direction

example

source code

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world453d.lib")
#else
#pragma comment(lib,"opencv_world453.lib")
#endif // _DEBUG

int main()
{
    
    
    Mat img = imread("D:\\My Bags\\图片\\Test.jpg");
    Mat imgGaus,imgEdge;
    GaussianBlur(img, imgGaus, Size(3, 3), 3);//高斯滤波降噪
    Canny(imgGaus, imgEdge, 100, 300);//边缘检测
    imshow("原图", img);
    imshow("边缘图", imgEdge);
    waitKey(0);
    return 0;
}

operation result:

Dilation and erosion of images - dilate and erode

function dilate/erode

Definition of the dilate function

void dilate( InputArray src, 
			 OutputArray dst, 
			 InputArray kernel,
             Point anchor = Point(-1,-1), 
             int iterations = 1,
             int borderType = BORDER_CONSTANT,
             const Scalar& borderValue = morphologyDefaultBorderValue() 
             );

Definition of erode function

void erode( InputArray src, 
            OutputArray dst, 
            InputArray kernel,
            Point anchor = Point(-1,-1), 
            int iterations = 1,
            int borderType = BORDER_CONSTANT,
            const Scalar& borderValue = morphologyDefaultBorderValue() 
            );

The two function parameters are exactly the same, parameter explanation:

  • src: Mat class, the original input image
  • dst: Mat class, used to store the processed image
  • kernel: The structural element used for expansion/erosion operations. When it is NULL, a 3 x 3 square structural element is used by default. You can use getStructuringElement() (described in detail below) to create a structural element
  • anchor: the anchor point position of the structural element, the default value value (-1,-1) indicates that the anchor point is located at the center of the structural element
  • iterations: the number of iterative executions for dilation/erosion operations, the default is 1
  • borderType: some inference mode for inferring pixels outside the image. Default value BORDER_CONSTANT
  • Scalar& borderValue: border value, default value morphologyDefaultBorderValue(). Generally do not need to change

Supplement: function getStructuringElement

Function definition:

Mat getStructuringElement(int shape, 
						  Size ksize, 
						  Point anchor = Point(-1,-1)
						  );

Function: Return (Mat type) the structural element of the specified shape and size

Parameter explanation:

  • shape: the shape of the structural element
    rectangle: MORPH_RECT;
    cross: MORPH_CROSS;
    ellipse: MORPH_ELLIPSE;
  • ksize: the size of the structural element, the same as the ksize of the GaussianBlur function
  • anchor: the position of the anchor point, the default value is Point (-1,-1), indicating that the anchor point is at the center point

example

source code:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
#ifdef _DEBUG
#pragma comment(lib,"opencv_world453d.lib")
#else
#pragma comment(lib,"opencv_world453.lib")
#endif // _DEBUG

int main()
{
    
    
    Mat imgDilate, imgErode;
    Mat img = imread("D:\\My Bags\\图片\\Test.jpg");
    Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));//新建一个结构元素
    dilate(img, imgDilate, kernel);//膨胀操作
    erode(img, imgErode, kernel);//侵蚀操作
    imshow("原图", img);
    imshow("膨胀图", imgDilate);
    imshow("侵蚀图", imgErode);
    waitKey(0);
    return 0;
}

operation result:

Guess you like

Origin blog.csdn.net/ZBC010/article/details/120572996
Recommended