Algorithm to Calculate all possible end points with Tile-based movement - c#

I've been trying to develop an algorithm to show which tiles you can move to on a square board based on these parameters:
-You move your characters around the board each turn to any tile within their movement speed.
-Each horizontal/vertical movement is worth 1 tile.
-Each diagonal is worth 1.5 (rounded down- so the first diagonal is worth 1, and the second is worth 2, and back to 1, etc.).
-You cannot move a character onto a tile that has another character on it, so you must go around.
NOTE: I don't currently check to see if a tile is occupied, I want to take this one step at a time, and the first step is getting a correct result of which tiles the character can go to.
I have a 3d array the size of the board. The third dimension has two layers, the first one is initialized as all 99 except the character you are moving (the origin), which is set to 0. This dimension contains the distance to the origin from each tile.
The other layer contains the number of diagonals it took to get to that tile.
Basically I have a recursive function that checks each adjacent tile for the lowest distance to the origin, and sets the current tile to that lowest distance number +1 (or +2 if it is the second diagonal). It moves out from the origin recursively, using the tiles it already filled in to generate a map of all of the potential tiles that it can move to.
`
const int edgeOfBoard = 15;
static int[,,] movementCount = new int[edgeOfBoard, edgeOfBoard, 2]; //movement speed/diagonals tracking matrix
static void Main()
{
int movementSpeed = 4; //number of tiles character can move
int x = 7; //x starting position
int y = 7; //y starting position
for(int i = 0; i < edgeOfBoard; i++) //fill movementCount with 99
{
for(int j = 0; j < edgeOfBoard; j++)
{
movementCount[i, j, 0] = 99;
}
}
movementCount[x, y, 0] = 0; //set origin (character's location) as 0 movements from itself
pathfinder(movementSpeed, x, y, 0); //run pathfinder algorithm
print(); //print result
}
private static void print() //print result
{
for(int y = 0; y < edgeOfBoard; y++) //print movement to get to a given tile
{
for(int x = 0; x < edgeOfBoard; x++)
{
if(movementCount[x, y, 0] == 99) //replace 99s with " " to make it easier to read
{
Console.Write("| ");
}else
{
Console.Write("|" + movementCount[x, y, 0]);
}
}
Console.WriteLine("|");
}
Console.WriteLine();
for(int y = 0; y < edgeOfBoard; y++) //print diagonals needed to get to a given tile
{
for(int x = 0; x < edgeOfBoard; x++)
{
if(movementCount[x, y, 1] == 0)
{
Console.Write("| ");
}else
{
Console.Write("|" + movementCount[x, y, 1]);
}
}
Console.WriteLine("|");
}
}
internal static void pathfinder(int movementSpeed, int x, int y, int depth)
{
if (depth <= movementSpeed) //cuts off when limit is reached
{
for (int Y = -1; Y <= 1; Y++) //checks all adjacent tiles
{
for (int X = -1; X <= 1; X++)
{
//Console.WriteLine("y = " + y + ", Y = " + Y + ", x = " + x + ", X = " + X + ", mvC[] = " + movementCount[x + X, y + Y, 0]);
//Checks if current adjacent tile subject is in bounds and is not the origin of the search
if (y + Y >= 0 && y + Y <= edgeOfBoard && x + X >= 0 && x + X <= edgeOfBoard && !(Y == 0 && X == 0) && (movementCount[x + X, y + Y, 0] == 99))
{
int[] lowestAdjacent = findLowestAdjacent(x + X, y + Y); //find the lowest adjacent tile
if (lowestAdjacent[0] + 1 <= movementSpeed) //if it is within the movement speed, add it to the matrix
{
movementCount[x + X, y + Y, 0] = lowestAdjacent[0] + 1; //update movement speed for subject tile
movementCount[x + X, y + Y, 1] = lowestAdjacent[1]; //update number of diagonals needed for subject tile
//print();
}
}
}
}
for (int Y = -1; Y <= 1; Y++) //mmove into already checked tiles to recursively check their adjacent tiles
{
for (int X = -1; X <= 1; X++)
{
if (y + Y >= 0 && y + Y <= 15 && x + X >= 0 && x + X <= 15 && !(Y == 0 && X == 0) && (movementCount[x + X, y + Y, 0] != 99) && (movementCount[x + X, y + Y, 0] < movementSpeed))
{
pathfinder(movementSpeed, x + X, y + Y, depth + 1);
}
}
}
}
}
private static int[] findLowestAdjacent(int x, int y) //finds lowest number of movements to get to subject tile (x, y)
{
int[] lowestRtrn = { 99, 0 }; //movement, diagonals
int lowest = 99;
for (int Y = -1; Y <= 1; Y++) //checks each adjacent tile
{
for (int X = -1; X <= 1; X++)
{
if (y + Y >= 0 && y + Y <= edgeOfBoard && x + X >= 0 && x + X <= edgeOfBoard) //ensures it is within bounds
{
int diag = isDiagonalMovement(x, y, x + X, y + Y) ? diagonalMovementIncrease(movementCount[x + X, y + Y, 1] + 1) : 0; //checks whether or not it should be diagonally increased
if ((movementCount[x + X, y + Y, 0] + diag) < lowest) //adds to result if lower than current
{
lowest = movementCount[x + X, y + Y, 0] + diag;
lowestRtrn[1] = movementCount[x + X, y + Y, 1] + (isDiagonalMovement(x, y, x + X, y + Y) ? 1 : 0);
}
}
}
}
lowestRtrn[0] = lowest;
return lowestRtrn;
}
private static int diagonalMovementIncrease(int diagonalMovements) //checks if diagonal is second diagonal (+2 instead of +1)
{
return diagonalMovements % 2 == 0 ? 1 : 0;
}
private static bool isDiagonalMovement(int x, int y, int X, int Y) //checks if (x, y) is diagonal from (X, Y)
{
if (
(x + 1 == X && y + 1 == Y) ||
(x - 1 == X && y + 1 == Y) ||
(x + 1 == X && y - 1 == Y) ||
(x - 1 == X && y - 1 == Y)
)
{
return true;
}
else
{
return false;
}
}`
Code Result
This is the result when printed with a movement speed of 4, starting at 7, 7 on a 15x15 grid (simply for testing purposes, edgeOfBoard = 15)
(99s replaced with " " on the top grid to make it easier to read)
The top grid is the first layer of the third dimension- the amount of tiles it takes to get to that tile.
The bottom grid is the number of diagonals it takes to get to that tile.
The upper left and bottom right quadrants work properly, but the upper right and bottom left do not, which is really stumping me. Can you help me either come up with a new algorithm or fix this one?

