10. 图像卷积

2020-03-11  本文已影响0人  逍遥才子
image.png
image.png
image.png
#include <opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv) {

    Mat src = imread("D:/HUANGHAI_WORK/tools/opencv4/opencv/sources/samples/data/home.jpg");


    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", src);

    int h = src.rows;
    int w = src.cols;
    Mat result = src.clone();
    for (int row = 1; row < h-1; row++)
    {
        for (int col = 1; col < w-1; col++)
        {
            //3x3卷积核
            int sb = src.at<Vec3b>(row, col)[0]
                +src.at<Vec3b>(row-1,col-1)[0]
                +src.at<Vec3b>(row-1,col)[0]
                +src.at<Vec3b>(row-1,col+1)[0]
                + src.at<Vec3b>(row, col-1)[0]
                + src.at<Vec3b>(row, col+1)[0]
                + src.at<Vec3b>(row+1, col-1)[0]
                + src.at<Vec3b>(row+1, col)[0]
                + src.at<Vec3b>(row+1, col+1)[0];
            int sg = src.at<Vec3b>(row, col)[1]
                + src.at<Vec3b>(row - 1, col - 1)[1]
                + src.at<Vec3b>(row - 1, col)[1]
                + src.at<Vec3b>(row - 1, col + 1)[1]
                + src.at<Vec3b>(row, col - 1)[1]
                + src.at<Vec3b>(row, col + 1)[1]
                + src.at<Vec3b>(row + 1, col - 1)[1]
                + src.at<Vec3b>(row + 1, col)[1]
                + src.at<Vec3b>(row + 1, col + 1)[1];
            int sr = src.at<Vec3b>(row, col)[2]
                + src.at<Vec3b>(row - 1, col - 1)[2]
                + src.at<Vec3b>(row - 1, col)[2]
                + src.at<Vec3b>(row - 1, col + 1)[2]
                + src.at<Vec3b>(row, col - 1)[2]
                + src.at<Vec3b>(row, col + 1)[2]
                + src.at<Vec3b>(row + 1, col - 1)[2]
                + src.at<Vec3b>(row + 1, col)[2]
                + src.at<Vec3b>(row + 1, col + 1)[2];
            result.at<Vec3b>(row, col)[0] = sb / 9;
            result.at<Vec3b>(row, col)[1] = sg / 9;
            result.at<Vec3b>(row, col)[2] = sr / 9;
        }
    }

    imshow("result", result);
    Mat dst;
    blur(src, dst, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
    imshow("blur-dst", dst);

    //边缘填充
    int border = 200;
    Mat borderM;
    copyMakeBorder(src, borderM, border, border, border, border, BORDER_DEFAULT);

    imshow("border fill demo", borderM);

    waitKey(0);
    destroyAllWindows();
    return 0;
}
image.png image.png
image.png
image.png
上一篇 下一篇

猜你喜欢

热点阅读