So far this is what I've got:
What I'm trying to do is read in a PPM, store it in an array (done), then prompt the user to enter a threshold. Then detect the edges in an image and output to a new internal array image where pixels that are part of an edgeare white, other pixels are black.
I've been given this formula:
grad = abs(im(x+1, y) - im(x-1, y)) + abs(im(x, y+1) - im(x, y-1))
and am told to compare it to a threshold then sum grad over the colour channels. Any pointers would be welcome. Thanks.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxheight 1080
#define maxwidth 1920
#define RGB_COMPONENT_COLOUR 255
#define pgmtype "P2"
#define ppmtype "P3"
typedef struct
{
int red, green, blue;
} PPMPixel;
typedef struct
{
int x, y;
//PPMPixel *data;
} PPMImage;
typedef struct
{
int rgb_comp_colour;
char filetype[3];
int height;
int width;
} PPMHead;
PPMHead head[3];
PPMPixel r;
PPMPixel data; //Defines pointer to PPMPixel
PPMPixel **EdgeArray;
void edge_detection()
{
int i, j;
FILE *fed;
char fnameed[100];
printf("\tEnter first file's name: \n");
scanf("%s", fnameed);
fseek(stdin,0,SEEK_END);
fed = fopen(fnameed, "r");
if (fed == NULL)
{
printf("\tError while opening the file\n");
}
else
{
printf("\tWriting in %s\n", fnameed);
}
fscanf(fed, "%s %d %d %d", head[3].filetype, &head[3].width, &head[3].height, &head[3].rgb_comp_colour);
printf("%s %d %d %d", head[3].filetype, head[3].width, head[3].height, head[3].rgb_comp_colour);
EdgeArray = (PPMPixel **)malloc(head[3].height*sizeof(PPMPixel*)); //Points to malloc
if((EdgeArray == NULL))
{
printf("\tError allocating memory to the array\n");
}
else
{
printf("\tMemory allocated to the PPM array sucessfully\n");
}
for (i=0;i<head[3].height;i++)
{
EdgeArray[i] = (PPMPixel *)malloc(head[3].width*sizeof(PPMPixel));
}
//Initialising each element
for (j=0;j<head[3].height;j++)
{
for (i=0;i<head[3].width;i++)
{
fscanf(fed, "%d %d %d ", &r.red, &r.green, &r.blue);
PPMPixel* data = &EdgeArray[j][i];
data->red = r.red;
data->green = r.green;
data->blue = r.blue;
}
}
}