I am doing this to get each pixel value of Image and printing it on Console
include "stdafx.h"
include "opencv2/imgproc/imgproc.hpp"
include "opencv2/highgui/highgui.hpp"
include <stdlib.h>
include <stdio.h>
using namespace cv;
int main( int argc, char** argv )
{
IplImage *img = cvLoadImage("lena.jpg");
CvMat *mat = cvCreateMat(img->height,img->width,CV_32FC3 );
cvConvert( img, mat );
for(int i=0;i<10;i++)
{
for(int j=0;j<10;j++)
{
CvScalar scal = cvGet2D( mat,j,i);
printf( "(%.f,%.f,%.f) ",scal.val[0], scal.val[1], scal.val[2] );
}
printf("\n");
}
waitKey(1);
return 0;
}
Is there better way to get each pixel value along with there header and channel values in txt.files
I would recommend against using the C API if you can help it. The C++ API is much easier to use. As for storage of a matrix in a file, have a look at the FileStorage class available in OpenCV.
It's as easy as:
FileStorage fs("test.yml", FileStorage::WRITE);
Mat cameraMatrix = (Mat_<double>(3,3) << 1000, 0, 320, 0, 1000, 240, 0, 0, 1);
fs << "cameraMatrix" << cameraMatrix;
...
fs.release();
FileStorage fs("test.yml", FileStorage::READ);
Mat cameraMatrixFromFile;
fs["cameraMatrix"] >> cameraMatrixFromFile;
...
fs.release();
Hope that helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With