Brief description of OpenCV functions_Chapter 1 Basic concepts of digital images (neighborhood, connectivity, color space)

Series Article Directory

  1. Brief description of OpenCV functions_Chapter 1 Basic concepts of digital images (neighborhood, connectivity, color space) [this article]
  2. Brief description of OpenCV functions_Chapter 2 Basic operations of digital images (image reading and writing, image pixel acquisition, image ROI acquisition, image mixing, graphics drawing)
  3. Brief description of OpenCV functions_Chapter 3 Filtering processing of digital images (box, mean, Gaussian, median and bilateral filtering)
  4. Brief description of OpenCV functions_Chapter 4 Morphological processing of digital images and image pyramids. (erosion, dilation, opening and closing operations, morphological gradients, top and black hats, and image pyramids)


foreword

This series of articles is only to record the relevant parameters and simple usage of commonly used OpenCV functions during the process of learning OpenCV, so as to facilitate query and use at any time. The specific algorithm principle will not be introduced too much.
本文C++使用的OpenCV版本是的4.5.5。
python使用的OpenCV版本是4.5.3.56
官方文档参考的是 4.6.0-dev


Contents of this article :
This article briefly introduces the relevant concepts of digital image processing, paving the way for subsequent content.

Reference blog:

  1. Take you to quickly learn RGB, HSV and HSL color spaces in three minutes
  2. Six major color spaces of digital image processing
  3. Baidu Encyclopedia_RGB color space
  4. Adjacency, Connectivity, Regions and Boundaries of Images
  5. 【CV Series】Basics of Digital Image Processing: Neighborhood and Connectivity

Reference books:

  1. Gonzalez - Digital Image Processing

Chapter 1 Basic Concepts of Digital Image

1.1 What is digital image processing

An image can be defined as a two-dimensional function f ( x , y ) f(x,y)f(x,y ) , wherexxxyyy is a space (plane) coordinate, and in any pair of space coordinates( x , y ) (x,y)(x,The magnitude ffat y )f is called the intensity or grayscale of the image at that point. whenxxx ,yyy and gray valuefff is a finite discrete value, we call the image a digital image. The methods and technologies of removing noise, enhancing, restoring, segmenting, and extracting features from images by computers are called digital image processing.

1.2 Image representation

An image can be defined as a two-dimensional function f ( x , y ) f(x,y)f(x,y ) . wherexxx is the space abscissa,yyy is the space vertical coordinate. We usually set the starting point to the upper left corner. So we can express a picture in the form of a matrix as shown in the figure below. whereMMM refers to the number of rows of the picture,NNN refers to the number of columns of the picture, so the picture is aM × NM \times NM×matrix of N , ffThe f values ​​are the pixel values ​​on the corresponding coordinates. This figure is excerpted from the Gonzalez version of digital image processing formula 2 _ 4 _ 1 2_\_4\_12_4_1
insert image description here

1.3 Spatial and grayscale resolution

1.3.1 Spatial resolution

Spatial resolution is a measure of the smallest details that can be discerned in an image. That is, the sampling resolution of the digital image. Quantitatively, spatial resolution can be expressed in many ways. Among them, the number of line pairs per unit distance and the number of points (pixels) per unit distance are the most common measures. Dots per unit distance is the most commonly used measure of image resolution in the printing and publishing industry. In the United States, this measurement is usually expressed in dots per inch (dpi). The larger the resolution, the finer the image display. The clock shown in the picture below is from Gonzales Digital Image Processing - Figure 2.20

DPI refers to pixels per inch, which is the scanning accuracy. The lower the DPI, the lower the resolution of the scan. Due to the influence of network transmission speed, the pictures used on the web are all 72dpi, but this parameter cannot be used for developing photos, it must be 300dpi or higher 350dpi. For example to flush 4 × 6 4\times64×For a 6 -inch photo, the scanning accuracy must be 300dpi, then the file size should be (4 × 300 4\times3004×300) × \times ×( 6 × 300 6\times300 6×300)= 1200 × 1800 1200\times1800 1200×1800 pixels. The same is true for the number of points per unit distance (unit: mm).

Please add a picture description

1.3.2 Gray resolution

