Skip to content

About

This project is a rudimentary dive into computer vision, built entirely in C. It implements classic image-processing techniques such as grayscale conversion, sepia filter, color inversion, horizontal reflection, blur, and edge detection using the Sobel operator. While simple in design, this project demonstrates core principles behind how machines

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Image Filter Program

A command-line image processing tool written in C that applies various filters to images. The program supports multiple image formats (converted to BMP internally) and can download images from URLs.

Features

Image Filters

  • Grayscale (-g): Converts color images to grayscale
  • Sepia (-s): Applies a warm, vintage sepia tone effect
  • Blur (-b): Creates a blur effect using box blur algorithm
  • Reflection (-r): Horizontally mirrors the image
  • Edge Detection (-e): Detects edges using Sobel operators
  • Inversion (-i): Inverts all colors (negative effect)

Additional Features

  • Web Image Download (-w): Downloads images directly from URLs
  • Format Conversion: Automatically converts non-BMP images to BMP format
  • Multiple Format Support: Works with various image formats through stb_image library

Dependencies

Required Libraries

  • curl - for downloading images from URLs
  • stb_image.h - for reading various image formats
  • stb_image_write.h - for writing image files
  • Standard C libraries (stdio.h, stdlib.h, math.h, getopt.h)

Installation of Dependencies

Ubuntu/Debian

sudo apt-get update
sudo apt-get install libcurl4-openssl-dev

macOS (using Homebrew)

brew install curl

Windows

Download and install curl development libraries from the official curl website.

Compilation

gcc -o filter filter.c -lcurl -lm

Make sure you have the stb_image.h and stb_image_write.h header files in your project directory.

Usage

Basic Syntax

./filter [flag] input_file output_file

Download Image from Web

./filter -w [URL]

Filter Examples

Apply Grayscale Filter

./filter -g input.jpg output.bmp

Apply Sepia Filter

./filter -s photo.png sepia_photo.bmp

Apply Blur Filter

./filter -b image.bmp blurred_image.bmp

Reflect Image Horizontally

./filter -r photo.jpg reflected_photo.bmp

Apply Edge Detection

./filter -e landscape.png edges.bmp

Invert Colors

./filter -i portrait.jpg inverted_portrait.bmp

Download Image from URL

./filter -w https://example.com/image.jpg

Directory Structure

The program expects/creates the following directory structure:

./
├── filter (executable)
├── stb_image.h
├── stb_image_write.h
└── images/
    ├── temp.bmp (temporary conversion file)
    └── downloaded_image.* (downloaded images)

Make sure to create the images/ directory before running the program.

How It Works

Image Processing Pipeline

  1. Input Validation: Checks command-line arguments and filter flags
  2. File Reading: Reads the input image file
  3. Format Conversion: If the input isn't a 24-bit BMP, converts it using stb_image
  4. Filter Application: Applies the selected filter algorithm
  5. Output Writing: Saves the processed image as a BMP file

Filter Algorithms

Grayscale

Converts RGB values to grayscale by averaging the red, green, and blue components.

Sepia

Applies sepia tone using the standard sepia transformation matrix:

  • Red = 0.393R + 0.769G + 0.189*B
  • Green = 0.349R + 0.686G + 0.168*B
  • Blue = 0.272R + 0.534G + 0.131*B

Blur

Uses a box blur algorithm that averages each pixel with its surrounding neighbors.

Edge Detection

Implements Sobel edge detection using horizontal and vertical convolution kernels.

Reflection

Swaps pixels horizontally to create a mirror effect.

Inversion

Subtracts each color component from 255 to create a negative effect.

Error Handling

The program includes comprehensive error handling for:

  • Invalid command-line arguments
  • File I/O errors
  • Memory allocation failures
  • Network download errors
  • Unsupported image formats

Limitations

  • Output is always in BMP format
  • Requires 24-bit color depth for direct BMP processing
  • Web downloads require internet connectivity
  • Memory usage scales with image size

Exit Codes

  • 0: Success
  • 1: Invalid filter flag
  • 2: Multiple filters specified
  • 3: Incorrect number of arguments
  • 4: Cannot open input file
  • 5: Cannot create output file
  • 7: Insufficient memory

Examples

Process a JPEG image with blur filter

./filter -b vacation.jpg blurred_vacation.bmp

Download and process an image

# First download
./filter -w https://example.com/photo.jpg

# Then process the downloaded image
./filter -g ./images/downloaded_image.jpg gray_photo.bmp

About

This project is a rudimentary dive into computer vision, built entirely in C. It implements classic image-processing techniques such as grayscale conversion, sepia filter, color inversion, horizontal reflection, blur, and edge detection using the Sobel operator. While simple in design, this project demonstrates core principles behind how machines

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages