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; }

New Perspectives on HTML5, CSS3, and JavaScript
6th Edition
ISBN:9781305503922
Author:Patrick M. Carey
Publisher:Patrick M. Carey
Chapter14: Exploring Object-based Programming: Designing An Online Poker
Section14.1: Visual Overview: Custom Objects, Properties, And Methods
Problem 7QC
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
New Perspectives on HTML5, CSS3, and JavaScript
New Perspectives on HTML5, CSS3, and JavaScript
Computer Science
ISBN:
9781305503922
Author:
Patrick M. Carey
Publisher:
Cengage Learning