Grayscale resolution refers to the smallest discernible change in grayscale. That is, the quantization resolution of the digital image. Based on hardware considerations, as mentioned in the previous section, the number of gray levels is usually an integer power of 2 . The most common number is 8 bits (indicating that the range of gray scale is 0-255), and in some special image enhancement applications, it is also necessary to use 16 bits. Systems are sometimes found that use 10 or 12 bits to digitize the gray scale of an image, but these are the exception rather than the rule. Unlike spatial resolution which has to be meaningful on a per unit distance basis, grayscale resolution refers to the number of bits used to quantize the grayscale. As an example, the images below show images of human skulls at different pixel levels, the images are from Gonzalez digital image processing Figure 2.21 .
Please add a picture description

1.4 Relationship between pixels

1.4.1 Adjacency

Suppose coordinates ( x , y ) (x,y)(x,pixel ppat y )p has 4 adjacent pixels horizontally and vertically, the coordinates are:

( x + 1 , y ) , ( x − 1 , y ) , ( x , y + 1 ) , ( x , y − 1 ) (x+1,y),(x-1,y),(x, y+1),(x,y-1)(x+1,y)(x1,y)(x,y+1)(x,y1)

Then this group of pixels is called ppThe 4 neighbors of p (not includingppp ), withN 4 ( p ) N4(p)N 4 ( p ) represents. Each pixel distance( x , y ) (x,y)(x,y ) a unit distance, if( x , y ) (x,y)(x,y ) lies on the boundary of the image, thenppSome neighboring pixels of p are located outside the digital image. As shown in the figure below:
N4(p)
ppThe coordinates of the 4 diagonally adjacent pixels of p are:

( x + 1 , y + 1 ) , ( x + 1 , y − 1 ) , ( x − 1 , y + 1 ) , ( x − 1 , y − 1 ) (x+1,y+1),( x+1,y-1),(x-1,y+1),(x-1,y-1)(x+1,y+1)(x+1,y1)(x1,y+1)(x1,y1)

And with ND ( p ) ND(p)ND ( p ) means . N 4 ( p ) N4(p)N 4 ( p ) andND (p) ND(p)N D ( p ) together are called the 8 neighbors of p, useN 8 ( p ) N8(p)N 8 ( p ) said. Likewise, these adjacent pixels may fall outside the image. As shown below:
ND(p)
N8(p)
注:以上概念为邻域,不是像素邻接。

1.4.2 Adjacency

Order VVV is a set of gray values ​​used to define adjacency. Gray value setVVV refers to any subset of 0-255.
At this time, there are
4 adjacencies:pixelppp andqqThe gray value of q belongs to the set VVV , pixelqqq inN 4 ( p ) N_4 (p)N4( p ) . For example, the following figure

8 is adjacent:pixelppp andqqThe gray value of q belongs to the set VVV , pixelqqq inN 8 ( p ) N_8 (p)N8( p ) . For example, the following figure

m-adjacency: To resolve the ambiguity of 8-adjacency.
If one of the following two conditions is met, it is mmm adjacency

forVVV is assumed to beppp q q If two points q are adjacent to m, one of the following two conditions must be met:
1)qqq inppIn the 4 neighborhood of p .
2)qqq inppin the diagonal field of p , and qqq 's 4-neighborhood andppThe 4 fields of p intersect as the empty set (no point of intersection belongs toVVV ).
m-adjacency overcomes the ambiguity of 8-adjacency. As shown in the figure below,V = { 1 } V=\{1\}V={ 1 } :
insert image description here
Pay attention to the difference between the concept of neighborhood and adjacency.

1.4.3 Connectivity

1.4.3.1 Passageway

Definition:
Pixel p ( x , y ) p(x,y)p(x,y ) to pixelq ( s , t ) q(s,t)q(s,t ) , consists of a series of coordinates( x 0 , y 0 ) (x_0, y_0)(x0,y0), ( x 1 , y 1 ) (x_1, y_1) (x1,y1),…, ( x i , y i ) (x_i, y_i) (xi,yi),…, ( x n , y n ) (x_n, y_n) (xn,yn) of independent pixels.
where( x , y ) = ( x 0 , y 0 ) (x,y)=(x_0, y_0)(x,y)=(x0,y0) ( x n , y n ) = ( s , t ) (x_n, y_n)=(s,t) (xn,yn)=(s,t),且 ( x i , y i ) (x_i, y_i) (xi,yi) ( x i − 1 , y i − 1 ) (x_{i-1}, y_{i-1}) (xi1,yi1) adjacent,1 ≤ i ≤ n 1\leq i\leq n1in, n n n is the path length.
Path types are: 4-path; 8-path; m-path.

