Draw rotated sine image - c#

I'm working with 2D Fourier transforms, and I have a method that will output the following result:
The code looks like this:
private Bitmap PaintSin(int s, int n)
{
Data = new int[Width, Height];
Bitmap buffer = new Bitmap(Width, Height);
double inner = (2 * Math.PI * s) / n;
BitmapData originalData = buffer.LockBits(
new Rectangle(0, 0, buffer.Width, buffer.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
for (int i = 0; i < buffer.Width; i++)
{
for (int j = 0; j < buffer.Height; j++)
{
double val;
int c = 0;
if (j == 0)
{
val = Math.Sin(inner * i);
val += 1;
val *= 128;
val = val > 255 ? 255 : val;
c = (int)val;
Color col = Color.FromArgb(c, c, c);
SetPixel(originalData, i, j, col);
}
else
SetPixel(originalData, i, j, GetColor(originalData, i, 0));
Data[i, j] = c;
}
}
buffer.UnlockBits(originalData);
return buffer;
}
Now, I'm trying to think of a formula that will accept an angle and will out put an image where the solid lines are at the given angle.
For example, if I inputted 45 Degrees, the result would be:
Thank you for any help!
Here is the SetPixel code if that is needed:
private unsafe void SetPixel(BitmapData originalData, int x, int y, Color color)
{
//set the number of bytes per pixel
int pixelSize = 3;
//get the data from the original image
byte* oRow = (byte*)originalData.Scan0 + (y * originalData.Stride);
//set the new image's pixel to the grayscale version
oRow[x * pixelSize] = color.B; //B
oRow[x * pixelSize + 1] = color.G; //G
oRow[x * pixelSize + 2] = color.R; //R
}

This should be possible by using a rotated coordinate system. Transformation of i and j is as following:
x = i * cos(angle) - j * sin(angle);
y = j * cos(angle) + i * sin(angle);
Note: I'm not sure about degree/radians here, so adjust angle so it fits to the unit that cos and sin do need. Also, you might have to negate the angle depending on your desired rotation direction.
In fact, you only need x which replaces your use of i. We'll precompute sin(angle) and cos(angle) because these are quite costy operations we don't want in inner loops. Additionaly, your optimization is removed as we can't draw only one line and repeat it:
[...]
// double angle = ...
double cos_angle = cos(angle);
double sin_angle = sin(angle);
for (int i = 0; i < buffer.Width; i++)
{
for (int j = 0; j < buffer.Height; j++)
{
double val;
double x;
int c = 0;
x = i * cos_angle - j * sin_angle;
val = Math.Sin(inner * x);
val += 1;
val *= 128;
val = val > 255 ? 255 : val;
c = (int)val;
Color col = Color.FromArgb(c, c, c);
SetPixel(originalData, i, j, col);
Data[i, j] = c;
}
}

You can write a simple function rotate(x,y,angle) and use its result in SetPixel. You can google for rotation matrix.
A call with angle = 0, should produce default output.

Related

Image convolution in spatial domain

I am trying to replicate the outcome of this link using linear convolution in spatial-domain.
Images are first converted to 2d double arrays and then convolved. Image and kernel are of the same size. The image is padded before convolution and cropped accordingly after the convolution.
As compared to the FFT-based convolution, the output is weird and incorrect.
How can I solve the issue?
Note that I obtained the following image output from Matlab which matches my C# FFT output:
.
Update-1: Following #Ben Voigt's comment, I changed the Rescale() function to replace 255.0 with 1 and thus the output is improved substantially. But, still, the output doesn't match the FFT output (which is the correct one).
.
Update-2: Following #Cris Luengo's comment, I have padded the image by stitching and then performed spatial convolution. The outcome has been as follows:
So, the output is worse than the previous one. But, this has a similarity with the 2nd output of the linked answer which means a circular convolution is not the solution.
.
Update-3: I have used the Sum() function proposed by #Cris Luengo's answer. The result is a more improved version of **Update-1**:
But, it is still not 100% similar to the FFT version.
.
Update-4: Following #Cris Luengo's comment, I have subtracted the two outcomes to see the difference:
,
1. spatial minus frequency domain
2. frequency minus spatial domain
Looks like, the difference is substantial which means, spatial convolution is not being done correctly.
.
Source Code:
(Notify me if you need more source code to see.)
public static double[,] LinearConvolutionSpatial(double[,] image, double[,] mask)
{
int maskWidth = mask.GetLength(0);
int maskHeight = mask.GetLength(1);
double[,] paddedImage = ImagePadder.Pad(image, maskWidth);
double[,] conv = Convolution.ConvolutionSpatial(paddedImage, mask);
int cropSize = (maskWidth/2);
double[,] cropped = ImageCropper.Crop(conv, cropSize);
return conv;
}
static double[,] ConvolutionSpatial(double[,] paddedImage1, double[,] mask1)
{
int imageWidth = paddedImage1.GetLength(0);
int imageHeight = paddedImage1.GetLength(1);
int maskWidth = mask1.GetLength(0);
int maskHeight = mask1.GetLength(1);
int convWidth = imageWidth - ((maskWidth / 2) * 2);
int convHeight = imageHeight - ((maskHeight / 2) * 2);
double[,] convolve = new double[convWidth, convHeight];
for (int y = 0; y < convHeight; y++)
{
for (int x = 0; x < convWidth; x++)
{
int startX = x;
int startY = y;
convolve[x, y] = Sum(paddedImage1, mask1, startX, startY);
}
}
Rescale(convolve);
return convolve;
}
static double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
{
double sum = 0;
int maskWidth = mask1.GetLength(0);
int maskHeight = mask1.GetLength(1);
for (int y = startY; y < (startY + maskHeight); y++)
{
for (int x = startX; x < (startX + maskWidth); x++)
{
double img = paddedImage1[x, y];
double msk = mask1[x - startX, y - startY];
sum = sum + (img * msk);
}
}
return sum;
}
static void Rescale(double[,] convolve)
{
int imageWidth = convolve.GetLength(0);
int imageHeight = convolve.GetLength(1);
double maxAmp = 0.0;
for (int j = 0; j < imageHeight; j++)
{
for (int i = 0; i < imageWidth; i++)
{
maxAmp = Math.Max(maxAmp, convolve[i, j]);
}
}
double scale = 1.0 / maxAmp;
for (int j = 0; j < imageHeight; j++)
{
for (int i = 0; i < imageWidth; i++)
{
double d = convolve[i, j] * scale;
convolve[i, j] = d;
}
}
}
public static Bitmap ConvolveInFrequencyDomain(Bitmap image1, Bitmap kernel1)
{
Bitmap outcome = null;
Bitmap image = (Bitmap)image1.Clone();
Bitmap kernel = (Bitmap)kernel1.Clone();
//linear convolution: sum.
//circular convolution: max
uint paddedWidth = Tools.ToNextPow2((uint)(image.Width + kernel.Width));
uint paddedHeight = Tools.ToNextPow2((uint)(image.Height + kernel.Height));
Bitmap paddedImage = ImagePadder.Pad(image, (int)paddedWidth, (int)paddedHeight);
Bitmap paddedKernel = ImagePadder.Pad(kernel, (int)paddedWidth, (int)paddedHeight);
Complex[,] cpxImage = ImageDataConverter.ToComplex(paddedImage);
Complex[,] cpxKernel = ImageDataConverter.ToComplex(paddedKernel);
// call the complex function
Complex[,] convolve = Convolve(cpxImage, cpxKernel);
outcome = ImageDataConverter.ToBitmap(convolve);
outcome = ImageCropper.Crop(outcome, (kernel.Width/2)+1);
return outcome;
}
Your current output looks more like the auto-correlation function than the convolution of Lena with herself. I think the issue might be in your Sum function.
If you look at the definition of the convolution sum, you'll see that the kernel (or the image, doesn't matter) is mirrored:
sum_m( f[n-m] g[m] )
For the one function, m appears with a plus sign, and for the other it appears with a minus sign.
You'll need to modify your Sum function to read the mask1 image in the right order:
static double Sum(double[,] paddedImage1, double[,] mask1, int startX, int startY)
{
double sum = 0;
int maskWidth = mask1.GetLength(0);
int maskHeight = mask1.GetLength(1);
for (int y = startY; y < (startY + maskHeight); y++)
{
for (int x = startX; x < (startX + maskWidth); x++)
{
double img = paddedImage1[x, y];
double msk = mask1[maskWidth - x + startX - 1, maskHeight - y + startY - 1];
sum = sum + (img * msk);
}
}
return sum;
}
The other option is to pass a mirrored version of mask1 to this function.
I have found the solution from this link. The main clue was to introduce an offset and a factor.
factor is the sum of all values in the kernel.
offset is an arbitrary value to fix the output further.
.
#Cris Luengo's answer also raised a valid point.
.
The following source code is supplied in the given link:
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));
}
}
}

