第十章 滤波和卷积(一)
文章目录
一、卷积
**卷积convolution:**对图像的操作;
卷积核kernel:一个矩阵,用于卷积运算,一般中心是锚点anchor point;
**填充pad:**由于卷积时边缘像素没有被卷积核覆盖,所以会有边缘问题。所以在卷积之前,需要对原图像进行边缘填充。
int cv::borderInterpolate( // Returns coordinate of "donor" pixel
int p, // 0-based coordinate of extrapolated pixel
int len, // Length of array (on relevant axis)
int borderType // Pixel extrapolation method
);
填充方式:
BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
BORDER_WRAP: cdefgh|abcdefgh|abcdefg
BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified ‘i’
二、阈值化操作
2.1 阈值化
阈值化操作就是要从图像中分离对象(前景和背景分离)。
double cv::threshold(
cv::InputArray src, // Input image
cv::OutputArray dst, // Result image
double thresh, // Threshold value
double maxValue, // Max value for upward operations
int thresholdType // Threshold type
);
阈值化操作有如下:
THRESH_BINARY二进制阈值化
THRESH_BINARY_INV反二进制阈值化
THRESH_TRUNC截断阈值化
THRESH_TOZERO阈值化为0
THRESH_TOZERO_INV反阈值化为0
void sum_rgb(const Mat& src, Mat& dst) {
// Split image onto the color planes.
vector< Mat> planes;
split(src, planes);
Mat b = planes[0], g = planes[1], r = planes[2], s;
// Add equally weighted rgb values.
addWeighted(r, 1. / 3., g, 1. / 3., 0.0, s);
addWeighted(s, 1., b, 1. / 3., 0.0, s);
// Truncate values above 100.
threshold(s, dst, 100, 100, THRESH_TRUNC);
}
int main() {
Mat src = imread("1.jpg"), dst;
imshow("org img", src);
sum_rgb(src, dst);
imshow("dst img", dst);
waitKey(0);
return 0;
}
之所以要把三个颜色的通道分开是因为,高位容易溢出?(没看懂)
2.2 Otsu算法
σ w 2 = w 1 ( t ) σ 1 2 + w 2 ( t ) σ 2 2 \sigma_{w}^{2}=w_{1}(t)\sigma_{1}^{2}+w_{2}(t)\sigma_{2}^{2} σw2