opencv / c ++ image processing to be continued

The image binarization process

namedWindow

namedWindow (): a new display window. You can specify the type of window.
A parameter: Window Name
Parameters II: window identifier. The default is WINDOW_AUTOSIZE.
WINDOW_AUTOSIZE: window size to automatically adapt to the size of the image, and can not be changed manually.
WINDOW_NORMAL: Users can change the window size.
WINDOW_OPENGL: When a window is created it will support OpenGL.

imread

imread (): Load a picture. The functions are often complex imshow () function used together.

imshow

imshow (): function function is to load the picture you just show up.

waitKey

Waiting for key events

Mat

data: Mat a pointer to the object, point matrix data stored in memory, a memory (uchar * data)

dims: dimension of the matrix represents Mat, such as the 4 * 3 matrix is ​​two-dimensional, 3 * 4 * 5 is a three-dimensional

channels: channel number of each matrix element of the matrix have values ​​of, for example, a total of 3 * 4 matrix of 12 elements, if each element has three values, then this matrix is ​​said to 3 channels, i.e. channels = 3. Common is a color picture red, green, and blue channels.

depth: depth, i.e. number of bits per pixel (bits), to give () in the opencv of Mat.depth is a number from 0 - 6, representing different numbers of bits: enum {CV_8U = 0, CV_8S = 1 , CV_16U = 2, CV_16S = 3, CV_32S = 4, CV_32F = 5, CV_64F = 6}; visible 0 and 1 represent 8, 2, and 3 represents a 16-bit, 4 and 5 represents a 32-bit, 6 for 64 ;

step: is an array, defines the layout matrices, image analysis as summarized below, additional attention step1 (step / elemSize1), M.step [m-1] is always equal elemSize, M.step1 (m-1) always equal channels;

elemSize: Data size of each element of the matrix, if the data type of the data is CV_8U Mat so elemSize = 1, CV_8UC3 then elemSize = 3, CV_16UC2 then elemSize = 4; remember elemSize1 addition there is a matrix representation of the data type size, i.e. the size elemSize / channels of

void two() {    
    //定义变量
    Mat img;
    int threshval = 170;//设定阈值
    Mat result;
    Mat bw = img;
    //载入图片
    img = imread("D:\\in.jpg", 0);
    //显示原图 
    namedWindow("原始图像");
    imshow("原始图像", img);
    bw = threshval < 100 ? (img < 160) : (img > 160);//如果阈值小于100,那么中间为真,即像素值小于160的为1,大于160的为0;如果阈值大于100,那么右边为真,即像素值大于160的为1,小于160的为0.
    namedWindow("二值化后的图像");
    imshow("二值化后的图像", bw);
    waitKey(0);
}

Image contour extraction

cvtcolor (): color space conversion function is a function, the color conversion may be achieved RGB color space to HSV, HSI like. It can be converted to grayscale.

findContours function introduction

void line() {	
    Mat img, dst;	
    img = imread("D:\\in.jpg");	
    namedWindow("原始图像", CV_WINDOW_AUTOSIZE);	
    imshow("原始图像", img);	
    dst = Mat::zeros(img.size(), CV_8UC3);//相当于创建一张黑色的图  	
    blur(img, img, Size(3, 3));  //用标准化的盒式过滤器来平滑图像	
    cvtColor(img, img, COLOR_BGR2GRAY);//颜色转换	
    Canny(img, img, 20, 80, 3, false);  //使用canny算法对输入图像进行边缘检测
    std::vector<std::vector<Point>> contours; //是一个向量,并且是一个双重向量,向量内每个元素保存了一组由连续的Point点构成的点的集合的向量,每一组Point点集就是一个轮廓。有多少轮廓,向量contours就有多少元素。	
    std::vector<Vec4i> hierarchy;  //Vec4i是Vec<int,4>的别名,定义了一个“向量内每一个元素包含了4个int型变量”的向量。	
    findContours(img, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));//检测出物体的轮廓 	
    RNG rng(0);    //随机生成数	
    for (int i = 0; i < contours.size(); i++)	{		
        Scalar color = Scalar(rng.uniform(255, 255), rng.uniform(255, 255), rng.uniform(255, 255));//将图像设置成单一灰度和颜色		
        drawContours(dst, contours, i, color, 1, 8, hierarchy, 0, Point(0, 0));    //用于画出图像的轮廓	
    }	
    namedWindow("轮廓", CV_WINDOW_AUTOSIZE);	imshow("轮廓", dst);
    waitKey(0);
}

Calculating image gradient

Sobel function

Detailed GaussianBlur function parameters as follows:

the src, the input image, i.e. the source image, the object can fill Mat class. It can be a single image any number of channels, but need to pay attention, the depth of the picture should be CV_8U, CV_16U CV_16S, CV_32F and one, CV_64F.

dst, ie, the target image, needs and source images have the same size and type. For example, you can use Mat :: Clone, the source picture as a template to initialize get disguising the target map.

ksize, the size of the Gaussian kernel. Ksize.width and ksize.height which may be different, but they all must be positive and odd (do not understand). Alternatively, they may be zero, which are calculated by the sigma come.

sigmaX, represents the standard deviation of the Gaussian kernel in the X direction.

sigmaY, represents the standard deviation of the Gaussian kernel function Y direction. If sigmaY zero Set it sigmaX, if sigmaX and sigmaY are 0, then calculated from ksize.width and ksize.height. For the sake of correctness of the results, preferably Size third parameter, and fourth parameter sigmaX sigmaY fifth parameter to specify all.

borderType, used to infer certain model image pixels outside the boundary. Note that it has a default value BORDER_DEFAULT.

Here Insert Picture Description
The arithmetic operation can be quickly implemented image enhancement and related operations

void gradient() {	
    Mat src, gray, dst;	
    src = imread("D:\\in.jpg");	
    namedWindow("input", CV_WINDOW_AUTOSIZE);	
    imshow("input", src);	
    GaussianBlur(src, src, Size(3, 3), 0, 0); //进行均值滤波操作
    cvtColor(src, gray, CV_BGR2GRAY);  //转换为灰度图
    //【1】sobel算子	
    Mat gradient_x, gradient_y;	
    Sobel(gray, gradient_x, CV_16S, 1, 0, 3);	
    Sobel(gray, gradient_y, CV_16S, 0, 1, 3); 	
    //【2】Scharr算子	
    //Scharr(gray, gradient_x, CV_16S, 1, 0, 1, 0);
    //Scharr(gray, gradient_y, CV_16S, 1, 0, 1, 0);*/
    //计算绝对值	
    convertScaleAbs(gradient_x, gradient_x);	
    convertScaleAbs(gradient_y, gradient_y);	
    //imshow("gradient_x", gradient_x);	
    //imshow("gradient_y", gradient_y);	
    addWeighted(gradient_x, 1, gradient_y, 1, 0, dst); 	
    //【3】laplace算子	
    //Laplacian(gray, dst, CV_16S, 3);	
    //convertScaleAbs(dst, dst); 	
    //通过访问像素进行操作 	
    int rows = gradient_x.rows; 	
    int cols = gradient_x.cols; 	
    Mat dst1 = Mat::zeros(gradient_x.size(), gradient_x.type()); 	
    for (int i = 0; i < rows; i++) { 		
        for (int j = 0; j < cols; j++) { 			
            dst1.at<uchar>(i, j) = saturate_cast<uchar>(gradient_x.at<uchar>(i, j) + gradient_y.at<uchar>(i, j));  //防止数据溢出 		
        } 	
    } 	
    imshow("梯度", dst1);	
    waitKey(0);
}
Published 35 original articles · won praise 2 · Views 958

Guess you like

Origin blog.csdn.net/y18771025420/article/details/103384675