/* Desired Functions: * Connect Components * Extract Hand * Get Shape Descriptors * Get Covariance Matrix (in Math.Net Possibly?) * reshape * getTfeatures */ /// <summary> /// Returns a 2d int array with number of connect components and indices of connected components /// </summary> /// <param name="array"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static ArrayList BWConnComp(DenseMatrix array, int width, int height) { ArrayList nodes = new ArrayList(); ArrayList roots = new ArrayList(); ArrayList returnArray = new ArrayList(); //Make each pixel a separate node for (int i = 0; i < array.RowCount; i++) { for (int j = 0; j < array.ColumnCount; j++) { ConnNode pixNode = new ConnNode(i * array.ColumnCount + j); if (array[i, j] == 1) { pixNode.setWhite(); } nodes.Add(pixNode); } } //iterate through the nodes foreach (ConnNode node in nodes) { //check if node's white if (node.white == 1) { //first check if root if (node.parent == null) { roots.Add(node); } //now check up,down,left,right // //up //first check if in top row if (!(node.id < width)) { ConnNode newNode = (ConnNode)nodes[node.id - width]; //check if parent or inspected already if (!(newNode.inspected)) { //check if white if (newNode.white == 1) { node.AddChild(newNode); newNode.inspected = true; } } } //down //first check if in bottom row if (!(node.id >= (width * (height - 1)))) { ConnNode newNode = (ConnNode)nodes[node.id + width]; //check if parent or inspected already if (!(newNode.inspected)) { //check if white if (newNode.white == 1) { node.AddChild(newNode); newNode.inspected = true; } } } //left //first check if in left column if (node.id % width != 0) { ConnNode newNode = (ConnNode)nodes[node.id - 1]; //check if parent or inspected already if (!(newNode.inspected)) { //check if white if (newNode.white == 1) { node.AddChild(newNode); newNode.inspected = true; } } } //right //first check if in right column if ((node.id + 1) % width != 0) { ConnNode newNode = (ConnNode)nodes[node.id + 1]; //check if parent or inspected already if (!(newNode.inspected)) { //check if white if (newNode.white == 1) { node.AddChild(newNode); newNode.inspected = true; } } } } node.inspected = true; } //Go through each root and find size and indices foreach (ConnNode newnode in roots) { //make new BWConnReturn BWConnReturn values = new BWConnReturn(); //Get size of tree values.size = newnode.GetSize(); //now get indices values.indices = newnode.GetIndices(); //now add returntype to array returnArray.Add(values); } return(returnArray); } //checked
/* Desired Functions: * Connect Components * Extract Hand * Get Shape Descriptors * Get Covariance Matrix (in Math.Net Possibly?) * reshape * getTfeatures */ /// <summary> /// Returns a 2d int array with number of connect components and indices of connected components /// </summary> /// <param name="array"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static List <BWConnReturn> BWConnComp(DenseMatrix array, int width, int height) { List <ConnNode> nodes = new List <ConnNode>(); List <ConnNode> roots = new List <ConnNode>(); string outstring = ""; for (int i = 0; i < array.ColumnCount; i++) { for (int j = 0; j < array.RowCount; j++) { outstring += array[j, i].ToString() + ","; } } List <BWConnReturn> returnArray = new List <BWConnReturn>(); //Make each pixel a separate node for (int i = 0; i < array.ColumnCount; i++) { for (int j = 0; j < array.RowCount; j++) { ConnNode pixNode = new ConnNode(i * array.RowCount + j); if (array[j, i] == 1) { pixNode.setWhite(); } nodes.Add(pixNode); } } //iterate through the nodes foreach (ConnNode node in nodes) { //check if node's white if ((node.white == 1)) { //first check if root if (node.parent == null) { roots.Add(node); } //now check up,down,left,right // //up //first check if in top row if (!(node.id % height == 0)) { ConnNode newNode = (ConnNode)nodes[node.id - 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //down //first check if in bottom row if (!((node.id + 1) % height == 0)) { ConnNode newNode = (ConnNode)nodes[node.id + 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //left //first check if in left column if (node.id >= height) { ConnNode newNode = (ConnNode)nodes[node.id - height]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //right //first check if in right column if (node.id < (height * (width - 1))) { ConnNode newNode = (ConnNode)nodes[node.id + height]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //NW if ((node.id >= height) && (!(node.id % height == 0))) { ConnNode newNode = (ConnNode)nodes[node.id - height - 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //NE if ((node.id < (height * (width - 1))) && (!(node.id % height == 0))) { ConnNode newNode = (ConnNode)nodes[node.id + height - 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //SE if ((node.id < (height * (width - 1))) && (!((node.id + 1) % height == 0))) { ConnNode newNode = (ConnNode)nodes[node.id + height + 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } //SW if ((node.id >= height) && (!((node.id + 1) % height == 0))) { ConnNode newNode = (ConnNode)nodes[node.id - height + 1]; //check if white if (newNode.white == 1) { //inspected already if (!(newNode.inspected)) { node.AddChild(newNode); newNode.inspected = true; } else { //compare roots. if Different roots, combine trees ConnNode root1 = node; ConnNode root2 = newNode; while (root1.parent != null) { root1 = root1.parent; } while (root2.parent != null) { root2 = root2.parent; } if (root1.id != root2.id) { root1.AddChild(root2); roots.Remove(root2); } } } } } node.inspected = true; } //Go through each root and find size and indices foreach (ConnNode newnode in roots) { //make new BWConnReturn BWConnReturn values = new BWConnReturn(); //Get size of tree values.size = newnode.GetSize(); //now get indices values.indices = newnode.GetIndices(); //now add returntype to array returnArray.Add(values); } return(returnArray); } //checked