You can simplify the business of the diagonal steps being counted as 1.5 steps by storing the number of steps multiplied by 2. So a horizontal or vertical step becomes 2 and a diagonal step becomes 3, and the maximum distance x becomes 2*x+1. This means we don't have to store an additional grid with how many diagonals have been used to get to any tile.
Let's start with this grid, where a value of 99 indicates it is unvisited and empty:
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 0 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
We start with the initial position, the tile with coordinates (5,4), on a stack (which is much simpler than using recursion) or a queue (which will be more efficient than a stack in this case). We will then repeatedly take a tile from the queue, check which of its neighbours have a value that is greater than the current tile's value plus two (or three if they are diagonal neighbours), and if so, replace the neighbour's value and add the tile to the queue. After processing all the neighbours of the first tile, we'd have this situation:
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 3 2 3 99 99 99
99 99 99 99 2 0 2 99 99 99
99 99 99 99 3 2 3 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
and these tiles in the queue:
(5,3), (6,3), (6,4), (6,5), (5,5), (4,5), (4,4), (4,3)
When we take the tile (5,3) from the queue, it has value 2, so its neighbour (4,3) would get value 4; however, that tile already has a smaller value 3, so we keep that smaller value, and don't add the tile to the queue. After processing all neighbours of this tile we get:
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 5 4 5 99 99 99
99 99 99 99 3 2 3 99 99 99
99 99 99 99 2 0 2 99 99 99
99 99 99 99 3 2 3 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
If the maximum distance that can be reached is e.g. 9 (converted from 4 by doing 2*4+1), we keep taking tiles from the queue and processing their neighbours, until no more new tiles have a value of 9 or less, and the queue is empty. The end result is:
99 99 99 99 9 8 9 99 99 99
99 99 9 8 7 6 7 8 9 99
99 99 8 6 5 4 5 6 8 99
99 9 7 5 3 2 3 5 7 9
99 8 6 4 2 0 2 4 6 8
99 9 7 5 3 2 3 5 7 9
99 99 8 6 5 4 5 6 8 99
99 99 9 8 7 6 7 8 9 99
99 99 99 99 9 8 9 99 99 99
99 99 99 99 99 99 99 99 99 99
To translate a distance to a tile from the +2/+3 logic to the +1/+1.5 logic, integer-divide the value in the grid by 2, so that e.g. 9 becomes 4.
You can use the same 2-dimensional grid to also take into account obstacles. You could e.g. mark the empty tiles by 99 and the obstacles by -1. An initial grid:
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 -1 99 99 -1 99 99
99 99 99 99 99 0 99 -1 99 99
99 99 99 99 99 99 99 -1 99 99
99 99 99 99 99 -1 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99 99 99
would then lead to:
99 99 99 99 9 8 9 99 99 99
99 99 99 8 7 6 7 8 99 99
99 99 8 7 5 4 5 7 9 99
99 9 7 5 -1 2 3 -1 99 99
99 8 6 4 2 0 2 -1 99 99
99 9 7 5 3 2 3 -1 99 99
99 99 8 6 5 -1 5 7 9 99
99 99 9 8 7 8 7 8 99 99
99 99 99 99 9 99 9 99 99 99
99 99 99 99 99 99 99 99 99 99
The details of the main part of the code would then be:
Take a tile from the queue.
Iterate over its neighbours: (-1,-1) (0,-1) (1,-1) (1,0) (1,1) (0,1) (-1,1) (-1,0) and for each (dx,dy) check:
whether coordinates (x+dx, y+dy) are within the grid,
and whether tile (x+dx, y+dy) is not an obstacle,
and whether tile (x+dx, y+dy) is empty or has a value larger than the current tile plus 2 or 3.
If yes to all three conditions, replace the value of (x+dx,y+dy) by the value of the current tile plus 2 or 3.
If the new value of tile (x+dx, y+dy) is less than the maximum distance minus 1, add the tile (x+dx, y+dy) to the queue.

