OpenCV fill image noise by neighborhood pixels

Source: Internet
Author: User
Tags sprintf

The demo is a pixel value of 0 labeled as noise, the picture is a grayscale (can be modified by itself to RGB), fill the 8 neighborhood pixel mean fill, only fix the noise

#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.P
        Ng ", 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; }

Original:

After correction:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.