本文主要是介绍opencv函数GaussianBlur,pyrDown,pyrUp深究,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
通常在使用GaussianBlur,pyrDown,pyrUp的时候都不会深究的内部实现,最近想自己实现一下,看看会遇到哪些问题,然后将结果和opencv的函数运行结果进行对比。
C++: void GaussianBlur(InputArray src, OutputArray dst, Size ksize, double sigmaX, double sigmaY=0, int borderType=BORDER_DEFAULT )
从GaussianBlur的参数可以看出,有sigmaX 和sigmaY, 二维高斯核使用的是分别在X和Y方向进行一维高斯核卷积来模拟二维高斯核的效果。
对于边界问题,BORDER_DEFAULT 是指:pixel[-1]使用pixel[1]的像素值来代替,pixel[-2] = pixel[2],pixel[width] = pixel[width - 2],依此类推(如果pixel[0],pixel[width -1]为边界的话) 。
for (int row = 0 ;row <src.rows ;row++)//x orientation{for (int col = 0 ; col <src.cols; col++){float sumRed = 0,sumGreen = 0,sumBlue = 0;for (int subcol = -win_size;subcol<=win_size;subcol++){int srccol = col+subcol;if (srccol<0){srccol *= -1;}if (srccol>=src.cols){srccol = src.cols*2 - srccol - 2;}sumRed += kernel[subcol+win_size]*src.at<Vec3b>(row,srccol)[2];sumGreen += kernel[subcol+win_size]*src.at<Vec3b>(row,srccol)[1];sumBlue += kernel[subcol+win_size]*src.at<Vec3b>(row,srccol)[0];}temp.at<Vec3b>(row,col)[2] = sumRed;temp.at<Vec3b>(row,col)[1] = sumGreen;temp.at<Vec3b>(row,col)[0] = sumBlue;}}for (int row = 0 ;row <src.rows ;row++)//y orientation{for (int col = 0 ; col <src.cols; col++){float sumRed = 0,sumGreen = 0,sumBlue = 0;for (int subrow = -win_size;subrow<=win_size;subrow++){int srcrow = row+subrow;if (srcrow<0){srcrow *= -1;}if (srcrow>=src.rows){srcrow = src.rows*2 - srcrow - 2;;}sumRed += kernel[subrow+win_size]*temp.at<Vec3b>(srcrow,col)[2];sumGreen += kernel[subrow+win_size]*temp.at<Vec3b>(srcrow,col)[1];sumBlue += kernel[subrow+win_size]*temp.at<Vec3b>(srcrow,col)[0];}result.at<Vec3b>(row,col)[2] = sumRed;result.at<Vec3b>(row,col)[1] = sumGreen;result.at<Vec3b>(row,col)[0] = sumBlue;}}
C++: void pyrDown(InputArray src, OutputArray dst, const Size& dstsize=Size())
pyrDown 是降采样,分两步进行。
第一步:对src图像进行模糊,使用的模糊核不是高斯核,而是这样一个核函数:
void GassianPyramid::downSampleFunc(Mat src, Mat& result)
{int width = src.cols;int height = src.rows;int ow = width/2; int oh = height/2; result.create(Size(ow,oh),src.type());float tempArr[5][5]={1,4,6,4,1,4,16,24,16,4,6,24,36,24,6,4,16,24,16,4,1,4,6,4,1};for (int i = 0;i<5;i++){for (int j = 0; j<5; j++){tempArr[i][j] /=256;}}Mat temp;temp.create(Size(src.cols,src.rows),src.type());for (int row = 0 ;row <src.rows ;row++){for (int col = 0 ; col <src.cols; col++){float sumRed = 0,sumGreen = 0,sumBlue = 0;for (int subrow = -win_size;subrow<=win_size;subrow++){int srcrow = row+subrow;if (srcrow<0){srcrow *= -1;}if (srcrow>=src.rows){srcrow = (src.rows - 1) - (srcrow - (src.rows-1));}for (int subcol = -win_size;subcol<=win_size;subcol++){int srccol = col+subcol;if (srccol<0){srccol *= -1;}if (srccol>=src.cols){srccol = (src.cols - 1) - (srccol - (src.cols-1));}sumRed += tempArr[subrow+win_size][subcol+win_size]*src.at<Vec3b>(srcrow,srccol)[2];sumGreen += tempArr[subrow+win_size][subcol+win_size]*src.at<Vec3b>(srcrow,srccol)[1];sumBlue += tempArr[subrow+win_size][subcol+win_size]*src.at<Vec3b>(srcrow,srccol)[0];}}temp.at<Vec3b>(row,col)[2] = sumRed;temp.at<Vec3b>(row,col)[1] = sumGreen;temp.at<Vec3b>(row,col)[0] = sumBlue;}}int inRow=0, inCol = 0; for(int row=0; row<oh; row++) { for(int col=0; col<ow; col++) { inRow = 2* row; inCol = 2* col; if(inRow >= height) { inRow = height; } if(inCol >= width) { inCol = width; } result.at<Vec3b>(row,col)[2]= temp.at<Vec3b>(inRow,inCol)[2];result.at<Vec3b>(row,col)[1]= temp.at<Vec3b>(inRow,inCol)[1];result.at<Vec3b>(row,col)[0]= temp.at<Vec3b>(inRow,inCol)[0];} }
}
C++: void pyrUp(InputArray src, OutputArray dst, const Size& dstsize=Size())
pyrUp是上采样,也是分两步进行。第一步:将图像在每个方向扩大为原来的两倍,新增的行和列以0填充。
第二步:将
上面降采样的高斯核*4和放大后的图像进行卷积。
void GassianPyramid::expendSampleFunc(Mat src, Mat& result)
{int width = src.cols;int height = src.rows;int ow = 2*width; int oh =2*height; Mat temp;temp.create(Size(ow,oh),src.type());result.create(Size(ow,oh),src.type());for(int row=0; row<oh; row++) { for(int col=0; col<ow; col++) { int inRow = row/2; int inCol = col/2;if(row - inRow*2 > 0 || col - inCol*2 > 0) { temp.at<Vec3b>(row,col)[2] = 0;temp.at<Vec3b>(row,col)[1] = 0;temp.at<Vec3b>(row,col)[0] = 0;continue;} temp.at<Vec3b>(row,col)[2] = src.at<Vec3b>(inRow,inCol)[2];temp.at<Vec3b>(row,col)[1] = src.at<Vec3b>(inRow,inCol)[1];temp.at<Vec3b>(row,col)[0] = src.at<Vec3b>(inRow,inCol)[0];} } float tempArr[5][5]={1,4,6,4,1,4,16,24,16,4,6,24,36,24,6,4,16,24,16,4,1,4,6,4,1};for (int i = 0;i<5;i++){for (int j = 0; j<5; j++){tempArr[i][j] /=64;}}for (int row = 0 ;row <temp.rows ;row++){for (int col = 0 ; col <temp.cols; col++){float sumRed = 0,sumGreen = 0,sumBlue = 0;for (int subrow = -win_size;subrow<=win_size;subrow++){int srcrow = row+subrow;if (srcrow<0){srcrow *= -1;}if (srcrow>=temp.rows){srcrow = (temp.rows - 1) - (srcrow - (temp.rows-1));}for (int subcol = -win_size;subcol<=win_size;subcol++){int srccol = col+subcol;if (srccol<0){srccol *= -1;}if (srccol>=temp.cols){srccol = (temp.cols - 1) - (srccol - (temp.cols-1));}sumRed += tempArr[subrow+win_size][subcol+win_size]*temp.at<Vec3b>(srcrow,srccol)[2];sumGreen += tempArr[subrow+win_size][subcol+win_size]*temp.at<Vec3b>(srcrow,srccol)[1];sumBlue += tempArr[subrow+win_size][subcol+win_size]*temp.at<Vec3b>(srcrow,srccol)[0];}}result.at<Vec3b>(row,col)[2] = sumRed;result.at<Vec3b>(row,col)[1] = sumGreen;result.at<Vec3b>(row,col)[0] = sumBlue;}}
}
这篇关于opencv函数GaussianBlur,pyrDown,pyrUp深究的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!