码迷,mamicode.com
首页 > 其他好文 > 详细

opencv:图像卷积

时间:2020-02-01 21:01:35      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:图像   could   result   ios   处理   turn   卷积   mat   开始   

卷积基本概念

技术图片

C++代码实现卷积

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

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    Mat src = imread("f:/images/lena.jpg");
    if (src.empty())
    {
        printf("Could not find the image!\n");
        return -1;
    }

    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);

    waitKey(0);
    destroyAllWindows();

    return 0;
}

blur函数

    Mat dst;
    /*
       blur参数:
            src:输入
            dst:输出
            ksize:卷积核大小
            anchor:锚定点,默认(-1,-1),中心位置(默认是卷积核大小除以2的位置)
            borderType:边缘处理方式,默认为BORDER_DEFAULT=4
    */
    blur(src, dst, Size(3, 3), Point(-1, -1), BORDER_DEFAULT);
    imshow("dst", dst);

卷积边缘处理

卷积处理的时候,边缘像素的填充方法:
技术图片

边缘在卷积开始前就填充好(知道卷积核大小之后)

技术图片

边缘填充 copyMakeBorder

    // 边缘填充 copyMakeBorder
    int border = 8;
    Mat border_m;
    copyMakeBorder(src, border_m, border, border, border, border, BORDER_WRAP, Scalar(0, 0, 255));
    imshow("border fill demo", border_m);

opencv:图像卷积

标签:图像   could   result   ios   处理   turn   卷积   mat   开始   

原文地址:https://www.cnblogs.com/wbyixx/p/12250034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!