OpenCV 4] [image convolution (Blur)

First, the programming environment:

OpenCV  4.1.0
HERE Visual Studio 2017 Enterprise (15.9.13)
operating system Windows 10 x64 Chinese Professional Edition (1903)

Second, the image convolution operation:

The image can be viewed as a convolution window is moved in another region of large image area of ​​each window are covered by a value obtained as a center dot pixel output value.

The window is moved from left to right, top to bottom.

Window can be understood as a two-dimensional matrix specified size, which has pre-specified value.

Third, the program description:

There are two ways Blur demo image convolution operation (3x3 mean blurred).

  1. Code.
  2. Direct API calls OpenCV: Blur ()
  • Blur () function is defined:
void blur( InputArray    src, 
           OutputArray   dst,
           Size          ksize, 
           Point         anchor = Point(-1,-1),
           int           borderType = BORDER_DEFAULT 
         );
  • borderTypes ranges:
enum BorderTypes {
    BORDER_CONSTANT    = 0,     // `iiiiii|abcdefgh|iiiiiii`  with some specified `i`
    BORDER_REPLICATE   = 1,     // `aaaaaa|abcdefgh|hhhhhhh`
    BORDER_REFLECT     = 2,     // `fedcba|abcdefgh|hgfedcb`
    BORDER_WRAP        = 3,     // `cdefgh|abcdefgh|abcdefg`
    BORDER_REFLECT_101 = 4,     // `gfedcb|abcdefgh|gfedcba`
    BORDER_TRANSPARENT = 5,     // `uvwxyz|abcdefgh|ijklmno`

    BORDER_REFLECT101  = BORDER_REFLECT_101, // same as BORDER_REFLECT_101
    BORDER_DEFAULT     = BORDER_REFLECT_101, // same as BORDER_REFLECT_101
    BORDER_ISOLATED    = 16 // do not look outside of ROI
};

The last presentation of the effect is almost no difference.

Fourth, the program code:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {
	Mat src = imread("../images/test.jpg");
	if (src.empty()) {
		printf("不能加载图像!\n");
		return -1;
	}
	namedWindow("1--原图", WINDOW_AUTOSIZE);
	imshow("1--原图", src);

	int h = src.rows;
	int w = src.cols;

	// 代码实现:3x3 均值模糊
	Mat dst = src.clone();
	for (int row = 1; row < h - 1; row++) {
		for (int col = 1; col < w - 1; col++) {
			// 卷积过程中取周围的 3x3 个像素(包括自身)
			Vec3b p1 = src.at<Vec3b>(row - 1, col - 1);
			Vec3b p2 = src.at<Vec3b>(row - 1, col);
			Vec3b p3 = src.at<Vec3b>(row - 1, col + 1);
			Vec3b p4 = src.at<Vec3b>(row, col - 1);
			Vec3b p5 = src.at<Vec3b>(row, col);
			Vec3b p6 = src.at<Vec3b>(row, col + 1);
			Vec3b p7 = src.at<Vec3b>(row + 1, col - 1);
			Vec3b p8 = src.at<Vec3b>(row + 1, col);
			Vec3b p9 = src.at<Vec3b>(row + 1, col + 1);

			// 分通道取值相加
			int b = p1[0] + p2[0] + p3[0] + p4[0] + p5[0] + p6[0] + p7[0] + p8[0] + p9[0];
			int g = p1[1] + p2[1] + p3[1] + p4[1] + p5[1] + p6[1] + p7[1] + p8[1] + p9[1];
			int r = p1[2] + p2[2] + p3[2] + p4[2] + p5[2] + p6[2] + p7[2] + p8[2] + p9[2];

			// 分通道求均值
			dst.at<Vec3b>(row, col)[0] = saturate_cast<uchar>(b / 9);
			dst.at<Vec3b>(row, col)[1] = saturate_cast<uchar>(g / 9);
			dst.at<Vec3b>(row, col)[2] = saturate_cast<uchar>(r / 9);
		}
	}
	imshow("2--blur(代码实现)", dst);

	// 直接调用 OpenCV API: 3x3 均值模糊
	Mat dst_opencv;
	blur(src, dst_opencv, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
	imshow("3--blur(OpenCV API)", dst_opencv);

	waitKey(0);
	return 0;
}

Fifth, the operating results:

Guess you like

Origin blog.csdn.net/kingkee/article/details/93590176