Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

how to iterate 5x5 window over mat image

hi, i am having problem while iterating a 5x5 window over a mat image. it iterates along column but it crashes when new row comes. what should i do. i am doing this to find local standard deviation.

here is the code:-

Mat block;

Mat inner = cv::imread("C:\\Users\\Intern-3\\Desktop\\processed\\edited.png",0);
int row = inner.rows; int col = inner.cols;
cv::imshow("Original Image", inner); // show the image

imwrite( "C:\\Users\\Intern-3\\Desktop\\processed\\original.jpg", inner );
cerr << inner.rows << endl;
cerr << inner.cols << endl;
row--;
col--;
cout<<"RowsLast = "<<endl<<" "<<inner.col(col)<<endl;
cout<<"-------------------\n"<<endl;

Mat outer = Mat(inner.rows+2, inner.cols+2, inner.type());
copyMakeBorder(inner,outer ,1,1,1,1,BORDER_REPLICATE);
row = outer.rows;
col=outer.cols;
cerr << outer.rows << endl;
cerr << outer.cols << endl;

cv::imshow("Processed Image", outer); // show the image
imwrite( "C:\\Users\\Intern-3\\Desktop\\processed\\processed.jpg", outer );
col--;
cout<<"RowsLast = "<<endl<<" "<<outer.col(col)<<endl;
cout<<"-------------------\n"<<endl;

block = (outer(Rect(0,0,4,4)));


for(int i=2;i<row+2;i++)
{
    for(int j=2;j<col+2;j++)
    {
            int sum1=0;
        int sum2=0;

        block = outer.rowRange(i-2,i+2).colRange(j-2,j+2);

        for(int x=0;x<5;x++)
        {
            for(int y=0;5<5;y++)
            {
                sum1=sum1+ block.at<uchar>(x, y);  // Addition of all elements
                sum2=sum2+block.at<uchar>(x,y)*block.at<uchar>(x,y); //Addition of all element square
                cout<<"Y = "<<y<<endl;
            }
        }

        double mean = sum1/25;  
        double variance=-(mean*mean -(sum2/25)); //Variance
        double deviation=sqrt(variance);         //Standard deviation

        //Storing deviation value at pixel position in output image
        block.at<uchar>(i-2,j-2)=deviation;  

    }
}

waitKey(0);

}

how to iterate 5x5 window over mat image

hi, i am having problem while iterating a 5x5 window over a mat image. it iterates along column but it crashes when new row comes. what should i do. i am doing this to find local standard deviation.

here is the code:-

Mat block;

Mat inner = cv::imread("C:\\Users\\Intern-3\\Desktop\\processed\\edited.png",0);
int row = inner.rows; int col = inner.cols;
cv::imshow("Original Image", inner); // show the image

imwrite( "C:\\Users\\Intern-3\\Desktop\\processed\\original.jpg", inner );
cerr << inner.rows << endl;
cerr << inner.cols << endl;
row--;
col--;
cout<<"RowsLast = "<<endl<<" "<<inner.col(col)<<endl;
cout<<"-------------------\n"<<endl;

Mat outer = Mat(inner.rows+2, inner.cols+2, inner.type());
copyMakeBorder(inner,outer ,1,1,1,1,BORDER_REPLICATE);
row = outer.rows;
col=outer.cols;
cerr << outer.rows << endl;
cerr << outer.cols << endl;

cv::imshow("Processed Image", outer); // show the image
imwrite( "C:\\Users\\Intern-3\\Desktop\\processed\\processed.jpg", outer );
col--;
cout<<"RowsLast = "<<endl<<" "<<outer.col(col)<<endl;
cout<<"-------------------\n"<<endl;

block = (outer(Rect(0,0,4,4)));


for(int i=2;i<row+2;i++)
{
    for(int j=2;j<col+2;j++)
    {
            int sum1=0;
        int sum2=0;

        block = outer.rowRange(i-2,i+2).colRange(j-2,j+2);

        for(int x=0;x<5;x++)
        {
            for(int y=0;5<5;y++)
y=0;y<5;y++)
            {
                sum1=sum1+ block.at<uchar>(x, y);  // Addition of all elements
                sum2=sum2+block.at<uchar>(x,y)*block.at<uchar>(x,y); //Addition of all element square
                cout<<"Y = "<<y<<endl;
            }
        }

        double mean = sum1/25;  
        double variance=-(mean*mean -(sum2/25)); //Variance
        double deviation=sqrt(variance);         //Standard deviation

        //Storing deviation value at pixel position in output image
        block.at<uchar>(i-2,j-2)=deviation;  

    }
}

waitKey(0);

}