Related

Get the pre-last element of a string split by spaces

Input examples:
7 9 12 16 18 21 25 27 30 34 36 39 43 45 48 52 54 57 61
7 9 12 16 18 21 25 27 30 34 36 39 43 45 48 52 54 57 ... 75 79
Note that it ends with a space.
I want to get 57 in the first case and 75 in the second case as integer. I tried with the following:
Convert.ToInt32(Shorten(sequence).Split(' ').ElementAt(sequence.Length - 2));
The problem is that sequence.Length is not really the right index.
You can use the overload for Split() and pass the RemoveEmptyEntires enum:
string input = "7 9 12 16 18 21 25 27 30 34 36 39 43 45 48 52 54 57 61 ";
var splitInput = input.Split(new char[0], StringSplitOptions.RemoveEmptyEntries);
var inputInt = Convert.ToInt32(splitInput[splitInput.Length - 2]);
// inputInt is 57
Doing it this way allows your last element to actually be what you want.
Fiddle here
Based on maccettura's answer, in C# 8 you can simplify index acces like so
var input = "1 2 3";
var parts = input.Split(' ', StringSplitOptions.RemoveEmptyEntries);
var value = parts.Length >= 2 ? Convert.ToInt32(parts[^2]) : null;
How about something that doesn't use strings at all.
public static int? SplitReverseInt(this string str, int ixFromBack)
{
var inWord = false;
var wEnd = 0;
var found = 0;
for (int i = str.Length - 1; i >= 0; i--)
{
var charr = str[i];
if (char.IsWhiteSpace(charr))
{
// we found the beginning of a word
if (inWord)
{
if (found == ixFromBack)
{
var myInt = 0;
for (int j = i+1; j <= wEnd; j++)
myInt = (myInt * 10 + str[j] - '0');
return myInt;
}
inWord = false;
found++;
}
}
else
{
if (!inWord)
{
wEnd = i;
inWord = true;
}
}
}
// word (number) is at the beginning of the string
if (inWord && found == ixFromBack)
{
var myInt = 0;
for (int j = 0; j <= wEnd; j++)
myInt = (myInt * 10 + str[j] - '0');
return myInt;
}
return null;
}
Performance is about 10 times faster on the example strings.
This only loops from the back, and only fetches one number, it doesnt create substrings or an array of strings we don't need.
Use like this:
var numberFromBack = SplitReverseInt(input, 1);

Calculate sum of numbers on matrix diagonal

I have a dynamic matrix and I need to to calculate sum of digits in this way:
0 1 2 3 4 5 6
10 11 12 13 14 15 16
20 21 22 23 24 25 26
30 31 32 33 34 35 36
40 41 42 43 44 45 46
50 51 52 53 54 55 56
60 61 62 63 64 65 66
I can't understand in which way I should compare i and j:
long result = 0;
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
// only works for diagonal
if (i == j) // should use j - 1 or i - 1?
{
result += matrix[i][j];
}
}
}
no need to scan full matrix:
long result = 0;
for (int i = 0; i < len; i++)
{
result += matrix[i][i]; // diagonal
if (i < len - 1) // stay within array bounds
result += matrix[i][i+1]; // next to diagonal
}
modification without index check on every iteration:
// assign corner value from bottom row to result
long result = matrix[len-1][len-1];
// for each row (except last!) add diagonal and next to diagonal values
for (int i = 0; i < len-1; i++)
result += matrix[i][i] + matrix[i][i+1];

Converting single dimension array to a 2D array in C# for AES data matrix

I'm trying to make a 2D array from a single dimension array to make a data state in Rijndael or AES cryptographical process. I've been trying using this code here;
public byte[,] MakeAState(byte[] block){
byte[,] State;
foreach (byte i in block)
for (int row = 0; row < 4; row++)
for (int column = 0; column < 4; column++)
State[column, row] = i;
return State;
}
and I intend to make the result to be like this
//Original Sequence
[99 111 98 97 112 97 115 115 99 111 98 97 112 97 115 115]
//Desired Sequence
[99 112 99 112]
[111 97 111 97]
[98 115 98 115]
[97 115 97 115]
The results always comes out as if the elements of Block used as if the index of the State array, causing an 'out-of boundary' error message appearing. any idea on how to manage this?
This should be what you want, and it's working with division and modulo to determine column and row(just switch "i % 4" with "i / 4" if you want to turn the matrix):
class Program
{
static void Main(string[] args)
{
byte[] original = new byte[] { 99, 111, 98, 97, 112, 97, 115, 115, 99, 111, 98, 97, 112, 97, 115, 115 };
byte[,] result = MakeAState(original);
for (int row = 0; row < 4; row++)
{
for (int column = 0; column < 4; column++)
{
Console.Write(result[row,column] + " ");
}
Console.WriteLine();
}
}
public static byte[,] MakeAState(byte[] block)
{
if (block.Length < 16)
{
return null;
}
byte[,] state = new byte[4,4];
for (int i = 0; i < 16; i++)
{
state[i % 4, i / 4] = block[i];
}
return state;
}
}
}
Output:
99 112 99 112
111 97 111 97
98 115 98 115
97 115 97 115
You likely flipped row and column on State[column, row] = i; which might be what's causing your out of bounds exception. Can't tell without more information about your variables, though.
But that's not the only issue here. Assuming you just want the array to be split into groups of four. This is your current situation if you flip row/columnand get past your exception.
//Original sequence:
[0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]
//Desired sequence:
[0 4 8 12]
[1 5 9 13]
[3 6 10 14]
[4 7 11 15]
//What you are currently getting:
[15 15 15 15]
[15 15 15 15]
[15 15 15 15] //<-- Last value of original sequence, everywhere.
What's happening in your code is every position in Block is placed in every position in the new array, which means that you'll end up with an array filled with the last value of Block when the algorithm is finished.
Changing it to something like this would return the result you want.
public static byte[,] State(byte[] Block)
{
if (Block.Length % 16 != 0)
throw new Exception("Byte array length must be divisible by 16.");
var rowCount = Block.Length / 4;
var State = new byte[rowCount, 4];
for (int column = 0, block = 0; column < 4; column++)
for (int row = 0; row < rowCount; row++, block++)
State[row, column] = Block[block];
return State;
}

Rerun a program using nested for loops

I just started learning how to program in c #, so don't shoot me if I have "dumb" questions or questions to which the answer is probably very logical.
I have the next assignment:
Use a for loop to write the next pattern:
1 2 3 4 5 6 7 8 9 10 10
11 12 13 14 15 16 17 18 19 20 20
21 22 23 24 25 26 27 28 29 30 30
31 32 33 34 35 36 37 38 39 40 40
41 42 43 44 45 46 47 48 49 50 50
51 52 53 54 55 56 57 58 59 60 60
61 62 63 64 65 66 67 68 69 70 70
71 72 73 74 75 76 77 78 79 80 80
81 82 83 84 85 86 87 88 89 90 90
91 92 93 94 95 96 97
It has to be possible for the user to set a maximum (in this example 97) and then rerun the application without restarting.
What I am trying now is this:
class Program
{
static void Main(string[] args)
{
string output = "", input = "";
int MaxWaarde, karakters = 15;
do
{
Console.WriteLine("Gelieve het maximum van de matrix in te geven");
input = Console.ReadLine();
MaxWaarde = Convert.ToInt32(input);
for (int i = 1; i <= MaxWaarde; i += 11)
{
Console.Write(i);
for (int j = i; j < MaxWaarde + 1; j += karakters)
{
Console.Write(j);
}
Console.WriteLine(karakters + "");
}
Console.Write("\nOpnieuw een matrix aanmaken? (y/n): ");
output = Console.ReadLine();
}
while (output.ToLower() == "y");
}
}
Which isn't correct at all, but I have been trying to fix this for a while now, and I think I have been staring myself blind at this one, so i really don't know which way to go with this anymore.
Somebody who can give me some advice on how to make this right?
Nested for loops doesn't seem like a good idea here.You can do it with one for loop.You are incrementing i +11 in every step,probably your mistake is here. Consider this:
for (int i = 1; i <= MaxWaarde; i++)
{
if(i % 10 != 0) Console.Write(i + " ");
else
{
Console.Write(i + " " + i + "\n");
}
}
% is modulus operator.We are looking for remainder of currentNumber / 10, if it's not zero we write the number.If it is then we write value twice and adding a newline character with \n to go the next line.Also You can use Console.WriteLine() instead of \n
Loop1: Count up from 1 to number by step 1. If number is a multiple of 10 it writes that number again with line feed.
Loop2: Do loop1 again as long user wishes.
Here you go
public static void Main(string[] args)
{
for (;;)
{
var val = int.Parse(Console.ReadLine());
for (int i = 1; i <= val; i++)
{
if (i % 10 == 0)
{
Console.WriteLine("{0} {0}", i);
}
else
{
Console.Write("{0} ", i);
}
}
Console.WriteLine();
}
}

How to 'normalize' a grayscale image?

My math is a bit rusty. I'm trying to equalize a histogram of a 2D array which represents grayscale values in the range 0-255 (values may not be whole numbers because of how they are computed).
I found this article on Wikipedia, but I don't quite understand the formulas they present.
ni, n and L I can compute, but I'm not quite sure how to implement this cdf function. Might this function be of use?
Here's what I've got so far:
static double[,] Normalize(double[,] mat)
{
int width = mat.GetLength(0);
int height = mat.GetLength(1);
int nPixels = width*height;
double sum = 0;
double max = double.MinValue;
double min = double.MaxValue;
var grayLevels = new Dictionary<double, int>();
foreach (var g in mat)
{
sum += g;
if (g > max) max = g;
if (g < min) min = g;
if (!grayLevels.ContainsKey(g)) grayLevels[g] = 0;
++grayLevels[g];
}
double avg = sum/nPixels;
double range = max - min;
var I = new double[width,height];
// how to normalize?
return I;
}
Found something that you might find useful
http://sonabstudios.blogspot.in/2011/01/histogram-equalization-algorithm.html
Hope that helps
Calculating the cumulative distribution function involves a couple of steps.
First you get the frequency distribution of your grayscale values.
So something like:
freqDist = new int[256];
for each (var g in mat)
{
int grayscaleInt = (int)g;
freqDist[grayscaleInt]++;
}
Then to get your CDF, something like:
cdf = new int[256];
int total = 0;
for (int i = 0; i < 256; i++)
{
total += freqDist[i];
cdf[i] = total;
}
I can help you to understand your link,
first, counting value which represent image, shows in that link,
Value Count Value Count Value Count Value Count Value Count
52 1 64 2 72 1 85 2 113 1
55 3 65 3 73 2 87 1 122 1
58 2 66 2 75 1 88 1 126 1
59 3 67 1 76 1 90 1 144 1
60 1 68 5 77 1 94 1 154 1
61 4 69 3 78 1 104 2
62 1 70 4 79 2 106 1
63 2 71 2 83 1 109 1
it is means, the image is created with values above, nothing else.
second, sums the value cumulatively from 52 to 154
Value cdf Value cdf Value cdf Value cdf Value cdf
52 1 64 19 72 40 85 51 113 60
55 4 65 22 73 42 87 52 122 61
58 6 66 24 75 43 88 53 126 62
59 9 67 25 76 44 90 54 144 63
60 10 68 30 77 45 94 55 154 64
61 14 69 33 78 46 104 57
62 15 70 37 79 48 106 58
63 17 71 39 83 49 109 59
it is means,
value 52 have 1 cdf cause it is initial value,
value 55 have 4 cdf cause it has 3 count in image plus 1 cdf from 52,
value 58 have 6 cdf cause it has 2 count in image plus 4 cdf from 55,
and so on.. till..
value 154 have 64 cdf cause it has 1 count in image plus 63 cdf from 144.
then, calculating histogram equalization formula for each image values based on the function
cdf(v) is represent current cdf value from current image value,
in this case, if h(v) = 61 so cdf(v) = 14
cdfmin is represent initial cdf value, in this case, 1 cdf from value 52
happy coding.. ^^
Here's my implementation:
private static byte[,] Normalize(byte[,] mat)
{
int width = mat.GetLength(0);
int height = mat.GetLength(1);
int nPixels = width*height;
var freqDist = new int[256];
foreach (var g in mat)
{
++freqDist[g];
}
var cdf = new int[256];
int total = 0;
for (int i = 0; i < 256; ++i)
{
total += freqDist[i];
cdf[i] = total;
}
int cdfmin = 0;
for (int i = 0; i < 256; ++i)
{
if (cdf[i] > 0)
{
cdfmin = cdf[i];
break;
}
}
var I = new byte[width,height];
double div = (nPixels - cdfmin) / 255d;
for (int y = 0; y < height; ++y)
{
for (int x = 0; x < width; ++x)
{
I[x, y] = (byte)Math.Round((cdf[mat[x, y]] - cdfmin) / div);
}
}
return I;
}
I changed it from using doubles to bytes to work better with the histogram (freqDist).
In addition to what John says, you will need to use the cdf array to compute the new value for every pixel. You do this by:
Adjust John's second iteration to get the first i that has a
freqDist > 0 and call that i imin
Going pixel by pixel i,j between 0 and width and 0 and height repectively and
evaluating round((cdf[pixel[i,j]]-cdf[imin])/(width*height-cdf[imin]))*255),
that is the normalized pixel value at that location.
You can use this function I just wrote:
public static Bitmap ContrastStretch(Bitmap srcImage, double blackPointPercent = 0.02, double whitePointPercent = 0.01)
{
BitmapData srcData = srcImage.LockBits(new Rectangle(0, 0, srcImage.Width, srcImage.Height), ImageLockMode.ReadOnly,
PixelFormat.Format32bppArgb);
Bitmap destImage = new Bitmap(srcImage.Width, srcImage.Height);
BitmapData destData = destImage.LockBits(new Rectangle(0, 0, destImage.Width, destImage.Height),
ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
int stride = srcData.Stride;
IntPtr srcScan0 = srcData.Scan0;
IntPtr destScan0 = destData.Scan0;
var freq = new int[256];
unsafe
{
byte* src = (byte*) srcScan0;
for (int y = 0; y < srcImage.Height; ++y)
{
for (int x = 0; x < srcImage.Width; ++x)
{
++freq[src[y*stride + x*4]];
}
}
int numPixels = srcImage.Width*srcImage.Height;
int minI = 0;
var blackPixels = numPixels*blackPointPercent;
int accum = 0;
while (minI < 255)
{
accum += freq[minI];
if (accum > blackPixels) break;
++minI;
}
int maxI = 255;
var whitePixels = numPixels*whitePointPercent;
accum = 0;
while (maxI > 0)
{
accum += freq[maxI];
if (accum > whitePixels) break;
--maxI;
}
double spread = 255d/(maxI - minI);
byte* dst = (byte*) destScan0;
for (int y = 0; y < srcImage.Height; ++y)
{
for (int x = 0; x < srcImage.Width; ++x)
{
int i = y*stride + x*4;
byte val = (byte) Clamp(Math.Round((src[i] - minI)*spread), 0, 255);
dst[i] = val;
dst[i + 1] = val;
dst[i + 2] = val;
dst[i + 3] = 255;
}
}
}
srcImage.UnlockBits(srcData);
destImage.UnlockBits(destData);
return destImage;
}
static double Clamp(double val, double min, double max)
{
return Math.Min(Math.Max(val, min), max);
}
The defaults mean that the darkest 2% of pixels will become black, the lightest 1% will become white, and everything in between will be stretched to fill the color space. This is the same as the default for ImageMagick.
This algorithm has the fun side effect that if you use values above 50% then it will invert the image! Set to .5, .5 to get a black & white image (2 shades) or 1, 1 to get a perfect inversion.
Assumes your image is already grayscale.

Categories

Resources