As can be seen from the picture in the previous section, the green line is the pixel p ( x , y ) p(x,y)p(x,y ) to pixelq ( s , t ) q(s,t)q(s,t ) , respectively by( x 0 , y 0 ) (x_0, y_0)(x0,y0), ( x 1 , y 1 ) (x_1, y_1) (x1,y1), ( x 2 , y 2 ) (x_2, y_2) (x2,y2), ( x 3 , y 3 ) (x_3, y_3) (x3,y3) composition. where( x 0 , y 0 ) (x_0, y_0)(x0,y0) if( x , y ) p(x,y)p(x,y )( x 3 , y 3 ) (x_3, y_3)(x3,y3) isq ( s , t ) q(s,t)q(s,t ) . Since there are m adjacent pixels, this path is an m path.
insert image description here

1.4.3.2 Connectivity

Connected: All pixel gray values ​​on the path satisfy the similarity criterion, ie ( xi , yi ) (x_i,y_i)(xi,yi) ( x i − 1 , y i − 1 ) (x_{i-1},y_{i-1}) (xi1,yi1) adjacent

Order SSS is a subset of pixels in the image. If two pixels ppin Sp andqqThere is a path between q , then say two pixelsppp andqqq inSSS is connected.

for SSAny pixel ppin Sp S S The set of pixels connected to this pixel in S (be careful not to repeat) is called SSConnected components of S. To give a more concrete example: Suppose inSSThere are 5 pixelsp 1 p1 in Sp1 p 2 p2 p2 p 3 p3 p3 p 4 p4 p4 p5p5p5 . _ Thenp 1 − p 2 − p 3 p1-p2-p3P1 _p2 _p 3 connected,p 4 p4p4 p5p5p5 . _ So these are the three connected components.

A set S is called a connected set if S has only one connected component. (all pixels in S are on one pass)

1.4.4 Regions

Let RRR is a subset of pixels in the image. if RRR is a connected set, calledRRR is a region. Two regionsR i R_iRiand R j R_jRj, if they jointly form a connected set, then the region R i R_iRiand R j R_jRjcalled the contiguous region. Regions that are not contiguous are called discontiguous regions.

1.4.5 Boundary

Suppose an image contains KKK disconnected regions, ieR k R_kRk k = 1 , 2 , 3 … , K k=1,2,3…,K k=1,2,3,K. _ and none of them touch the border of the image (this assumption is made to avoid handling special cases. This does not lose generality, because if one or more regions touch the border of the image, we can simply use the 1-pixel wide background value bounds to fill the image). LetR u R_uRufor all KKThe union of K regions, and let ( R u ) c (R_u)_c(Ru)crepresents its complement. We call R u R_uRuAll points in are the foreground of the image, and it is called ( R u ) c (R_u)_c(Ru)cAll points in are the background of the image.

Region RRThe boundary (also known as edge or contour) of R is the set of points that are consistent withRRThe points in the complement set of R are adjacent. In other words, the boundary of a region is the set of pixels in the region that have at least one background neighbor. Here again, we must specify the connectivity used to define the adjacency (that is, several connected).

The foregoing definition is sometimes referred to as the inner boundary of a region to distinguish it from its outer boundary, which corresponds to the background boundary. This distinction is important when developing algorithms for tracking boundaries. This algorithm is usually established along the outer boundary in order to ensure that the result forms a closed path.

My understanding is that the previous concept of connectivity is to find a region with similar pixels (different adjacency methods, different regions), where region 1, region 2 and region 3 are not adjacent to each other. According to the definition, the union of areas 1, 2, and 3 is the foreground, and the complement of the foreground (blue area) is the background. The edges of the foreground and background are borders.
insert image description here

