Problem in cvGet2D function
//Program to use an image having 4 characters(eg : Z,L,5,T) of size 128x128 and store its pixel //values in a 1D array input_data
include "opencv2/core/core_c.h"
include "opencv2/highgui/highgui_c.h"
include <stdio.h>
include <stdlib.h>
define size 128
define neurons 16384
int input_data[4*neurons];
int main( int argc, const char** argv )
{
unsigned char pixels_array[size][size];
int height,total_width,per_image_width,i,counter=0,image_count=0,u,v;
FILE *fp;
IplImage* image;
CvScalar s;
char input_image[16];
printf("\nEnter the name of image\n");
scanf("%s",input_image);
//input image is 512x128 i.e 4 characters of 128x128 arranged horizontally
image = cvLoadImage(input_image,CV_LOAD_IMAGE_GRAYSCALE);
if(!image)
{
printf("Cannot read image file");
return -1;
}
// get the image data
height = image->height;
total_width = image->width;
printf("Processing a %dx%d image\n",height,total_width);
image_count = total_width / size ; //gives the number of characters in image strip
per_image_width = total_width / image_count; //this is definitely 128
printf("\nNo of images in plate : %d, Per image width : %d\n",image_count,per_image_width);
fp = fopen("actual.txt","w");
for(i=0;i<image_count;i++)
{
counter = per_image_width * i;
for(u=0;u<height;u++)
{
for(v= counter ; (v < (counter + per_image_width)) && (v < total_width) ; v+=1)
{
//this is how I am extracting pixel values s=cvGet2D(image,u,v); pixels_array[u][v]=s.val[0];
if (pixels_array[u][v]>127)
input_data[(neurons*i)+ u*height + v] = 1 ;
else
input_data[(neurons*i)+u*height +v] = -1;
fprintf(fp,"%d",input_data[(neurons*i)+u*height+v]);
}
fprintf(fp,"\n");
}
fprintf(fp,"\n@@@@@@@@@@@This is the end of image number %d@@@@@@@@@@@@\n",i);
}
fclose(fp);
cvReleaseImage(&image);
return 0;
}
When i see the resultant matrix in a file, i can see the first character clearly. But the second character does not print completely. It fails in its last iteration ie i=1, u=127, v=161 The error thrown is as follows:
OpenCV Error: Bad argument (unrecognized or unsupported array type) in cvPtr2D, file /home/varun/MTP1/opencv-2.4.6.1/modules/core/src/array.cpp, line 1830 terminate called after throwing an instance of 'cv::Exception' what(): /home/varun/MTP1/opencv-2.4.6.1/modules/core/src/array.cpp:1830: error: (-5) unrecognized or unsupported array type in function cvPtr2D
Aborted (core dumped)
I checked in array.cpp of OpenCV/modules/core/src directory containing cvGet2D but did not understand completely. Can somebody tell what the problem is? Thanks.