# Convolution Offset and Factor in Frequency Domain

See this link. The following Convolution in Spatial domain code is written in C#.

private void SafeImageConvolution(Bitmap image, ConvMatrix fmat)
{
//Avoid division by 0
if (fmat.Factor == 0)
return;

Bitmap srcImage = (Bitmap)image.Clone();

int x, y, filterx, filtery;
int s = fmat.Size / 2;
int r, g, b;
Color tempPix;

for (y = s; y < srcImage.Height - s; y++)
{
for (x = s; x < srcImage.Width - s; x++)
{
r = g = b = 0;

// Convolution
for (filtery = 0; filtery < fmat.Size; filtery++)
{
for (filterx = 0; filterx < fmat.Size; filterx++)
{
tempPix = srcImage.GetPixel(x + filterx - s, y + filtery - s);

r += fmat.Matrix[filtery, filterx] * tempPix.R;
g += fmat.Matrix[filtery, filterx] * tempPix.G;
b += fmat.Matrix[filtery, filterx] * tempPix.B;
}
}

r = Math.Min(Math.Max((r / fmat.Factor) + fmat.Offset, 0), 255);
g = Math.Min(Math.Max((g / fmat.Factor) + fmat.Offset, 0), 255);
b = Math.Min(Math.Max((b / fmat.Factor) + fmat.Offset, 0), 255);

image.SetPixel(x, y, Color.FromArgb(r, g, b));
}
}
}


Note the following lines:

    r = Math.Min(Math.Max((r / fmat.Factor) + fmat.Offset, 0), 255);
g = Math.Min(Math.Max((g / fmat.Factor) + fmat.Offset, 0), 255);
b = Math.Min(Math.Max((b / fmat.Factor) + fmat.Offset, 0), 255);


these lines are keeping the values of Red, Green, and Blue between 0 and 255. We are able change the values of Factor and Offset to control the contrast/brightness of the output image.

How can we achieve the same in frequency domain? I.e. how do we change the values of factor and offset in Frequency domain?

We know that a convolution operation in the Spatial domain is equal to the multiplication operation in Frequency domain. In frequency domain we are multiplying two Complex numbers. Which part of those complex numbers represent factor and offset?

Those lines that keep the range of the R,G,B channels within the valid [0,255] range work on individual pixels. There is a comparison between the pixel value and 0 / 255 levels. Unfortunately, this operation requires the exact pixel value to be known. However the Fourier transform mixes all the individual pixel values into all frequency bins and unless explicitly (and inefficiently) inverted back for a given pixel, this value is not accessible.

So it's not possible to limit the individual pixel values in the frequency domain.