EmguCV - Convert color image to rg chromaticity

Does EmguCV provide any built-in function for converting a color image into RG Chromaticity (see Wikipedia link) ?
Thanks in advance.
I could not find any built-in method in EmguCV at least on v3.1 . So , I ended up doing the old way.
Image<Bgr, Byte> img_live = Live_Mat.ToImage<Bgr, Byte>();
for (int i = 0; i < Live_Mat.Height; i++)
{
for (int j = 0; j < Live_Mat.Width; j++)
{
float blue = img_live.Data[i, j, 0];
float green = img_live.Data[i, j, 1];
float red = img_live.Data[i, j, 2];
double sum = red + blue + green;
double r = red / sum;
double g = green / sum;
double b = blue / sum;
if (sum == 0)
{
r = 0;
g = 0;
b = 0;
}
img_live.Data[i, j, 0] = System.Convert.ToByte(Math.Round((b * AWB_rgb_mult), 0));
img_live.Data[i, j, 1] = System.Convert.ToByte(Math.Round((g * AWB_rgb_mult), 0));
img_live.Data[i, j, 2] = System.Convert.ToByte(Math.Round((r * AWB_rgb_mult), 0));
}

C# Convolution filter for any size matrix (1x1, 3x3, 5x5, ...) not fully applied

I'm making a convolution filter for my project and I managed to make it for any size of matrix but as it gets bigger I noticed that not all bits are changed.
Here are the pictures showing the problem:
First one is the original
Filter: Blur 9x9
Filter: EdgeDetection 9x9:
As you can see, there is a little stripe that is never changed and as the matrix gets bigger, the stripe also gets bigger (in 3x3 it wasn't visible)
My convolution matrix class:
public class ConvMatrix
{
public int Factor = 1;
public int Height, Width;
public int Offset = 0;
public int[,] Arr;
//later I assign functions to set these variables
...
}
The filter function:
Bitmap Conv3x3(Bitmap b, ConvMatrix m)
{
if (0 == m.Factor)
return b;
Bitmap bSrc = (Bitmap)b.Clone();
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;
unsafe
{
byte* p = (byte*)(void*)Scan0;
byte* pSrc = (byte*)(void*)SrcScan0;
int nOffset = stride - b.Width * m.Width;
int nWidth = b.Width - (m.Size-1);
int nHeight = b.Height - (m.Size-2);
int nPixel = 0;
for (int y = 0; y < nHeight; y++)
{
for (int x = 0; x < nWidth; x++)
{
for (int r = 0; r < m.Height; r++)
{
nPixel = 0;
for (int i = 0; i < m.Width; i++)
for (int j = 0; j < m.Height; j++)
{
nPixel += (pSrc[(m.Width * (i + 1)) - 1 - r + stride * j] * m.Arr[j, i]);
}
nPixel /= m.Factor;
nPixel += m.Offset;
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[(m.Width * (m.Height / 2 + 1)) - 1 - r + stride * (m.Height / 2)] = (byte)nPixel;
}
p += m.Width;
pSrc += m.Width;
}
p += nOffset;
pSrc += nOffset;
}
}
b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);
return b;
}
Please help
The problem is that your code explicitly stops short of the edges. The calculation for the limits for your outer loops (nWidth and nHeight) shouldn't involve the size of the matrix, they should be equal to the size of your bitmap.
When you do this, if you imagine what happens when you lay the center point of your matrix over each pixel in this case (because you need to read from all sizes of the pixel) the matrix will partially be outside of the image near the edges.
There are a few approaches as to what to do near the edges, but a reasonable one is to clamp the coordinates to the edges. I.e. when you would end up reading a pixel from outside the bitmap, just get the nearest pixel from the edge (size or corner).
I also don't understand why you need five loops - you seem to be looping through the height of the matrix twice. That doesn't look right. All in all the general structure should be something like this:
for (int y = 0; y < bitmap.Height; y++) {
for (int x = 0; x < bitmap.Width; x++) {
int sum = 0;
for (int matrixY = -matrix.Height/2; matrixY < matrix.Height/2; matrixY++)
for (int matrixX = -matrix.Width/2; matrixX < matrix.Width/2; matrixX++) {
// these coordinates will be outside the bitmap near all edges
int sourceX = x + matrixX;
int sourceY = y + matrixY;
if (sourceX < 0)
sourceX = 0;
if (sourceX >= bitmap.Width)
sourceX = bitmap.Width - 1;
if (sourceY < 0)
sourceY = 0;
if (sourceY >= bitmap.Height)
sourceY = bitmap.Height - 1;
sum += source[sourceX, sourceY];
}
}
// factor and clamp sum
destination[x, y] = sum;
}
}
You might need an extra loop to handle each color channel which need to be processed separately. I couldn't immediately see where in your code you might be doing that from all the cryptic variables.

