Search code examples
c++opencvimage-processingimagefilter

Error in c++ openCv my very first code (median filter)


hi I just started c++/openCv and trying to write a median code i'm cinda confused......

EDIT2: OK thank to dear friends my first error is corrected now this is my new error :| I want to sort 9 elemented Mat file. could I use onother type for window not Mat file? how can I sort it corectly

the error refers to this line: std::sort(window.begin(), window.end()); error: request for member 'begin' in 'window', which is of non-class type 'cv::Mat [9]'| |36|error: request for member 'end' in 'window', which is of non-class type 'cv::Mat [9]'|

I exped matlab and i'm a complete noob at c++, this is my code:

using namespace std;
using namespace cv;
Mat img_gray,img;

int main ()
{

img = imread( "6.jpg", IMREAD_COLOR ); // Load an image
if( img.empty() )
    { return -1; }
cvtColor( img, img_gray, COLOR_BGR2GRAY );
int M = img.rows;
int N = img.cols;
cvNamedWindow("windows",WINDOW_AUTOSIZE);
imshow("windows",img);
for (int m = 2; m < M - 1; ++m)
    for (int n = 2; n < N - 1; ++n)
    {
        int k = 0;
        int tmpmedian = 0;
        //int window[9]={0};
        Mat window[9];
        for (int i = m - 1; i < m + 2; ++i){
            for (int j = n - 1; j < n + 2; ++j)
            {
                window[k++] = img_gray.at<uchar>(i, j);
            }
            std::sort(window.begin(), window.end());
            tmpmedian = window[5];
            fimg[m][n] = tmpmedian;
        }
    }
}

i'm a student and need this for my class project and I appreciate your responses thanks alot


Solution

  • In your double for loop, try this.

    int k = 0;
    int tmpmedian = 0;
    int window[9]={0};
    for (int i = m - 1; i < m + 2; ++i)
        for (int j = n - 1; j < n + 2; ++j)
            window[k++] = img_gray.at<uchar>(i, j);
    std::sort(std::begin(window), std::end(window));
    tmpmedian = window[4];
    fimg[m][n] = tmpmedian;
    
    1. Mat window[9] declares an array of 9 Mat type object. I don't think you'd want that. You just need an array of 9 int values. So what you need is int window[9].

    2. Your usage of std::begin() and std::end() is wrong. std::sort(std::begin(window), std::end(window)) is what you need.

    3. Array indices are zero-based. So your median number is stored at window[4] not window[5].