2019-06-27(day021)

python

import cv2 as cv
import numpy as np
def my_blur(img):

    h, w, chan = img.shape
    result = np.copy(img)
    for i in range(1, h - 1, 1):
        for j in range(1, w - 1, 1):
            v1 = img[i][j]
            v2 = img[i][j - 1]
            v3 = img[i][j + 1]

            v4 = img[i - 1][j]
            v5 = img[i - 1][j + 1]
            v6 = img[i - 1][j - 1]

            v7 = img[i + 1][j]
            v8 = img[i + 1][j - 1]
            v9 = img[i + 1][j + 1]

            b = v1[0] + v2[0] + v3[0] + v4[0] + v5[0] + v6[0] + v7[0] + v8[0] + v9[0]
            g = v1[1] + v2[1] + v3[1] + v4[1] + v5[1] + v6[1] + v7[1] + v8[1] + v9[1]
            r = v1[2] + v2[2] + v3[2] + v4[2] + v5[2] + v6[2] + v7[2] + v8[2] + v9[2]

            result[i][j] = [b // 9, g // 9, r // 9]
    cv.imshow("my_blur", result)
img = cv.imread("../mm.jpg")
my_blur(img)
cv.imshow("input", img)
dst = cv.blur(img, (15, 15))
cv.imshow("cv.blur", dst)
cv.waitKey(0)
cv.destroyAllWindows()

c++

#include "all.h"
using namespace std;
using namespace cv;
void my_blur(Mat img, Mat result) {
    int row = img.rows;
    int height = img.cols;
    //Mat result = img.clone();
    for (int i = 1; i < row - 1; i++) {
        for (int j = 1; j < height - 1; j++) {
            Vec3b v1 = img.at<Vec3b>(i, j);
            Vec3b v2 = img.at<Vec3b>(i, j - 1);
            Vec3b v3 = img.at<Vec3b>(i, j + 1);
            Vec3b v4 = img.at<Vec3b>(i - 1, j);
            Vec3b v5 = img.at<Vec3b>(i - 1, j - 1);
            Vec3b v6 = img.at<Vec3b>(i - 1, j + 1);
            Vec3b v7 = img.at<Vec3b>(i + 1, j);
            Vec3b v8 = img.at<Vec3b>(i + 1, j - 1);
            Vec3b v9 = img.at<Vec3b>(i + 1, j + 1);

            int b = v1[0] + v2[0] + v3[0] + v4[0] + v5[0] + v6[0] + v7[0] + v8[0] + v9[0];
            int g = v1[1] + v2[1] + v3[1] + v4[1] + v5[1] + v6[1] + v7[1] + v8[1] + v9[1];
            int r = v1[2] + v2[2] + v3[2] + v4[2] + v5[2] + v6[2] + v7[2] + v8[2] + v9[2];
            /*cv::saturate_cast的作用是:防止溢出*/
            result.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(b / 9);
            result.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(g / 9);
            result.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(r / 9);

        }
    }
    //imshow("my_blur", result);
};
void MyClass::day021() {
    Mat img = myRead("mm.jpg"), result, cv_blur;
    result = img.clone();
    if (img.empty()) {
        printf("can't open image");
        return;
    }
    namedWindow("input", WINDOW_AUTOSIZE);
    imshow("input", img);
    my_blur(img, result);
    namedWindow("my_blur", WINDOW_AUTOSIZE);
    imshow("my_blur", result);
    blur(img, cv_blur, Size(15, 15), Point(-1, -1), 4);
    namedWindow("cv_blur", WINDOW_AUTOSIZE);
    imshow("cv_blur", cv_blur);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值