0

I tried to get the N*N matrix. to do that i used following code but printing a one value all the time. any idea to take the correct value of matrix.

I checked the destination mat using imread function which is not null. countervalue. But in the printf comes \377 value which int count=0;

 Size s=destination.size();

    int count=0;
    for(int i=0 ;i<s.height; i++)
    {
        for (int j=0; j<s.width; j++) {
            unsigned char* byte = destination.ptr<unsigned char>(i,j);
            count++;
            printf("valeue %s ",byte);
            printf("\n");

        }
    }

output

valeue \377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377 .......
waruni guna
  • 69
  • 1
  • 11
  • 1
    what is the output of `cout< – Rishit Sanmukhani Nov 18 '15 at 06:22
  • @Rishit i still getting the same error. – waruni guna Nov 18 '15 at 08:29
  • I don't see any issue just by above code. Can you paste some more relevant code? what is output of `cout< – Rishit Sanmukhani Nov 18 '15 at 09:25
  • @Rishit when i prints my Mat object of destination i get the values 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 250, 173, 104, 187, 194, 193, 187, 181, 179, 178, 163, 152, 145, 134, 118, 118, 101, 83, 93, 77, 54, 60, 37, 54, 191, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255; Any idea to print these values – waruni guna Nov 18 '15 at 09:28

2 Answers2

1

ptr accepts as argument the row number. You can thus get the pointer to the starting element of each row by calling pointer on each i (outside inner loop).

Then you need %d (or %u) in your printf (or you can use cout). Check the code below:

#include <opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;

int main() 
{
    int N = 10;

    // Init matrix
    Mat destination(N, N, CV_8UC1);
    randu(destination, Scalar(0), Scalar(255));

    Size s = destination.size();
    int count = 0;
    for (int i = 0; i<s.height; i++)
    {
        unsigned char* pByte = destination.ptr<unsigned char>(i);
        for (int j = 0; j<s.width; j++) {
            count++;
            printf("value %d ", pByte[j]);
            printf("\n");

            //cout << "value " << int(pByte[j]) << endl;
        }
    }

    return(0);
}

You can see in my other answer other methods to get the value at specific position in a Mat.

Community
  • 1
  • 1
Miki
  • 40,887
  • 13
  • 123
  • 202
0

For printing the Mat values, you just need to iterate over Mat.

Here is some piece of code:

Mat in = imread("image.bmp",CV_LOAD_IMAGE_GRAYSCALE);
if(in.empty())
{
  puts("Cannot open image!");
  return -1;
}
//Print the values
for(int i=0;i<in.rows;i++)
{
    for(int j=0;j<in.cols;j++)
    {
        cout<<in.at<uchar>(i,j)<<" ";
    }
    cout<<endl;
}

Here basically I am loading the image, checking whether it is empty or not, and then printing the values.

As my image is grayscale image, I have used in.at<uchar>(i,j) where uchar means unsigned char.

Rishit Sanmukhani
  • 2,159
  • 16
  • 26