unmix and conditional average: eliminating aliasing and conditional mean

 

unmix

 The procedure used to eliminate the "pixel aliasing." The so-called aliasing pixel, is a value of the natural scene image, cmos edge line imaged onto the pixel, some pixels will immediately across the edge line.

Such a feature is a pixel which R, G, B three-color pixel gradient values ​​are inconsistent. For example, the CMOS, the color of each pixel has R, G, B three-color sensor, whose arrangement order is sorted from left to right.

So, if there is just one line of the inclined edge pixel position G, and the background of the edge line portion, a lower portion of the object, which is different then the three color gradient value, and dR> dB, otherwise dB> dR .. Like this

Pixel, we can by means of unmix will eliminate it. The method is very simple, that is, the same or similar pixel neighborhood of the pixel gradient pixel replacement. Here's the code:

Unmix Mat (const Mat & IMG, int neibourSize) 
{ 
    MAT imgCopy img.clone = (); 
    int = NR neibourSize / 2; 
    int cols = img.cols, rows = img.rows; 
    MAT kernel_x = (Mat_ <int> (. 1, 2) << - 1,1); 
    Mat kernel_y = (Mat_ <int> (2,1) << - 1,1); 
    Mat img_dx, img_dy; // predeclared gradient matrix X, Y directions 
    filter2D (img , img_dx, CV_32F, kernel_x); 
    filter2D (IMG, img_dy, CV_32F, kernel_y); 

    Mat Gradient = ABS (img_dx) + ABS (img_dy); 

    Mat Theta = getGradientDirect (img_dy, img_dx); // this function see: https: //www.cnblogs.com/phoenixdsg/p/11352623.html 

    Mat thetaGray; 
    cvtColor (Theta, thetaGray, COLOR_RGB2GRAY); // transformed into a gray scale image processing to facilitate 

    /// unmix () function 
    for (int i = nr; i <rows-nr; i ++ ) 
    {
        for (int J = NR; J <cols-NR; J ++) 
        { 
            a float * the pG = CV_MAT_ELEM2 (Gradient, a float, I, J); 
            a float R & lt = the pG [2], G = the pG [. 1], B = the pG [ 2]; 
            IF (!! R & lt B = G = R & lt || || B = G!) 
            { 
                Mat thetaGray tmp = (Rect (J-NR, NR-I, neibourSize, neibourSize)); 
                Mat TMPR = tmp.clone (); // neighboring pixels taken 
                float Pij = * (CV_MAT_ELEM2 (thetaGray , float, i, j)); // get the current pixel value 
                TMPR = ABS (TMPR-Pij is); 
                tmpr.at <a float> (NR, NR) =. 7; 

                Double MINV; 
                Point minloc; 
                minMaxLoc (TMPR, & MINV, 0, & minloc);  
                minloc = Point (NR, NR) -minLoc;
// << COUT "MINV =" << << MINV ";"
//                   <<"minLoc="<<minLoc<< endl;
                int ii=minLoc.y+i,jj=minLoc.x+j;
                for(int k=0;k<3;k++)
                {
                    imgCopy.ptr<Vec3b>(i)[j][k]=
                            img.ptr<Vec3b>(ii)[jj][k];
                }
            }
        }
    }
    return imgCopy;
}

  This function takes two parameters, a first image is to be processed, the second parameter is the neighborhood size, the size must be an odd number like 3,5,7. Function return value, that is, the result of unmix.

conditional average

Set the current pixel N 0 , which is a set of neighboring pixels is S, selected from the effective pixel S, {Ni}. Obtaining mean effective pixel N 0 ', and alternative N 0 .

{Wherein N = S I : | N I = N 0 | <T}. Where n is the effective number of pixels. Note that S always contains N 0 . The program will have the following effects:

1) In a homogeneous region, the smoothing out the small amount of noise, the relative value of the noise is less than T;

2) in the vicinity of the boundary area, the contrast is greater than T, just across the pixels at the boundary without treatment mean. So smoothing the image, only the sides of the border pixel mean for processing.

    This is different from ordinary fuzzy mean, even the ordinary mean fuzzy boundary has also been blurred;

3) it is only the noise smoothing a gradient range, but does not smooth out gradient;

4) in the texture area, if there is a small difference between texels (less than T), then the pixels will be smooth as the notification area, i.e. to eliminate texture.

     If the difference between texels greater than T, then the process is not smooth.

 

 

Code is as follows:

void conditionalAerage(Mat&img,int T)
{
    int rows=img.rows,cols=img.cols;
    int neighborSize=3;
    int nr=neighborSize/2;
    for(int i=nr;i<rows-nr;i++)
    {
        for(int j=nr;j<cols-nr;j++)
        {
            int ctn=0;
            vector<Vec3b> nv;
            Vec3b P0=img.at<Vec3b>(i,j);
            float P0m=float(P0[0]+P0[1]+P0[1])/3.;
            for(int in=i-nr;in<neighborSize;in++)
            {
                for(int jn=i-nr;jn<neighborSize;jn++)
                {
                    Vec3b Pn=img.at<Vec3b>(in,jn);
                    float Pnm=float(Pn[0]+Pn[1]+Pn[1])/3.;
                    if(abs(Pnm-P0m)<T)
                    {
                        nv.push_back(Pn);
                    }
                }
                Vec3b sum(0,0,0);
                ctn=nv.size();
                for(int v=0;v<ctn;v++)
                {
                    sum +=nv[v];
                }
                Vec3b aver=sum/ctn;
                for(int k=0;k<img.channels();k++)
                    img.at<Vec3b>(i,j)[k]=aver[k];
            }

        }
    }
}

  

 

Guess you like

Origin www.cnblogs.com/phoenixdsg/p/11359122.html