用OpenCV随意写的对图像的处理2

#include<opencv2/core/core.hpp>
#include"MyBlur.hpp"

using namespace cv;

Mat MyMeanFilter(Mat& inputMat,int oddNum){

    Mat dstImg(inputMat.rows,inputMat.cols,inputMat.type());
    
    for(int i=0;i<inputMat.rows;i++){
        for(int j=0;j<inputMat.cols;j++){
            
           

            int mid = oddNum/2;//size 5x5 7x7 9x9 ... odd x odd
            int r = i-mid;
            int c = j-mid;
           
            int sumB = 0;
            int sumG = 0;
            int sumR = 0;
           
            for(int x=0;x<oddNum;x++){
               
                for(int y=0;y<oddNum;y++){

                   
                    //dealing of overflow of boundary
                    //eq: overflow region of elements is 0(zero)!(I think)
                    if(r + x < 0 || c + y < 0 ||r+x > inputMat.rows-1 || c+y > inputMat.cols-1){
                        continue;
                    }
                    
                    sumB += inputMat.at<Vec3b>(r+x,c+y)[0];
                    sumG += inputMat.at<Vec3b>(r+x,c+y)[1];
                    sumR += inputMat.at<Vec3b>(r+x,c+y)[2];
                }
            }

            sumB -= inputMat.at<Vec3b>(i,j)[0];
            sumG -= inputMat.at<Vec3b>(i,j)[1];
            sumR -= inputMat.at<Vec3b>(i,j)[2];
            int dividend = oddNum * oddNum - 1;
            dstImg.at<Vec3b>(i,j)[0] = saturate_cast<uchar>(sumB/dividend);
            dstImg.at<Vec3b>(i,j)[1] = saturate_cast<uchar>(sumG/dividend);
            dstImg.at<Vec3b>(i,j)[2] = saturate_cast<uchar>(sumR/dividend);

            

        }
    }
    return dstImg;
}


#include"OpenCVLib2.hpp"
using namespace cv;



int main(){

    Mat img = imread("p1.jpg");
    imshow("Origin",img);
    
    Mat&& dstImg = MyMeanFilter(img,7);
    imshow("Effect1",dstImg);

    dstImg = MyMeanFilter(img,17);
    imshow("Effect2",dstImg);

    waitKey(0);

    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值