Skip to content

Instantly share code, notes, and snippets.

@connors511
Created September 21, 2011 16:49
Show Gist options
  • Save connors511/1232622 to your computer and use it in GitHub Desktop.
Save connors511/1232622 to your computer and use it in GitHub Desktop.
BOOL filter_2DConvolution(IMAGE* iImage, IMAGE* oImage, int F[3][3]) {
int i, row, col; // counters
int m = 3;
int normalization = 0;
int pixel, pos;
int lastPixel = iImage->Width * iImage->Height - 1;
printf("Filter:\n %d %d %d\n %d %d %d\n %d %d %d\n",
F[0][0], F[0][1], F[0][2],
F[1][0], F[1][1], F[1][2],
F[2][0], F[2][1], F[2][2]);
for (row = 0; row < m; row++)
{
for (col = 0; col < m; col++)
{
normalization += F[row][col];
}
}
printf("Normalization: %d\n", normalization);
for (i = 0; i < LENGTH(iImage->Pixels); i++)
{
pixel = pos = 0;
for (row = -(m - 1)/2.0; row <= (m - 1)/2.0; row++)
{
for (col = -(m - 1)/2.0; col <= (m - 1)/2.0; col++)
{
pos = i + row * iImage->Width + col;
if (pos >= 0 && pos <= lastPixel) // Check for top and bottom
{
if (i % iImage->Width == 0 && (pos + 1) % iImage->Width != 0) // check left
{
pixel += F[row][col] * iImage->Pixels[pos];
}
else if ( (i + 1) % iImage->Width == 0 && pos % iImage->Width != 0) // check right
{
pixel += F[row][col] * iImage->Pixels[pos];
}
}
}
}
oImage->Pixels[i] = pixel / normalization;
}
return TRUE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment