2

Possible Duplicate:
Pixel access in OpenCV 2.2

I was wondering how would I read a pixel value (in integer/float format) in C++ using Mat class?

Lots of people have asked the same question, but there are no specific WORKING answers for this.

I have the following code which compiles but gives NO proper result.

void Block(cv::Mat &image)
{
    for(int row = 0; row < image.rows; ++row)
    {

        for (int col = 0; col < image.cols; ++col)
     {
            cout<<image.at<int>(row, col)<<endl ;

        }
    }

}

The above code prints garbage values.

Community
  • 1
  • 1
garak
  • 4,713
  • 9
  • 39
  • 56
  • What do you mean garbage? What do you expect it to print? Have you tried a trivial input image, like 1x2? How about running under valgrind to check for memory access problems? – John Zwinck Jun 25 '11 at 16:52
  • 5
    Exact duplicate: http://stackoverflow.com/q/4742251/176769 – karlphillip Jun 25 '11 at 16:57
  • you did not put there the right cast at image.at<> (Sadly, OpenCV does not complain for that...). You can check the right cast here: [(link)](http://stackoverflow.com/questions/13484475/what-are-the-upper-and-lower-limits-of-pixel-values-in-opencv) – Barney Szabolcs Nov 23 '12 at 02:27

2 Answers2

5

This is a very good question. Wiki helps you when you know the type and number of channels the matrix. If you dont, then you need a switch statement. Here is a simple code example that prints values/pixels of almost any type of matrix:

// Main print method which includes the switch for types    
void printMat(const Mat& M){
        switch ( (M.dataend-M.datastart) / (M.cols*M.rows*M.channels())){

        case sizeof(char):
             printMatTemplate<unsigned char>(M,true);
             break;
        case sizeof(float):
             printMatTemplate<float>(M,false);
             break;
        case sizeof(double):
             printMatTemplate<double>(M,false);
             break;
        }
    }

// Print template using printf("%d") for integers and %g for floats

template <typename T>  
void printMatTemplate(const Mat& M, bool isInt = true){
    if (M.empty()){
       printf("Empty Matrix\n");
       return;
    }
    if ((M.elemSize()/M.channels()) != sizeof(T)){
       printf("Wrong matrix type. Cannot print\n");
       return;
    }
    int cols = M.cols;
    int rows = M.rows;
    int chan = M.channels();

    char printf_fmt[20];
    if (isInt)
       sprintf_s(printf_fmt,"%%d,");
    else
       sprintf_s(printf_fmt,"%%0.5g,");

    if (chan > 1){
        // Print multi channel array
        for (int i = 0; i < rows; i++){
            for (int j = 0; j < cols; j++){         
                printf("(");
                const T* Pix = &M.at<T>(i,j);
                for (int c = 0; c < chan; c++){
                   printf(printf_fmt,Pix[c]);
                }
                printf(")");
            }
            printf("\n");
        }
        printf("-----------------\n");          
    }
    else {
        // Single channel
        for (int i = 0; i < rows; i++){
            const T* Mi = M.ptr<T>(i);
            for (int j = 0; j < cols; j++){
               printf(printf_fmt,Mi[j]);
            }
            printf("\n");
        }
        printf("\n");
    }
}
DanielHsH
  • 4,287
  • 3
  • 30
  • 36
0

Sometimes I refer to this.. Visit http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html by Gady Adam

shernshiou
  • 518
  • 2
  • 10