Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C, edit int main() to read and write a picture with sobel edge detection. Source code below: #include #include #include #include #include /* Do

image text in transcribed

In C, edit int main() to read and write a picture with sobel edge detection. Source code below:

#include #include #include #include #include

/* Do not modify write_pgm() or read_pgm() */ int write_pgm(char *file, void *image, uint32_t x, uint32_t y) { File *o;

if (!(o = fopen(file, "w"))) { perror(file);

return -1; }

fprintf(o, "P5 %u %u 255 ", x, y);

/* Assume input data is correctly formatted. * * There's no way to handle it, otherwise. */

if (fwrite(image, 1, x * y, o) != (x * y)) { perror("fwrite"); fclose(o);

return -1; }

fclose(o);

return 0; }

/* A better implementation of this function would read the image dimensions * * from the input and allocate the storage, setting x and y so that the * * user can determine the size of the file at runtime. In order to * * minimize complication, I've written this version to require the user to * * know the size of the image in advance. */ int read_pgm(char *file, void *image, uint32_t x, uint32_t y) { FILE *f; char s[80]; unsigned i, j;

if (!(f = fopen(file, "r"))) { perror(file);

return -1; }

if (!fgets(s, 80, f) || strncmp(s, "P5", 2)) { fprintf(stderr, "Expected P6 ");

return -1; }

/* Eat comments */ do { fgets(s, 80, f); } while (s[0] == '#');

if (sscanf(s, "%u %u", &i, &j) != 2 || i != x || j != y) { fprintf(stderr, "Expected x and y dimensions %u %u ", x, y); fclose(f);

return -1; }

/* Eat comments */ do { fgets(s, 80, f); } while (s[0] == '#');

if (strncmp(s, "255", 3)) { fprintf(stderr, "Expected 255 "); fclose(f);

return -1; }

if (fread(image, 1, x * y, f) != x * y) { perror("fread"); fclose(f);

return -1; }

fclose(f);

return 0; }

int main(int argc, char *argv[]) { int8_t image[1024][1024]; int8_t out[1024][1024]; /* Example usage of PGM functions */ /* This assumes that motorcycle.pgm is a pgm image of size 1024x1024 */ read_pgm("motorcycle.pgm", image, 1024, 1024);

/* After processing the image and storing your output in "out", write * * to motorcycle.edge.pgm. */ write_pgm("motorcycle.edge.pgm", out, 1024, 1024); return 0; }

image text in transcribed

image text in transcribed

One of the most important operations in image processing and computer vision is edge detection. A very simple and effective edge detector is the Sobel Filter. The Sobel Filter is a pair of 3 x 3 matrices which are convolved with the input image seperately then recombined. Specifically, the Sobel Filter is given by the pair of discrete convolutions 1 0 +1 1 0 +1 where I is the input image, Oz and Oy are the piecewise output images, and (described below). The x and y components are recombined with is the convolution operator n and a matrix M, the convolution M,-K Given a convo!tion matrix (a kernel) of size n given by M is TL That may look like some scary linear algebra, but it's actually very simple. Here's some pseudocode: for each row r in M for each column c in M accumulator 0 for each row j in K for each column i in K accumulator accumulator+ K[j][i] MEr (j - ceil(n/2))][c + (i -ceil(n/2))] M'[r][c]accumulator Positions in the matrix where the kernel only partially covers the matrix (e.g., the edges) have to be handled specially. For our purposes, we'll ignore those cells and simply assign 0 (zero) to the output This YouTube video gives some visual examples of how convolution works1: https://www.youtube. com/watch?v-CzFhWdM4ic I have provided source code that implements image reading and writing (and shows example usage) Starting with that code, write a program that takes the name of a PGM image on the command line, reads the image, applies a Sobel filter, and write the edge-detected image to disc with the file name sobel.pgm All input files with be greyscale PGM images of size 1024 x 1024 PGM is a very, very simple image format. Tools that can display PGM images in UNIX and UNIX-like environments include xv and gimp. In Windows, IrfanView can do the job You can ignore the division step described in the video, because our kernels sum to zero so the division is undefined (and unnecessary) One of the most important operations in image processing and computer vision is edge detection. A very simple and effective edge detector is the Sobel Filter. The Sobel Filter is a pair of 3 x 3 matrices which are convolved with the input image seperately then recombined. Specifically, the Sobel Filter is given by the pair of discrete convolutions 1 0 +1 1 0 +1 where I is the input image, Oz and Oy are the piecewise output images, and (described below). The x and y components are recombined with is the convolution operator n and a matrix M, the convolution M,-K Given a convo!tion matrix (a kernel) of size n given by M is TL That may look like some scary linear algebra, but it's actually very simple. Here's some pseudocode: for each row r in M for each column c in M accumulator 0 for each row j in K for each column i in K accumulator accumulator+ K[j][i] MEr (j - ceil(n/2))][c + (i -ceil(n/2))] M'[r][c]accumulator Positions in the matrix where the kernel only partially covers the matrix (e.g., the edges) have to be handled specially. For our purposes, we'll ignore those cells and simply assign 0 (zero) to the output This YouTube video gives some visual examples of how convolution works1: https://www.youtube. com/watch?v-CzFhWdM4ic I have provided source code that implements image reading and writing (and shows example usage) Starting with that code, write a program that takes the name of a PGM image on the command line, reads the image, applies a Sobel filter, and write the edge-detected image to disc with the file name sobel.pgm All input files with be greyscale PGM images of size 1024 x 1024 PGM is a very, very simple image format. Tools that can display PGM images in UNIX and UNIX-like environments include xv and gimp. In Windows, IrfanView can do the job You can ignore the division step described in the video, because our kernels sum to zero so the division is undefined (and unnecessary)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 22nd International Conference Dexa 2011 Toulouse France August/September 2011 Proceedings Part 1 Lncs 6860

Authors: Abdelkader Hameurlain ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2011th Edition

3642230873, 978-3642230875

More Books

Students also viewed these Databases questions

Question

1. Organize and support your main points

Answered: 1 week ago

Question

3. Move smoothly from point to point

Answered: 1 week ago

Question

5. Develop a strong introduction, a crucial part of all speeches

Answered: 1 week ago