I’m getting crazy with this problem in OpenCv. I have to read a raw image, saved as an array in a binary file, and save it in the cvMat structure prepared in opencv. I did, and seems it works, I can access to the data. But I can’t plot it. It appears as a lot of diagonal lines. I have printed the numbers in the data matrix, and seems to be correct, except that some 0’s appear sometimes. This tell me that something with the ‘step’ field is running but I have no idea.
I write here the code I have, I hope some of you understand what is happening. Thanks a lot in advance! Omg.
#include <stdio.h>//printf;fread...
#include <stdlib.h>//malloc,calloc ...
#include <cv.h>
#include <highgui.h>
// READING IMAGES
int read_raw_image(FILE* filehandler,float*** datamatrix,int* height,int* width);
//THIS READ THE RAW FILE SAVING THE DATA IN A MATRIX [HEIGHT][WIDTH]
char image_file[]="IR.raw";
CvMat image;
//RUBBISH FOR READ THE IMAGE
FILE *Fimage;
int height,width;
float **pimage=(float**)malloc(sizeof(float**));
if((Fimage=fopen(image_file,"rb"))!= NULL)
{
printf("\n\tFile: %s was opened correctly\n",image_file);
read_raw_image(Fimage,&pimage,&height,&width);
fclose(Fimage);
}
else {printf("\n\tFile: %s wasn't opened correctly\n",image_file);return 0;}
//NOW THE PROBLEM
image=cvMat(height,width,CV_32FC1,*pimage);//HEIGHT=640; WIDTH=480; THE NUMBERS ARE FLOATS
printf("%i",image.step);//THE NUMBER GIVEN IS 1920 (480*4)
int i,j;
for(j = 0;j<2;j++)//print just two rows
{
for(i = 0;i<480;i++)
{
printf("%f\n",cvmGet(image,j,i));/The numbers showed are well, except some 0's in the middle
if(*(Aux->data.fl+(j*640)+i)<10)
{
printf("piiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii\n");//MAKE EASY TO SEE THE 0'S
}
}
printf("fila:%i\n",j);//Print the number of row at the end
}
//AND NOW PLOT IT!
cvNamedWindow("win1",CV_WINDOW_AUTOSIZE);
cvShowImage("win1",&image);
cvWaitKey(0);
return 0;
I have no idea what can be the problem with the image.step values. I test to changed it. When I add more, some 0’s dissapear, but the image is no printed also.
Thanks for your help. Omg
P.D: The plot… I can’t post images!
Finally I achieve the solutions thanks to Dimitar. By some reason,each row finished with a float 0’s, this made an offset in each line, represented in the plot as four black dots. Since each line is now displaced, the result was a displaced 4 black dots, that seemed a diagonal line. The explanation for this 0’s at the end should be the way I saved the array (It doesn’t appear in the code) as a 2D dinamically allocated array, where first I allocated the number of rows, and later each row separatedly. However, the best way to save the images is in a 1D array, allocated just once. When you put it into the cvMat or IplImage, the code runs by rows and columns only with the knowledge of number of rows, number of cols and step (the length in bytes of each row). Also, if I changed the step in the original problem, by 16bytes, I obtain the 0’s were not displaces, and stand as a black column in to the right.
Also it’s important to take care that raw files, and in the case of IR cameras, the numbers into the array have to be scaled to a 0-to-1 scale in order to be represented as grayscale plot.
Thanks for your help all.