I need c source code to gaussion blur and image. The image has values from 0 to 1.0.
- User
I need c source code to gaussion blur and image. The image has values from 0 to 1.0.
- Assistant
Here is a C source code that applies Gaussian blur to an image with values ranging from 0 to 1.0:
#include <stdio.h> #include <stdlib.h> #include <math.h> // Function to clamp values between 0 and 1 float clamp(float value) { return value < 0 ? 0 : (value > 1 ? 1 : value); } // Function to apply Gaussian blur to an image void gaussianBlur(float* image, int width, int height, float sigma) { // Create a temporary image to store the blurred result float* blurredImage = (float*)malloc(width * height * sizeof(float)); float kernelSize = sigma * 6; int radius = kernelSize / 2; // Create the Gaussian kernel float* kernel = (float*)malloc(kernelSize * sizeof(float)); float normalizationFactor = 0; for (int i = 0; i < kernelSize; i++) { float x = i - radius; kernel[i] = exp(-(x * x) / (2 * sigma * sigma)); normalizationFactor += kernel[i]; } for (int i = 0; i < kernelSize; i++) { kernel[i] /= normalizationFactor; } // Apply the convolution for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float blurredPixel = 0; for (int i = 0; i < kernelSize; i++) { int pixelX = x + i - radius; // Handle out-of-bounds pixels by clamping to the image edges pixelX = clamp(pixelX); blurredPixel += image[pixelX + y * width] * kernel[i]; } // Store the blurred pixel in the temporary image blurredImage[x + y * width] = blurredPixel; } } // Copy the blurred image back to the original image for (int i = 0; i < width * height; i++) { image[i] = blurredImage[i]; } // Free the memory free(blurredImage); free(kernel); } int main() { // Example usage int width = 3; int height = 3; float sigma = 1.0; float image[] = {0.2, 0.1, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.4}; gaussianBlur(image, width, height, sigma); // Print the blurred image for (int i = 0; i < width * height; i++) { printf("%.2f ", image[i]); if ((i + 1) % width == 0) { printf("\n"); } } return 0; }This code defines a function
gaussianBlurthat applies Gaussian blur to an image represented as a 1D array of floats. Theclampfunction is used to handle out-of-bounds pixels by clamping them to the image edges. Themainfunction provides an example usage, where a 3x3 image is blurred with a sigma value of 1.0, and the resulting image is printed to the console. - User
That code is wrong. the clamp function is being applied to array index.
- Assistant
Apologies for the mistake. You are correct, the
clampfunction should be applied to the array index, not the pixel value. Here's the corrected code:#include <stdio.h> #include <stdlib.h> #include <math.h> // Function to clamp values between 0 and size-1 int clamp(int value, int size) { return value < 0 ? 0 : (value > size - 1 ? size - 1 : value); } // Function to apply Gaussian blur to an image void gaussianBlur(float* image, int width, int height, float sigma) { // Create a temporary image to store the blurred result float* blurredImage = (float*)malloc(width * height * sizeof(float)); float kernelSize = sigma * 6; int radius = kernelSize / 2; // Create the Gaussian kernel float* kernel = (float*)malloc(kernelSize * sizeof(float)); float normalizationFactor = 0; for (int i = 0; i < kernelSize; i++) { float x = i - radius; kernel[i] = exp(-(x * x) / (2 * sigma * sigma)); normalizationFactor += kernel[i]; } for (int i = 0; i < kernelSize; i++) { kernel[i] /= normalizationFactor; } // Apply the convolution for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { float blurredPixel = 0; for (int i = 0; i < kernelSize; i++) { int pixelX = x + i - radius; // Handle out-of-bounds pixels by clamping to the image edges pixelX = clamp(pixelX, width); blurredPixel += image[pixelX + y * width] * kernel[i]; } // Store the blurred pixel in the temporary image blurredImage[x + y * width] = blurredPixel; } } // Copy the blurred image back to the original image for (int i = 0; i < width * height; i++) { image[i] = blurredImage[i]; } // Free the memory free(blurredImage); free(kernel); } int main() { // Example usage int width = 3; int height = 3; float sigma = 1.0; float image[] = {0.2, 0.1, 0.3, 0.5, 0.8, 0.6, 0.7, 0.9, 0.4}; gaussianBlur(image, width, height, sigma); // Print the blurred image for (int i = 0; i < width * height; i++) { printf("%.2f ", image[i]); if ((i + 1) % width == 0) { printf("\n"); } } return 0; }In this updated code, the
clampfunction takes an additional argumentsize, which represents the size of the image (width or height). Theclampedfunction ensures that the pixel indexpixelXremains within the bounds of the image size.I apologize for the confusion caused by the previous code. This version should work correctly.