Opencv-with me. The Laplace operator for Digital image processing

Source: Internet
Author: User

The Laplace operator, like the Sobel operator, belongs to the spatial sharpening filter operation. The essence and the previous spatial filter operation is similar, the following is through the Laplace operator to introduce the spatial sharpening filter, and OPENCV provided in the Laplacian function for some explanation.

    • Mathematical Principles

Discrete function derivative

The derivative of the discrete function is degenerate into the difference, the one-dimensional first-order difference formula and the second differential equation are, respectively,

The difference form of Laplace operator

The Laplace operator of discrete function is obtained by differential the second derivative of X, y two directions of Laplace operator respectively.

In a two-dimensional function f (x, Y), the second-order difference in X, y two directions is, respectively,

So the differential form of the Laplace operator is,

The form of a filter mask is as follows,

0 1 0
1 -4 1
0 1 0
Notice the characteristics of the mask, mask in the upper and lower left and right four 90 degrees in the direction of the same result, that is, in the direction of 90 degrees without directionality. In order for the mask to have this property in the direction of 45 degrees, the filter mask extension is defined as
1 1 1
1 -8 1
1 1 1

Note:

Sometimes we also see the filter mask of the Laplace operator, which differs from the above results,

0 -1 0
-1 4 -1
0 -1 0
-1 -1 -1
-1 8 -1
-1 -1 -1

The reason for this is that the opposite definition is used when defining the second derivative, which is irrelevant, but it is important to note that when the image is superimposed with the original by the Laplace operator, the blending operation is added or reduced depending on the definition above.

Laplace operation of Images

As stated at the beginning of this article, when the Laplace operator is written as a filter mask, its operation is similar to other spatial filtering operations. The filter mask is moved line by row on the original image, then the value in mask is multiplied by its coincident pixels, assigned to the pixels coincident with the mask Center, and the first, and last rows and columns of the images cannot be assigned 0 of pixels of the above operation, and the result of the Laplace operation is obtained.

Mixing the results of Laplace operation with original image

Because the Laplace operator is a second derivative operation, it does not emphasize the continuous portion of the grayscale value while emphasizing the discontinuity of the gray scale in the image element. This produces a black background with significant gray boundaries, but not enough features. The background feature can be recovered by the image mixed with the Laplace operator after the original image is manipulated. Using formulas,

The value of the parameter C is related to the two mask definitions above, and when the value of the Mask Center is c=-1, the opposite c=1 is obtained.

    • Calculation of Laplace operator based on OPENCV

The Laplacian function in OpenCV can realize the Laplace operation of the image, and the concrete usage is as follows.

Laplacian (Src_gray, DST, ddepth, kernel_size, scale, Delta, Border_default);

The parameter meaning is,

    1. src_gray, input image
    2. DST, Laplace operation results
    3. ddepth, the output image depth, because the input image is generally cv_8u, in order to avoid data overflow, the output image depth should be set to Cv_16s
    4. kernel_size, the size of the filter mask, our mask is 3x3, so here should be set to 3
    5. Scale,delta,border_default, the default setting is good

The OpenCV-based Laplace operator simulation code snippet is as follows,

//load the Original Image and get some informationsMat src = imread ("012.jpg",0); Namedwindow ("Originalimage"); Imshow ("Originalimage", SRC); Cv_assert (src.depth ()==cv_8u);//OpenCV Solution-laplacianMat Dst,abs_dst_laplace; Laplacian (Src,dst,cv_16s,3); Convertscaleabs (dst,abs_dst_laplace);//Show the resultNamedwindow ("Result_laplacian"); Imshow ("Result_laplacian", abs_dst_laplace);

The function of Convertscaleabs function is to transform the output image of cv_16s type into cv_8u type image.

Simulation results:

Original:

Laplace operation Result:

    • Simulation based on mask operation principle

Laplace operator filtering simulation

According to the algorithm introduced in the mathematical principle, the corresponding code is written and the related simulation is carried out. The results of the Laplace operation are image-stretched, because the range of pixel values of the Laplace operation results may fall outside of [0,255], while the computer displays the assigned values at 0, and the pixels greater than 255 are all displayed as 255.

The code snippet is as follows,

//get some informations of original imageintNR =src.rows;intNC =Src.cols;intn = nr*NC;intarr[9] = {0};//scan the whole pixels of original image//And do Laplacian operationint* Table_lap =New int[n];int* Table_orig =New int[n];intl; for(intI=0; i<n;i++) {Table_lap[i]=0; Table_orig[i]=0;} for(intI=1; i<nr-1; i++){    Constuchar* previous = src.ptr<uchar> (i-1); Constuchar* current = src.ptr<uchar>(i); Constuchar* next = src.ptr<uchar> (i+1);  for(intj=1; j<nc-1; j + +)    {         for(intk=0;k<3; k++) {Arr[k]= previous[j+k-1]; Arr[k+3] = current[j+k-1]; Arr[k+6] = next[j+k-1]; } l= Nc*i+j;//calculate the location in the table of current pixellmaskoperation (table_lap,arr,l); TABLE_ORIG[L]= arr[4]; }}//Pixels Scaleuchar* la_scaled =NewUchar[n];table_scale (table_lap,la_scaled,n);//padding ValuesMat Laresult_own; Laresult_own.create (Src.size (), Src.type ()); Uchar* p =NULL; for(intI=0; i<nr;i++) {p= laresult_own.ptr<uchar>(i);  for(intj=0; j<nc;j++) {L= nc*i+J; P[J]=La_scaled[l]; }}//Show ResultsNamedwindow ("Laresult_own"); Imshow ("Laresult_own", Laresult_own);

Where lmaskoperation is I write mask for Laplace mask operation operation function, function section as follows,

//**********************////Laplacian Mask Operation//**********************//voidLmaskoperation (int* Table,int* Arr,intl) {    inttmp[9] = {-1,-1,-1,-1,8,-1,-1,-1,-1};  for(intI=0;i<9; i++) {Table[l]= Table[l] + tmp[i]*Arr[i]; }}

The Tabel_scale function is the image stretching function I wrote, stretching the result of the Laplace operation to [0,255], the function segment as follows,

//*****************************////Scale the pixels to [0 255]//*****************************//voidTable_scale (int* table,uchar* result,intN) {    intMin = table[0]; intmax = table[0];  for(intI=0; i<n;i++)    {        if(min>Table[i]) {min=Table[i]; }        if(max<Table[i]) {Max=Table[i]; }    }     for(intI=0; i<n;i++) {Result[i]= (Uchar) (255* (table[i]-min)/(max-min)); }}

Simulation results, operation results of Laplace operator after stretching

The display result of the gray-dominated hue is a major feature of the Laplace operator's operation after stretching.

Mixing of Laplace filter image with original image

I use the Mask center value is positive, so the blending operation requires the original image minus the Laplace filter images, the code snippet is as follows,

//blending with the original image using Eq g ( x, y) =f (x, y) +c*lap (x, y)int* Table_blend =New int[n]; for(intI=0; i<n;i++) {Table_blend[i]= Table_orig[i]-Table_lap[i]; if(table_blend[i]<0) {Table_blend[i]=0; }    Else if(table_blend[i]>255) {Table_blend[i]=255; }}//padding values to blending resultMat Blresult; Blresult.create (Src.size (), Src.type ()); for(intI=0; i<nr;i++) {p= blresult.ptr<uchar>(i);  for(intj=0; j<nc;j++) {L= nc*i+J; P[J]=Table_blend[l]; }}//Show blending resultNamedwindow ("Blending Result_laplacian"); Imshow ("Blending Result_laplacian", Blresult);

Simulation results:

Finally, the simulation results of the material given by Gonzalez in introducing Laplacian are given.

Opencv-with me. The Laplace operator for Digital image processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.