1.5 Color space

The term "color space" originates from the western "Color Space", also known as "color gamut". In color science, people have established a variety of color models to represent a certain One color, the color range that can be defined by this coordinate system is the color space. The color spaces we often use mainly include RGB, CMYK, Lab, etc.

1.5.1 RGB color space

The RGB color space is based on the three basic colors of R (Red: red), G (Green: green), and B (Blue: blue), and most colors can be generated through different linear superpositions.
Advantages:
RGB color space is the most basic, most commonly used, and hardware-oriented color space in image processing, and it is relatively easy to understand.

limitation:

  1. The three components of RGB are highly correlated, so when adjusting the color, it is often necessary to adjust the three components at the same time.
  2. The images acquired under natural conditions are very sensitive to the brightness of the light, and the three components of R, G, and B are all sensitive to the light. Therefore, the RGB color space becomes difficult to express the color of the object under the change of the brightness.
  3. The sensitivity of the human eye to the three components of R, G, and B is different. In monochrome, the human eye is the least sensitive to red and the most sensitive to blue, so the RGB color space is a color space with poor uniformity. For a certain color, it is difficult for us to speculate on the more accurate three-component numerical representation.

因此RGB颜色空间适合于显示系统,却并不适合于图像处理。
insert image description here

1.5.2 HSV color space

参考自博客1中的内容,更细节的信息可以跳转博客1来阅读, 下面HLS, Lab,CMYK同理
The HSV color space is mostly used in image processing, which can express the hue, vividness and lightness of the color very intuitively, and is convenient for color comparison.
In the HSV color space, it is easier to track objects of a certain color than BGR, and is often used to segment objects of a specified color.
The way HSV expresses color images consists of three parts:

  • Hue (hue, hue)
  • Saturation (saturation, color purity)
  • Value (brightness)

Use the following cylinder to represent the HSV color space. The cross section of the cylinder can be regarded as a polar coordinate system. H is represented by the polar angle of the polar coordinates, S is represented by the polar axis length of the polar coordinates, and V is represented by the axis of the cylinder. Highly indicated.
insert image description here
In the case of a certain Hue, the saturation S decreases, that is, white is added to the spectral color, and the proportion of the spectral color is also reduced. When the saturation is reduced to 0, it means that the proportion of the spectral color is zero, resulting in the entire The color appears white.

When the lightness V decreases, black is added to the spectral color, and the proportion of the spectral color is also reduced. When the lightness is reduced to 0, it means that the proportion of the spectral color is zero, causing the entire color to appear black.

The stretch contrast enhancement of HSV is to normalize the two components of S and V (min-max normalize), and H remains unchanged.

1.5.3 HLS color space

HLS and HSV are similar, so we will introduce them together here. HLS also has three components, hue (hue), saturation (saturation), lightness (brightness). The specific description is shown in the figure below:
insert image description here

1.5.4 Lab color space

The Lab color model is a color model developed by the CIE (International Commission on Illumination). Any color in nature can be expressed in Lab space, and its color space is larger than RGB space. In addition, this mode describes human visual perception in a digital way, and has nothing to do with equipment, so it makes up for the deficiency that RGB and CMYK modes must depend on the color characteristics of equipment. Because the color space of Lab is larger than the color space of RGB mode and CMYK mode. This means that the color information that can be described by RGB and CMYK can be mapped in the Lab space. The Lab color model takes the coordinate Lab, where the brightness of L; the positive number of a represents red, the negative end represents green; the positive number of b represents yellow, and the negative end represents blue. As shown in the figure below: The picture comes from Baidu
insert image description here

1.5.5 CMY/CMYK color space

CMY is the color space used in industrial printing. It corresponds to RGB. The simple analogy of RGB comes from the object emitting light, while CMY is obtained based on reflected light. Specific applications such as printers: generally use four-color ink cartridges, that is, CMY plus black ink cartridges

1.5.6 Color space example

图片来自网络

# C++ example
#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>

using namespace cv;

