### In C #### I need help finishing this question, I need it to read an store th
ID: 3561418 • Letter: #
Question
### In C ####
I need help finishing this question, I need it to read an store the pixel data of a PGM image. I can read through the nessescary parts in the header I just need help getting out of the header then using fread to insert the pixel data into an array Image_data. Image data also needs to be dynamically allocated using Malloc()
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main()
{
int type, width = 0, height = 0, data, counter = 0;
FILE* fp;
fp = fopen("test.pgm", "rb");
if(fp == NULL){
printf("unable to open file ");}
else{
fscanf(fp, "P%d %d %d %d *%s", &type,&width,&height,&data);
int length = (width*height);
int image_data[length];
printf("the length of the file is: %d ", length);
printf("%d %d %d %d ", type, width, height, data);
// from here how do I read into the actual pixel data of the file
fread(image_data, 3, length, fp);
printf("%d ", image_data);
}
fclose(fp);
}
Explanation / Answer
// load an image img=cvLoadImage(argv[1]); if(!img){ printf("Could not load image file: %s ",argv[1]); exit(0); } // get the image data height = img->height; width = img->width; step = img->widthStep; channels = img->nChannels; data = (uchar *)img->imageData; printf("Processing a %dx%d image with %d channels ",height,width,channels); // create a window cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); cvMoveWindow("mainWin", 100, 100); // invert the image for(i=0;iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.