拉普拉斯金字塔
拉普拉斯金字塔(Laplacian pyramid): 用来从金字塔低层图像重建上层未采样图像,在数字图像处理中也即是预测残差,可以对图像进行最大程度的还原,配合高斯金字塔一起使用。我们可以将拉普拉斯金字塔理解为高斯金字塔的逆过程形式。
拉普拉斯金字塔的计算过程是通过源图像减去先缩小后再放大的图像,可以直接用OpenCV进行拉普拉斯运算:
拉普拉斯效果图如下:
将拉普拉斯金字塔逐层上采样累加重构后的效果图如下:
C++和opencv代码实现:
usingnamespacestd; usingnamespacecv; voidGaussian_Pyramid(Mat&image, vector<Mat>&pyramid_images, intlevel); voidLaplaian_Pyramid(vector<Mat>&pyramid_images, Mat&image); voidreconstuction(intlevel); intmain(intargc, char**argv) { Matsrc=imread("Curry.jpg"); /*copyMakeBorder(src, src, 0, 3, 0, 3, BORDER_REPLICATE);*/imshow("01", src); /*resize(src, src, Size(src.cols *4, src.rows *4));*//*cvtColor(src, src, COLOR_BGR2GRAY);*/vector<Mat>p_images; constintlayer=3; //金字塔层数Gaussian_Pyramid(src, p_images, layer-1); Laplaian_Pyramid(p_images, src); reconstuction(layer-1);//从拉普拉斯金字塔恢复原图waitKey(); return0; } voidGaussian_Pyramid(Mat&image, vector<Mat>&pyramid_images, intlevel) { Mattemp=image.clone(); Matdst; charbuf[64]; for (inti=0; i<level; i++) { pyrDown(temp, dst); imshow(format("pyramid_up_%d", i), dst); sprintf_s(buf, "./result/gaussian_%d.jpg", i); imwrite(buf, dst); temp=dst.clone(); pyramid_images.push_back(temp); } } voidLaplaian_Pyramid(vector<Mat>&pyramid_images, Mat&image) { intnum=pyramid_images.size() -1; imwrite("./result/laplacian_0.jpg", pyramid_images[num]); imshow("laplacian_0.jpg", pyramid_images[num]); for (intt=num; t>-1; t--) { Matdst; charbuf[64]; if (t-1<0) { pyrUp(pyramid_images[t], dst, image.size()); subtract(image, dst, dst); // dst = dst + Scalar(127, 127, 127);sprintf_s(buf, "./result/laplacian_%d.jpg", num-t+1); imshow(buf, dst); imwrite(buf, dst); } else { pyrUp(pyramid_images[t], dst, pyramid_images[t-1].size()); subtract(pyramid_images[t-1], dst, dst); // dst = dst + Scalar(127, 127, 127);sprintf_s(buf, "./result/laplacian_%d.jpg", num-t+1); imshow(buf, dst); imwrite(buf, dst); } } } voidreconstuction(intlevel) { charbuf[64]; Matdst=imread("./result/laplacian_0.jpg"); Matdst2=imread("./result/laplacian_1.jpg"); Matdst3=imread("./result/laplacian_2.jpg"); pyrUp(dst, dst); Matdst4=dst+dst2; pyrUp(dst4, dst4); Matdst5=dst4+dst3; imshow("dst", dst5); }