Last active
November 16, 2020 11:48
-
-
Save zeronyk/c1323358e4e0ed47fcc3a27e0ef1bf54 to your computer and use it in GitHub Desktop.
This function will create the clockwise indexing for Moor Neighbourhood.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clockwise gernation is distance max -> ascending -> distance min -> descendig | |
// if you want to understand this method, write down x and y coordinates (on top of each other, y = shifted) of a clockwise Moor Neighbour rotation | |
/* | |
* if you want to search in a Moor neighbourhood (surrounding 8 pixels of a pixel) you might need to get a clockwise rotation. | |
* I was searching for a generalized method to generate clockwise rotated indexing for | |
*range 1 (x = [0,1,1,1,0,-1-1-1], y =[1,1,0,-1,-1,-1,0,1]) | |
*range 2 (x = { 2, -1, 0, 1, -2, -2, -2, -2, -2, 1, 0, -1, 2, 2, 2, 2 }, y = { 2, 2, 2, 2, 2, -1, 0, 1, -2, -2, -2, -2, -2, 1, 0, -1 }) | |
*range 3 (x = { 3, 3, 3, 3, 3, 3, 3, -2, -1, 0, 1, 2, -3, -3, -3, -3, -3, -3, -3, 2, 1, 0, -1, -2 }, y = { 3, -2, -1, 0, 1, 2, -3, -3, -3, -3, -3, -3, -3, 2, 1, 0, -1, -2, 3, 3, 3, 3, 3, 3 }) | |
* HOW to use, | |
* the clockwise rotated sequence of x will be returned if shifted = false | |
* if shifted = true the corresponding y sequence will be returned | |
* distance is the range of the moor neighbourhood (1 = 8 pixel) (2 = 16) (3 = 24) ... | |
* | |
*/ | |
public static int[] generateClockwise(int distance, bool shifted) | |
{ | |
int range = distance * 2 + 1; | |
int[] between_asc = new int[range -2]; | |
int[] between_dsc = new int[range -2]; | |
int[] max = new int[range]; | |
int[] min = new int[range]; | |
for (int i = 0; i < range; i++) | |
{ | |
max[i] = distance; | |
min[i] = -distance; | |
} | |
for (int i = 0; i < range - 2; i++) | |
{ | |
between_asc[i] = -distance + 1 + i; | |
between_dsc[i] = distance - 1 - i; | |
} | |
int shift_range = 0; | |
int[] output = new int[range * 2 + (range - 2) * 2]; | |
max.CopyTo(output, 0); | |
between_asc.CopyTo(output, max.Length); | |
min.CopyTo(output,max.Length+ between_asc.Length); | |
between_dsc.CopyTo(output, max.Length + between_asc.Length + min.Length); | |
if (shifted) | |
{ | |
shift_range = range - 1; | |
int[] output_tmp = (int[]) output.Clone(); | |
for (int i = 0; i < output.Length; i++) | |
{ | |
output[(i + output.Length- shift_range) % output.Length] = output_tmp[i]; | |
} | |
} | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment