I am trying to create a texture (in a 3-D byte array) that is a coloured gabor patch. I am using OpenTK to map the texture. The texture mapping is working fine, but the texture that is created by my code below is not what I need.
The code I have come up with is as follows:
for (int x = 0; x < size; x++)
{
for (int y = 0; y < size; y++)
{
double sin_term = 0.5*(double)Math.Sin(10 * 3.14159 * ((double)x / (double)size));
sin_term += 0.5;
double gauss = 0.5+Math.Exp(-((Math.Pow(x,2)+Math.Pow(y,2))/(2*Math.Pow(sigma,2))));
double gabor = sin_term * gauss;
byteTexture2[j,i,0] = (byte)(((double)Colour.R * gabor));
byteTexture2[j,i,1] = (byte)(((double)Colour.G * gabor));
byteTexture2[j,i,2] = (byte)(((double)Colour.B * gabor));
}
}
My maths isn't alll that good, so I may be off track but I was trying to multiply the sine wave by the gaussian. The sine wave term seems to work OK by itself but the gaussian may be where it is having problems.
Any help would be much appreciated.
Have found MATLAB code for this problem but no c/c++/c# code
Thanks.
I recently coded up a Gabor filter kernel for use in OpenCV (using C++). Here is my code for the kernel:
/// compute Gabor filter kernels
for (int i = 0; i < h; i++){
x = i - 0.5*(h - 1);
for (int j = 0; j < h; j++) {
y = j - 0.5*(h - 1);
gaborKernelCos.at<float>(i, j) = exp((-16 / (h*h))*(x*x + y*y))*cos((2 * M_PI*w / h)*(x*cos(q) + y*sin(q))) / (h*h);
gaborKernelSin.at<float>(i, j) = exp((-16 / (h*h))*(x*x + y*y))*sin((2 * M_PI*w / h)*(x*cos(q) + y*sin(q))) / (h*h);
}
}
Where the input parameters are the kernel size h, wave number w, and filter orientation q. Note the wave number is related to the filter pixel wavelength by l = h/w. Also, my value for sigma is simply a constant multiple of h.
This shouldn't really produce anything wildly different from your code as far as I can tell. Does your value for sigma make sense? It should probably be at most sigma = 0.5*size.
Related
GitHub link: https://github.com/pthdaniel/ProjectileMotion/tree/master
Hey, I was working on a project where my app should calculate the datas with the initial velocity, angle and height of the motion, and now my only problem is with the path the projectile makes. I had an idea to make a 2D string array to simulate it, but either the idea itself or my calculations are incorrect. For the array I round the double type datas and then cast them to integers.
public Canvas(int height, int width)
{
this.height = height;
this.width = width;
board = new string[height+1, width+1]; //indexing needs more space
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
board[i, 0] = "|";
board[height - 1, j] = "-";
}
}
board[height - 1, 0] = "O";
board[(int)Math.Round(Program.p.Y0), 0] = "+";
for (int j = 1; j < width; j++)
{
board[Program.p.OtherPosition(j), j] = "+";
}
}
Here is the constructor of my Canvas class which fills up my string[,] board. The Program.p is the projectile itself, which has attributes like y0, which is the initial height. The OtherPosition takes in the j to calculate, where should the method place the * character, where the x position = j. Like this:
public int OtherPosition(int position)
{
y = y0 + v0y * (position / (2 * v0x)) - (g / 2) * Math.Pow(position / (v0x * 2), 2);
return (int)(Math.Round(Program.p.Ymax, 1) * 2)+1- 2 * ((int)Math.Round(y));
}
The canvas generation is the following:
static void GenerateCanvas(Projectile p)
{
int height = (int)(Math.Round(p.Ymax,5)*2);
int width = (int)(Math.Round(p.Xmax,5)*2);
board = new Canvas(height, width);
}
The *2 multiplier is there for the bigger size of the whole biard itself.
Can somebody help me to smoothen the path? Sometimes there are * characters underneath each other, which I have no idea how. Or what's another way of doing it?
I am using Unity 5 to create an isometric game. I have generated a grid of tiles and it works well. However, when I use two different tiles to fill in the grid (their image sizes are slightly different), I get gaps in between the tiles. The obvious solution would be to create the tiles so that they are all the same image size, but this would prevent me from creating anything on a tile that is larger than the size of a tile (eg. a tree).
Here are some images to demonstrate:
With only one type of tile:
With two types of tile:
This is the code I use to create the map:
private void CreateMap() {
float tileWidth;
float tileHeight;
int orderInLayer = 0;
SpriteRenderer r = floorTiles [0].GetComponent<SpriteRenderer> ();
tileWidth = r.bounds.max.x - r.bounds.min.x;
tileHeight = r.bounds.max.y - r.bounds.min.y;
for (int i = 0; i < map.GetLength(0); i++) {
orderInLayer += 1;
for (int j = 0; j < map.GetLength (1); j++) {
Vector2 position = new Vector2 ((j * tileWidth / 2) + (i * tileWidth / 2) + (tileWidth / 2), (j * tileHeight / 2) - (i * tileHeight / 2) + (tileHeight/ 2));
r = map[i,j].GetComponent<SpriteRenderer>();
r.sortingOrder = orderInLayer;
Instantiate(map[i, j], position, Quaternion.identity);
}
}
}
Any help would be greatly appreciated, I cannot seem to fix it!
You appear to be calculating a position for each of your tiles from scratch every time you create one. If you have 2 different sized tiles, then your calculation comes out different, hence the gaps in your tiles. This is because you're only using the width/height of the current tile, failing to take into account any previous tiles that may be a shorter/longer height/width.
Given you have varying heights AND widths you'll need a way to calculate the correct position for both to prevent gaps in the X and Y direction. I've mocked up something here, but it's untested. More of a concept(?) I guess.
float tileHeight = 0;
float tileWidth = 0;
Vector2 position = new Vector2(0,0);
Dictionary<int, float> HeightMap = new Dictionary<int, float>();
for (int iRow = 0; iRow < map.GetLength(0); iRow++)
{
position.x = 0;
orderInLayer += 1;
for (int jColumn = 0; jColumn < map.GetLength (1); jColumn++)
{
position.y = HeightMap[jColumn];
r = map[iRow, jColumn].GetComponent<SpriteRenderer>();
tileWidth = r.bounds.max.x - r.bounds.min.x;
tileHeight = r.bounds.max.y - r.bounds.min.y;
r.sortingOrder = orderInLayer;
position.x += tileWidth / 2;
position.y += tileHeight / 2;
Instantiate(map[iRow, jColumn], position, Quaternion.identity);
HeightMap[jColumn] = position.y;
}
}
I leave the best way of storing the height, or instantiating the contents of the HeightMap dictionary to however you see fit.
I have two images(original and noisy). I'm calculating PSNR. I kinda did it for color RGB images, but i don't know how to do it with grayscale. As i read, MSE calculation is different. For RGB i'm doing it like you can see in following code (I'm using Visual C#):
for (int i = 0; i < bmp1.Width; i++)
{
for (int j = 0; j < bmp1.Height; j++)
{
mseR += Math.Pow(bmp1.GetPixel(i, j).R - bmp2.GetPixel(i, j).R, 2);
mseG += Math.Pow(bmp1.GetPixel(i, j).G - bmp2.GetPixel(i, j).G, 2);
mseB += Math.Pow(bmp1.GetPixel(i, j).B - bmp2.GetPixel(i, j).B, 2);
}
}
mse = (mseR + mseG + mseB) / ((bmp1.Width * bmp1.Height) * 3);
Here I am manipulating with R,G,B of pixels.But i don't know what should i take in case of grayscale images. Can I use RGB aswell, because it actually gives some results, or i should take something else?
To make grayscale you can make the picture out of averages (no need to vary your implementation). I'm assuming your images are bmp1 = grayImage and bmp2 = noisy image.
for (int i = 0; i < bmp1.Width; i++)
{
for (int j = 0; j < bmp1.Height; j++)
{
// As a grayscale image has rthe same color on all RGB just pick one
int gray1 = bmp1.GetPixel(i, j).R;
int gray2 = bmp2.GetPixel(i, j).R;
double sum = Math.Pow(gray1 - gray2, 2)
mseGray += sum;
}
}
mse = (mseGray) / ((bmp1.Width * bmp1.Height) * 3);
Also getting pixels one at a time is a slow process look into using the indexes, and a optimization in the loop. It should give about a tenfold in performance.
You need to make the bitmap into an indexable img, I'm assuming its BitmapSource for this example. the interesting part is the loop and the index building and not the precode, the precode is just to make the image indexable.
var height = bmp1.Height;
var width = bmp1.Width;
var pixelBytes1 = new byte[height * width * 4];
var pixelBytes2 = new byte[height * width * 4];
bmp1.CopyPixels(pixelBytes1, stride, 0);
bmp2.CopyPixels(pixelBytes2, stride, 0);
for (int x = 0; x < width; x++)
{
int woff = x * height;
for (int y = 0; y < height; y++)
{(R*0.3 + G*0.59+ B*0.11)
int index = woff + y;
int gray1 = bmp1[index];
int gray2 = bmp2[index];
double sum = Math.Pow(gray1 - gray2, 2)
mseGray += sum;
}
}
mse = (mseGray) / ((bmp1.Width * bmp1.Height) * 3);
EDIT:
http://www.mathworks.com/matlabcentral/answers/49906-how-to-calculate-psnr-of-compressed-images-and-how-to-compare-psnr-of-images-compressed-by-two-diff
I'm having an issue with your implementation of PSNR though im thinking its not per definition
here is an example from java (very similar to C#)
http://www.cyut.edu.tw/~yltang/program/Psnr.java
This isn't really a C# question, BUT I am doing this in C# and hopefully some individuals here can educate me on functions within the Math class, or help me find a good algorithm for this anyhow.
We'll use this example. I'm at coordinate 10, 10. I want to find all coordinates that are within a 7x7 grid, using 10, 10 as the center of it. Can anyone suggest a good algorithm, or use of the Math class to find all the coordinates I need?
This code make grid of '1' in the arr[15,15] with center at 10,10.
int x = 10, y = 10;
int lConerX = x - 4, lConerY = y - 4;//coords of top-left conner
for (int i = lConerX; i < lConerX + 7; i++)
{
for (int j = lConerY; j < lConerY + 7; j++)
{
arr[i, j] = 1;
}
}
It seems you just need double cycle by X and Y coordinates
pseudocode
X0=10
Y0=10
ASize = 7
HalfSize = ASize / 2
for Y = Y0 - HalfSize to Y0 + ASize - HalfSize do
for X = X0 - HalfSize to X0 + ASize - HalfSize do
output Y,X coordinates
I'm working on civilization game in C# and XNA. I use a two dimensional integer array, populated with a loop, to generate tiles, I've done a ton research and have been unable to find a way to generate earth like terrain. Can anyone explain how to do this or at least give me code that could do it, though I would prefer and explanation? Thank you.
I use an algorithm similar to this to make my terrain. Basicly it generates some random numbers and uses a sine wave to generate hills, when combined they give a nice hilly landscape. Note that you can add a loop and array of values if you want more than just 3 passes.
private void GenerateTerrain()
{
terrainContour = new int[Width*Height];
//Make Random Numbers
double rand1 = randomizer.NextDouble() + 1;
double rand2 = randomizer.NextDouble() + 2;
double rand3 = randomizer.NextDouble() + 3;
//Variables, Play with these for unique results!
float peakheight = 20
float flatness = 50
int offset = 30;
//Generate basic terrain sine
for (int x = 0; x < Width; x++)
{
double height = peakheight / rand1 * Math.Sin((float)x / flatness * rand1 + rand1);
height += peakheight / rand2 * Math.Sin((float)x / flatness * rand2 + rand2);
height += peakheight / rand3 * Math.Sin((float)x / flatness * rand3 + rand3);
height += offset;
terrainContour[x] = (int)height;
}
}
Then to fill the heightmap just loop through the values and check if it is above the threshold or not.
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
if (y > terrainContour[x])
tiles[x, y] = Solid Tile
else
tiles[x, y] = Blank Tile
}
}
Theres much more you can add to it, I've added more randomness and indenting some tiles by 1 up or down for better terrain. And adding more sine waves will make it more realistic.
Try using 2D Perlin Noise algorithms, and selecting certain heights to make caves and more advanced terrain, as this is now what I do, but this code here is a good start.