I have some code to try to run a sobel edge detection program for images. I am confusded on how to use an image as input with the argc and argv arguments in main for the code.  Here is the code: #include #include #include #define ROWS 100 #define COLS 100 // Sobel kernels int sobel_x[3][3] = { {-1, 0, 1},                        {-2, 0, 2},                        {-1, 0, 1} }; int sobel_y[3][3] = { {-1, -2, -1},                        { 0,  0,  0},                        { 1,  2,  1} }; // Function to apply Sobel edge detection void sobel_edge_detection(int image[ROWS][COLS], int output[ROWS][COLS]) {     int i, j, x, y;     int Gx, Gy, gradient;     for (i = 1; i < ROWS - 1; i++) {         for (j = 1; j < COLS - 1; j++) {             Gx = Gy = 0;             // Convolve image with Sobel kernels             for (x = -1; x <= 1; x++) {                 for (y = -1; y <= 1; y++) {                     Gx += image[i + x][j + y] * sobel_x[x + 1][y + 1];                     Gy += image[i + x][j + y] * sobel_y[x + 1][y + 1];                 }             }             // Compute gradient magnitude             gradient = abs(Gx) + abs(Gy);             // Thresholding             output[i][j] = (gradient > 100) ? 255 : 0;         }     } } // Function to read image from a file void read_image(const char* filename, int image[ROWS][COLS]) {     FILE *file = fopen(filename, "rb");     if (file == NULL) {         printf("Error: Unable to open file\n");         exit(1);     }     // Read pixel data     fread(image, sizeof(int), ROWS * COLS, file);     fclose(file); } // Function to write image to a file void write_image(const char* filename, int image[ROWS][COLS]) {     FILE *file = fopen(filename, "wb");          for (int i = 0; i < ROWS; i++) {         for (int j = 0; j < COLS; j++) {             fprintf("%d ", output_image[i][j]);         }         fprintf("\n");     }     fclose(file); } int main(int argc, char *argv[]) {     if (argc != 2) {         printf("Usage: %s \n", argv[0]);         return 1;     }     int input_image[ROWS][COLS];     int output_image[ROWS][COLS];     // Read image from file     read_image(argv[1], input_image);     // Apply Sobel edge detection     sobel_edge_detection(input_image, output_image);     write_image("edge_output.png", output_image);     return 0; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

I have some code to try to run a sobel edge detection program for images. I am confusded on how to use an image as input with the argc and argv arguments in main for the code. 

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define ROWS 100
#define COLS 100

// Sobel kernels
int sobel_x[3][3] = { {-1, 0, 1},
                       {-2, 0, 2},
                       {-1, 0, 1} };

int sobel_y[3][3] = { {-1, -2, -1},
                       { 0,  0,  0},
                       { 1,  2,  1} };

// Function to apply Sobel edge detection
void sobel_edge_detection(int image[ROWS][COLS], int output[ROWS][COLS]) {
    int i, j, x, y;
    int Gx, Gy, gradient;

    for (i = 1; i < ROWS - 1; i++) {
        for (j = 1; j < COLS - 1; j++) {
            Gx = Gy = 0;

            // Convolve image with Sobel kernels
            for (x = -1; x <= 1; x++) {
                for (y = -1; y <= 1; y++) {
                    Gx += image[i + x][j + y] * sobel_x[x + 1][y + 1];
                    Gy += image[i + x][j + y] * sobel_y[x + 1][y + 1];
                }
            }

            // Compute gradient magnitude
            gradient = abs(Gx) + abs(Gy);

            // Thresholding
            output[i][j] = (gradient > 100) ? 255 : 0;
        }
    }
}

// Function to read image from a file
void read_image(const char* filename, int image[ROWS][COLS]) {
    FILE *file = fopen(filename, "rb");
    if (file == NULL) {
        printf("Error: Unable to open file\n");
        exit(1);
    }

    // Read pixel data
    fread(image, sizeof(int), ROWS * COLS, file);

    fclose(file);
}

// Function to write image to a file
void write_image(const char* filename, int image[ROWS][COLS]) {
    FILE *file = fopen(filename, "wb");
    
    for (int i = 0; i < ROWS; i++) {
        for (int j = 0; j < COLS; j++) {
            fprintf("%d ", output_image[i][j]);
        }
        fprintf("\n");
    }


    fclose(file);
}


int main(int argc, char *argv[]) {
    if (argc != 2) {
        printf("Usage: %s <image_file>\n", argv[0]);
        return 1;
    }

    int input_image[ROWS][COLS];
    int output_image[ROWS][COLS];

    // Read image from file
    read_image(argv[1], input_image);

    // Apply Sobel edge detection
    sobel_edge_detection(input_image, output_image);

    write_image("edge_output.png", output_image);

    return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education