int main() {
    
    
    char sImgPath[] = "./cat.jpg";
    char sNameWindow[] = "BGR_img";

    // BGR颜色空间
    Mat img;
    img = imread(sImgPath, 1);
    //如果读入图像失败
    if (img.empty()) {
    
    
        fprintf(stderr, "Can not load image %s\n", sImgPath);
        return -1;
    }
    namedWindow(sNameWindow);
    //imshow(sNameWindow, img);
    imwrite("BGR_img.jpg", img);

    // HSV颜色空间
    Mat HSVImg;
    char sHSVNameWindow[] = "HSV_img";
    cvtColor(img, HSVImg, COLOR_BGR2HSV);
    namedWindow(sHSVNameWindow);
    //imshow(sHSVNameWindow, HSVImg);
    imwrite("HSV_img.jpg", HSVImg);

    // HLS颜色空间
    Mat HLSImg;
    char sHLSNameWindow[] = "HLS_img";
    cvtColor(img, HLSImg, COLOR_BGR2HLS);
    namedWindow(sHLSNameWindow);
    //imshow(sHLSNameWindow, HLSImg);
    imwrite("HLS_img.jpg", HLSImg);

    // Lab颜色空间
    Mat LabImg;
    char sLabNameWindow[] = "Lab_img";
    cvtColor(img, LabImg, COLOR_BGR2Lab);
    namedWindow(sLabNameWindow);
    //imshow(sLabNameWindow, LabImg);
    imwrite("Lab_img.jpg", LabImg);
    //waitKey();
    destroyAllWindows();
    return 0;
}

BGR image
Please add a picture description

HSV image
insert image description here

HLS image
insert image description here

Lab image
insert image description here

