PandA-2024.02
sobel.c
Go to the documentation of this file.
1 /* sobel.c */
2 #include <float.h>
3 #include "mypgm.h"
4 
5 int sobel(unsigned char *image1, unsigned char *image2, unsigned int x_size, unsigned int y_size)
6  /* Spatial filtering of image data */
7  /* Sobel filter (horizontal differentiation */
8  /* Input: image1[y][x] ---- Outout: image2[y][x] */
9 {
10  /* Definition of Sobel filter in horizontal direction */
11  int weight[3][3] = {{ -1, 0, 1 },
12  { -2, 0, 2 },
13  { -1, 0, 1 }};
14  double pixel_value;
15  double min, max;
16  int x, y, i, j; /* Loop variable */
17 
18  /* Maximum values calculation after filtering*/
19  min = DBL_MAX;
20  max = -DBL_MAX;
21  for (y = 1; y < y_size - 1; y++) {
22  for (x = 1; x < x_size - 1; x++) {
23  pixel_value = 0.0;
24  for (j = -1; j <= 1; j++) {
25  for (i = -1; i <= 1; i++) {
26  pixel_value += weight[j + 1][i + 1] * (*(image1 + (y + j) + (x + i) * MAX_IMAGESIZE));
27  }
28  }
29  if (pixel_value < min) min = pixel_value;
30  if (pixel_value > max) max = pixel_value;
31  }
32  }
33  if ((int)(max - min) == 0) {
34  return -1;
35  }
36 
37  /* Initialization of image2[y][x] */
38  for (y = 0; y < y_size; y++) {
39  for (x = 0; x < x_size; x++) {
40  *(image2 + y + x * MAX_IMAGESIZE) = 0;
41  }
42  }
43  /* Generation of image2 after linear transformtion */
44  for (y = 1; y < y_size - 1; y++) {
45  for (x = 1; x < x_size - 1; x++) {
46  pixel_value = 0.0;
47  for (j = -1; j <= 1; j++) {
48  for (i = -1; i <= 1; i++) {
49  pixel_value += weight[j + 1][i + 1] * (*(image1 + (y + j) + (x + i) * MAX_IMAGESIZE));
50  }
51  }
52  pixel_value = MAX_BRIGHTNESS * (pixel_value - min) / (max - min);
53  *(image2 + y + x * MAX_IMAGESIZE) = (unsigned char)pixel_value;
54  }
55  }
56 
57  return 0;
58 }
#define MAX_IMAGESIZE
Definition: mypgm.h:7
int sobel(unsigned char *image1, unsigned char *image2, unsigned int x_size, unsigned int y_size)
Definition: sobel.c:5
#define min(x, y)
#define MAX_BRIGHTNESS
Definition: mypgm.h:8
#define max
Definition: backprop.h:17
x
Return the smallest n such that 2^n >= _x.

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13