opencv通过邻域像素填补图片噪点

10 篇文章 0 订阅
9 篇文章 0 订阅

该demo为将像素值为0标注为噪点,图片为灰度图(可自行修改为RGB),填补方式为8邻域像素均值填补,只修复噪点

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

using namespace std;
using namespace cv;

int main() {
    char buf1[100];
    char buf2[100];
    vector<Point2i> NeihborPos;
    NeihborPos.push_back(Point2i(-1, 0));
    NeihborPos.push_back(Point2i(1, 0));
    NeihborPos.push_back(Point2i(0, -1));
    NeihborPos.push_back(Point2i(0, 1));
    NeihborPos.push_back(Point2i(-1, -1));
    NeihborPos.push_back(Point2i(-1, 1));
    NeihborPos.push_back(Point2i(1, -1));
    NeihborPos.push_back(Point2i(1, 1));
    int Neihbor_count=8;
    int CurrX=0,CurrY=0;
    for (int i=1;i<8;++i){
        sprintf(buf1,"/home/yangjunfeng/workspace_lj/workspace/clion_project/disp_new/disp_%d.png",i);
        sprintf(buf2,"/home/yangjunfeng/workspace_lj/workspace/clion_project/disp_new2/disp_%d.png",i);
        Mat src=imread(buf1,CV_LOAD_IMAGE_GRAYSCALE);
        Mat dst = Mat::zeros(src.size(), CV_8UC1);

        for(int i = 0; i < src.rows; ++i)
        {
            int bad_c_count=0;
            vector<Point2i> temp_local;
            uchar* s_data = src.ptr<uchar>(i);
            uchar* d_data = dst.ptr<uchar>(i);
            for(int j = 0; j < src.cols; ++j)
            {
                //temp_local.push_back(Point2i(j,i));
                if(s_data[j] != 0) d_data[j]=s_data[j];
                else{
                    temp_local.push_back(Point2i(j,i));
                    bad_c_count++;
                    int pixel_sum=0;
                    int nerbor_suit=0;
                    for(int k=0;k<Neihbor_count;++k){
                        CurrX=temp_local.at(bad_c_count-1).x+NeihborPos.at(k).x;
                        CurrY=temp_local.at(bad_c_count-1).y+NeihborPos.at(k).y;
                        if (CurrX>=0&&CurrX<src.cols&&CurrY>=0&&CurrY<src.rows&&src.at<uchar>(CurrY, CurrX)!=0){
                            pixel_sum+=src.at<uchar>(CurrY, CurrX);
                            nerbor_suit++;
                        }

                    }
                    //cout<<"pixel_sum:"<<pixel_sum<<endl<<"nerbor_suit:"<<nerbor_suit<<endl;
                    //float kkk=pixel_sum/nerbor_suit;
                    //cout<<kkk<<endl;
                    if (nerbor_suit!=0)
                        d_data[j]=int(pixel_sum/nerbor_suit);
                    //d_data[j]=255;
                }

            }
        }
        imwrite(buf2,dst);

    }


    std::cout << "Hello, World!" << std::endl;
    return 0;
}

原图:

这里写图片描述

这里写图片描述

修正后:
这里写图片描述

这里写图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值