//The code types of the color space are as follows. Except for the above codes, other types are not shown, and readers can try it by themselves.
/*
cv::cvtColor() supports conversion between multiple color spaces. The supported conversion types and conversion codes are as follows:
1. RGB and BGR (opencv default color image color space is BGR) color space conversion
cv ::COLOR_BGR2RGB
cv::COLOR_RGB2BGR
cv::COLOR_RGBA2BGRA
cv::COLOR_BGRA2RGBA
2. Add alpha channel to RGB and BGR images
cv::COLOR_RGB2RGBA
cv::COLOR_BGR2BGRA
3. Remove alpha channel from RGB and BGR images
cv::COLOR_RGBA2RGB
cv ::COLOR_BGRA2BGR
4. Convert from RBG and BGR color space to grayscale space
cv::COLOR_RGB2GRAY
cv::COLOR_BGR2GRAY
cv::COLOR_RGBA2GRAY
cv::COLOR_BGRA2GRAY
5. Convert from grayscale space to RGB and BGR color space
cv::COLOR_GRAY2RGB
cv ::COLOR_GRAY2BGR
cv::COLOR_GRAY2RGBA
cv::COLOR_GRAY2BGRA
6. Conversion between RGB and BGR color spaces and BGR565 color space
cv::COLOR_RGB2BGR565
cv::COLOR_BGR2BGR565
cv::COLOR_BGR5652RGB cv::COLOR_BGR5652BGR cv
:: COLOR_RGBA2BGR565 cv::COLOR_BGRA2BGR565 cv:: COLOR_BGR5652RGBA cv::COLOR_BGR5652BGRA 7, gray Conversion between degree space domain BGR565 cv::COLOR_GRAY2BGR555 cv::COLOR_BGR5552GRAY 8, conversion between RGB and BGR color spaces and CIE XYZ cv::COLOR_RGB2XYZ cv::COLOR_BGR2XYZ cv::COLOR_XYZ2RGB cv::COLOR_XYZ2BGR 9, RGB and Conversion between BGR color space and uma chromaticity (YCrCb space) cv::COLOR_RGB2YCrCb cv::COLOR_BGR2YCrCb cv::COLOR_YCrCb2RGB cv::COLOR_YCrCb2BGR 10. Mutual conversion between RGB and BGR color space and HSV color space cv:: COLOR_RGB2HSV



















cv::COLOR_BGR2HSV
cv::COLOR_HSV2RGB
cv::COLOR_HSV2BGR
11, mutual conversion between RGB and BGR color space and HLS color space
cv::COLOR_RGB2HLS
cv::COLOR_BGR2HLS
cv::COLOR_HLS2RGB
cv::COLOR_HLS2BGR
12, RGB and BGR color Mutual conversion between space and CIE Lab color space
cv::COLOR_RGB2Lab
cv::COLOR_BGR2Lab
cv::COLOR_Lab2RGB
cv::COLOR_Lab2BGR
13. Mutual conversion between RGB and BGR color space and CIE Luv color space
cv::COLOR_RGB2Luv
cv: :COLOR_BGR2Luv
cv::COLOR_Luv2RGB
cv::COLOR_Luv2BGR
14. Conversion from Bayer format (raw data) to RGB or BGR color space
cv::COLOR_BayerBG2RGB
cv::
COLOR_BayerGB2RGB
cv::COLOR_BayerRG2RGB cv::COLOR_BayerGR2RGB
cv::COLOR_B ayerBG2BGR
cv::COLOR_BayerGB2BGR
cv::COLOR_BayerRG2BGR
cv::COLOR_BayerGR2BGR
————————————————
Copyright statement: This article is the original article of CSDN blogger "1_blue", following the CC 4.0 BY-SA copyright Agreement, please attach the original source link and this statement for reprinting.
Original link: https://blog.csdn.net/weixin_51105360/article/details/113941015

1.5.6 Example of Color Space Application

For image segmentation tasks, sometimes converting the color space is often more conducive to segmenting the desired area.
The following still uses the above cat as an example.

#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main() {
    
    
    char sImgPath[] = "./cat.jpg";
    char sNameWindow[] = "BGR_img";
    // BGR颜色空间
    Mat img;
    img = imread(sImgPath, 1);
    //如果读入图像失败
    if (img.empty()) {
    
    
        fprintf(stderr, "Can not load image %s\n", sImgPath);
        return -1;
    }
    // HSV颜色空间
    Mat hsvImg;
    char sHSVNameWindow[] = "HSV_img";
    cvtColor(img, hsvImg, COLOR_BGR2HSV);
	// 获取HSV中的H通道图片(色相)
    Mat vImg;
    vImg = Mat::zeros(hsvImg.rows, hsvImg.cols, CV_8UC1);
    for (int nrow = 0; nrow < hsvImg.rows; nrow++)
    {
    
    
        uchar* data = vImg.ptr(nrow);
        for (int ncol = 0; ncol < hsvImg.cols; ncol++)
        {
    
    
            Vec3i obj = hsvImg.at<Vec3b>(nrow, ncol);
            data[ncol] = obj[0];
        }
    }
    //namedWindow("V_img");
    //imshow("V_img", vImg);
    imwrite("V_img.jpg", vImg);
	//获取灰度图像
    Mat grayImg;
    cvtColor(img, grayImg, COLOR_BGR2GRAY);
    //namedWindow("GREAY_img");
    //imshow("GREAY_img", grayImg);
    imwrite("GREAY_img.jpg", grayImg);
	//对灰度图像进行阈值分割 
    Mat grayThImg;
    threshold(grayImg, grayThImg, 80, 255, THRESH_BINARY);
    //namedWindow("grayThImg");
    //imshow("grayThImg", grayThImg);
    imwrite("grayThImg.jpg", grayThImg);
    //V通道图像进行阈值分割 
    Mat vThImg;
    threshold(vImg, vThImg, 80, 255, THRESH_BINARY);
    //namedWindow("vThImg");
    //imshow("vThImg", vThImg);
    imwrite("vThImg.jpg", vThImg);
    //waitKey();
    //destroyAllWindows();
    return 0;
}

cat grayscale image
insert image description here

Hue image of a cat (H channel image)
insert image description here

Threshold segmentation on grayscale images
insert image description here

Threshold segmentation of the hue image.
insert image description here

从上述两种结果可以看出,我们可以通过变换不同的色彩空间,来实现获取自己想要分割阈值区域。


Summarize

Thank you readers for reading. If you think it is helpful to you, you can give the little brother a thumbs up.

Finally, remember Mr. Mao Xingyun and thank Mr. Mao Xingyun for his guidance. This article mainly refers to Mr. Mao Xingyun's "Introduction to OpenCV3 Programming".

Guess you like

Origin blog.csdn.net/weixin_43610114/article/details/125848404