函数:
void cv::copyMakeBorder (InputArray src,
OutputArray dst,
int top,
int bottom,
int left,
int right,
int borderType, //填充类型
const Scalar & value = Scalar() //填充颜色
)
Python:
cv.copyMakeBorder( src, top, bottom, left, right, borderType[, dst[, value]] ) -> dst
填充类型:
使用如下:
RNG rng(12345);
int top, bottom, left, right;
int borderType = BORDER_CONSTANT;
// Initialize arguments for the filter
top = (int) (0.05*src.rows); bottom = top;
left = (int) (0.05*src.cols); right = left;
Scalar value( rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255) );
copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
void cv::Sobel (InputArray src,
OutputArray dst,
int ddepth,
int dx, //x方向
int dy, //y方向
int ksize = 3, //核大小
double scale = 1,
double delta = 0,
int borderType = BORDER_DEFAULT
)
Python:
cv.Sobel( src, ddepth, dx, dy[, dst[, ksize[, scale[, delta[, borderType]]]]] ) -> dst
变换函数:
void cv::convertScaleAbs( InputArray src,
OutputArray dst,
double alpha = 1, //对比度
double beta = 0 //亮度
)
Python:
cv.convertScaleAbs( src[, dst[, alpha[, beta]]] ) -> dst
cvtColor(src, src_gray, COLOR_BGR2GRAY);
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;
Sobel(src_gray, grad_x, ddepth, 1, 0, ksize, scale, delta, BORDER_DEFAULT);
Sobel(src_gray, grad_y, ddepth, 0, 1, ksize, scale, delta, BORDER_DEFAULT);
// converting back to CV_8U
convertScaleAbs(grad_x, abs_grad_x);
convertScaleAbs(grad_y, abs_grad_y);
addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad);
函数接口:
void cv::Scharr (InputArray src,
OutputArray dst,
int ddepth,
int dx,
int dy,
double scale = 1,
double delta = 0,
int borderType = BORDER_DEFAULT
)
Python:
cv.Scharr( src, ddepth, dx, dy[, dst[, scale[, delta[, borderType]]]] ) -> dst
当核的大小为3时,上面所示的Sobel核可能产生明显的不准确性(毕竟,Sobel只是导数的近似值)。OpenCV通过使用Scharr()函数解决了大小为3的内核的不准确性问题。这与标准Sobel函数一样快,但更准确。它实现了以下内核:
求梯度时,与Sobel算子实现代码相同,下边给出边缘检测应用:
Mat dx,dy;
Scharr(blurImage,dx,CV_16S,1,0);
Scharr(blurImage,dy,CV_16S,0,1);
Canny( dx,dy, edge2, edgeThreshScharr, edgeThreshScharr*3 );
cedge = Scalar::all(0);
image.copyTo(cedge, edge2);
void cv::Laplacian( InputArray src,
OutputArray dst,
int ddepth,
int ksize = 1,
double scale = 1,
double delta = 0,
int borderType = BORDER_DEFAULT
)
Python:
cv.Laplacian( src, ddepth[, dst[, ksize[, scale[, delta[, borderType]]]]] ) -> dst
cvtColor( src, src_gray, COLOR_BGR2GRAY ); // Convert the image to grayscale
Mat abs_dst;
Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
// converting back to CV_8U
convertScaleAbs( dst, abs_dst );