RGB to Ycbcr conversion

I am doing image processing and I have a 3D array with the rgb values of an image and I am trying to convery those values into ycbcr ( I made a copy of the rgb array and called it ycbcr, and
public static void rgb2ycbcr(System.Drawing.Bitmap bmp, ref byte[, ,] arrayrgb, ref byte[, ,] arrayycbcr)
{
byte Y;
byte Cb;
byte Cr;
for (int i = 1; i < (bmp.Height + 1); i++) //don't worry about bmp.height/width+2 its for my project
{
for (int j = 1; j < (bmp.Width + 1); j++)
{
byte R = arrayrgb[i, j, 0];
byte G = arrayrgb[i, j, 1];
byte B = arrayrgb[i, j, 2];
Y = (byte)((0.257 * R) + (0.504 * G) + (0.098 * B) + 16);
Cb = (byte)(-(0.148 * R) - (0.291 * G) + (0.439 * B) + 128);
Cr = (byte)((0.439 * R) - (0.368 * G) - (0.071 * B) + 128);
arrayycbcr[i, j, 0] = Y;
arrayycbcr[i, j, 1] = Cb;
arrayycbcr[i, j, 2] = Cr;
}
}
}
the problem is I am not getting the same values for ycbcr as I would get in matlab when I use rgb2ycbcr, is there something missing in my code?
Faster and acurate code.
Output values ​​between 0 and 255 (JPG formula)
width = bmp.Width;
height = bmp.Height;
yData = new byte[width, height]; //luma
bData = new byte[width, height]; //Cb
rData = new byte[width, height]; //Cr
unsafe
{
BitmapData bitmapData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bmp.PixelFormat);
int heightInPixels = bitmapData.Height;
int widthInBytes = width * 3;
byte* ptrFirstPixel = (byte*)bitmapData.Scan0;
//Convert to YCbCr
for (int y = 0; y < heightInPixels; y++)
{
byte* currentLine = ptrFirstPixel + (y * bitmapData.Stride);
for (int x = 0; x < width; x++)
{
int xPor3 = x * 3;
float blue = currentLine[xPor3++];
float green = currentLine[xPor3++];
float red = currentLine[xPor3];
yData[x, y] = (byte)((0.299 * red) + (0.587 * green) + (0.114 * blue));
bData[x, y] = (byte)(128 - (0.168736 * red) + (0.331264 * green) + (0.5 * blue));
rData[x, y] = (byte)(128 + (0.5 * red) + (0.418688 * green) + (0.081312 * blue));
}
}
bmp.UnlockBits(bitmapData);
}
Convert R/G/B to uint before assign.
uint R =ConvertToUint(arrayrgb[i, j, 0]);
uint G =ConvertToUint(arrayrgb[i, j, 1]);
uint B =ConvertToUint(arrayrgb[i, j, 2]);

