//load the Original Image and get some informations
Mat src = imread("012.jpg",0);
namedWindow("OriginalImage");
imshow("OriginalImage",src);
CV_Assert(src.depth() == CV_8U);
//OpenCV solution - Laplacian Mat dst,abs_dst_laplace; Laplacian(src,dst,CV_16S,3); convertScaleAbs(dst,abs_dst_laplace);
//show the result namedWindow(“result_laplacian”); imshow(“result_laplacian”,abs_dst_laplace);
//get some informations of original image
int nr = src.rows;
int nc = src.cols;
int n = nr*nc;
int arr[9] = {0};
//scan the whole pixels of original image //and do Laplacian Operation int* table_lap = new int[n]; int* table_orig = new int[n]; int l; for (int i=0;i<n;i++) { table_lap[i] = 0; table_orig[i] = 0; } for (int i=1;i<nr-1;i++) { const uchar* previous = src.ptr<uchar>(i-1); const uchar* current = src.ptr<uchar>(i); const uchar* next = src.ptr<uchar>(i+1); for (int j=1;j<nc-1;j++) { for (int k=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 pixel Lmaskoperation(table_lap,arr,l); table_orig[l] = arr[4]; } }
//pixels scale uchar* La_scaled = new uchar[n]; table_scale(table_lap,La_scaled,n);
//padding values Mat LaResult_own; LaResult_own.create(src.size(),src.type()); uchar* p = NULL; for (int i=0;i<nr;i++) { p = LaResult_own.ptr<uchar>(i); for (int j=0;j<nc;j++) { l = nc*i+j; p[j] = La_scaled[l]; } }
//*****************************//
//scale the pixels to [0 255]
//*****************************//
void table_scale(int* table,uchar* result,int n)
{
int min = table[0];
int max = table[0];
for (int i=0;i<n;i++)
{
if(min>table[i])
{
min = table[i];
}
if(max<table[i])
{
max = table[i];
}
}
for (int i=0;i<n;i++)
{
result[i] = (uchar)(255*(table[i]-min)/(max-min));
}
}
仿真结果,拉伸后Laplace算子的操作结果
以灰色为主色调的显示结果就是Laplace算子操作拉伸后显示的一大特点。
Laplace滤波图像与原图像的混合
我使用的mask中心值为正,所以混合操作需要原图减去Laplace滤波图像,代码段如下,
//blending with the original image using Eq g(x,y)=f(x,y)+c*Lap(x,y)
int* table_blend = new int[n];
for(int i=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 result Mat Blresult; Blresult.create(src.size(),src.type()); for (int i=0;i<nr;i++) { p = Blresult.ptr<uchar>(i); for(int j=0;j<nc;j++) { l = nc*i+j; p[j] = table_blend[l]; } }
//show blending result namedWindow(“blending result_laplacian”); imshow(“blending result_laplacian”,Blresult);