read image as array - save array as image
Hello , I just started to study openCV and I am trying to figure how to properly read the image as array.
First of all , when I am loading the image using imread , what is the type ? Always unsigned char?
So,for example, I am trying to do:
...
Mat grayImage;
...
unsigned char *myData = grayImage.data;
int width = grayImage.cols;
int height = grayImage.rows;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
cout << "Mat[ " << i * width + j << " ] = " << (myData[ i * width + j ]) << "\t";
}
cout << endl;
}
I am receiving as output symbols and letters.
Also, if I want to store an array as an image what steps must I follow?
(I forgot to mention that I want to use pointers in order to access data)
even though you can do
cout << grayImage
because ofunsigned char *myData
cout print out chars (symbol and letter) use cast if you want numbers.At the end to use pointers in order to access data you have to be sure that grayImage is continuous. Check the doc for more information about how to scan images .
@pklab:Thanks for the help.So,the proper way to use pointers in order to access data is (after checking the continuous ) is to use
const float* ptr1 = grayImage.ptr<float>(i);
in the loop ?And what about accessing the data outside the loop? ( because ptr1 is defined in the loop according to [this] http://docs.opencv.org/3.0.0/d3/d63/c...Too many things in the question...
imread()
outputs uchar matrices as images usually take 0-255 rangeimwrite()
is there. But if you have a different type of array (say float) or you just want to save your array preserving it structure, you can also useFileStorage
class or use custom functions to save it to .csv files (or even binary files). The search bar of the forum will help you