Changing the tint of a bitmap while preserving the overall brightness

I'm trying to write a function that will let me red-shift or blue-shift a bitmap while preserving the overall brightness of the image. Basically, a fully red-shifted bitmap would have the same brightness as the original but be thoroughly red-tinted (i.e. the G and B values would be equal for all pixels). Same for blue-tinting (but with R and G equal). The degree of spectrum shifting needs to vary from 0 to 1.
Thanks in advance.
Here is the effect I was looking for (crappy JPEG, sorry):
alt text http://www.freeimagehosting.net/uploads/d15ff241ca.jpg
The image in the middle is the original, and the side images are fully red-shifted, partially red-shifted, partially blue-shifted and fully blue-shifted, respectively.
And here is the function that produces this effect:
public void RedBlueShift(Bitmap bmp, double factor)
{
byte R = 0;
byte G = 0;
byte B = 0;
byte Rmax = 0;
byte Gmax = 0;
byte Bmax = 0;
double avg = 0;
double normal = 0;
if (factor > 1)
{
factor = 1;
}
else if (factor < -1)
{
factor = -1;
}
for (int x = 0; x < bmp.Width; x++)
{
for (int y = 0; y < bmp.Height; y++)
{
Color color = bmp.GetPixel(x, y);
R = color.R;
G = color.G;
B = color.B;
avg = (double)(R + G + B) / 3;
normal = avg / 255.0; // to preserve overall intensity
if (factor < 0) // red-tinted:
{
if (normal < .5)
{
Rmax = (byte)((normal / .5) * 255);
Gmax = 0;
Bmax = 0;
}
else
{
Rmax = 255;
Gmax = (byte)(((normal - .5) * 2) * 255);
Bmax = Gmax;
}
R = (byte)((double)R - ((double)(R - Rmax) * -factor));
G = (byte)((double)G - ((double)(G - Gmax) * -factor));
B = (byte)((double)B - ((double)(B - Bmax) * -factor));
}
else if (factor > 0) // blue-tinted:
{
if (normal < .5)
{
Rmax = 0;
Gmax = 0;
Bmax = (byte)((normal / .5) * 255);
}
else
{
Rmax = (byte)(((normal - .5) * 2) * 255);
Gmax = Rmax;
Bmax = 255;
}
R = (byte)((double)R - ((double)(R - Rmax) * factor));
G = (byte)((double)G - ((double)(G - Gmax) * factor));
B = (byte)((double)B - ((double)(B - Bmax) * factor));
}
color = Color.FromArgb(R, G, B);
bmp.SetPixel(x, y, color);
}
}
}
You'd use the ColorMatrix class for this. There's a good tutorial available in this project.

Categories

Resources