private void VisualizeWeightCenter(WeightCenter w, int rows, int cols, byte[, ,] destination) { int wx, wy; // Visualize weight centres if ((w.x > 0) && (w.y > 0)) { wx = w.x; wy = w.y; //Draw white pixels on X and Y axis for (int i = -20; i < 20; i++) { //Checks if a white cross is drawn outside the picture if (((wx + i) > (cols - 1)) || ((wy + i) > (rows - 1))) { break; } if (((wx + i) <= 0) || ((wy + i) <= 0)) { break; } //pixels are indexed as row -> col, therefore wy and wx are in this order, row = wy, col = wx destination[wy + i, wx, 2] = destination[wy + i, wx, 1] = destination[wy + i, wx, 0] = 255; destination[wy, wx + i, 2] = destination[wy, wx + i, 1] = destination[wy, wx + i, 0] = 255; } } }
} // End FindSpots private WeightCenter CalculateWeightCenter(byte[, ,] source, int x, int y, int width, int height, FilterColors color) { int sum_x = 0, sum_y = 0, n = 0; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (source[i + y, j + x, (byte)color] == 255) { sum_x += j; sum_y += i; n++; } } } WeightCenter w; if (n > 0) { w = new WeightCenter( x + (int)sum_x / n, y + (int)sum_y / n, y); } else { w = WeightCenter.Empty; } return(w); }
private void FindBinaryCode(byte[, ,] original, Point3D[] sourceBinPars, Point3D[] destBinPars) { List <Point3D> binarPlacesHomography = Geometry.homography_calc( new List <Point3D>(dataMouse.binarPlacesBase), sourceBinPars, destBinPars); WeightCenter binFound = FindBinaryPlace(binarPlacesHomography, original); List <Point3D> binar8Codes = new List <Point3D>(Geometry.CloneList(dataMouse.binarSpotsSide01)); // Rotate the 8 binary codes according the founded position Geometry.p_rotZ(binar8Codes, dataMouse.SinCosDescriptor[binFound.rowStart], 1); binar8Codes = Geometry.homography_calc(binar8Codes, sourceBinPars, destBinPars); if (checkBoxVizDetectedSpots.Checked) { DrawPoints(original, binar8Codes, FrameRows, FrameCols); } }
public static Color GetColor(byte[, ,] imgSource, WeightCenter p, int borderPixels) { int RSum = 0, GSum = 0, BSum = 0; int pixelCounter = 0; for (int i = p.y - borderPixels; i <= p.y + borderPixels; i++) { for (int j = p.x - borderPixels; j <= p.x + borderPixels; j++) { RSum += imgSource[i, j, 2]; GSum += imgSource[i, j, 1]; BSum += imgSource[i, j, 0]; pixelCounter++; } } byte resultR = (byte)(RSum / pixelCounter); byte resultG = (byte)(GSum / pixelCounter); byte resultB = (byte)(BSum / pixelCounter); return(Color.FromArgb(resultR, resultG, resultB)); }
// Returns the correct order of weight centers if exactly five spots are detected private List <Point3D> DetectFace(byte[, ,] data, byte[, ,] original, List <WeightCenter> weights, int rows, int cols, FilterColors color) { // Five points from the sides descriptor - destination for homography Point3D[] destP = new Point3D[5]; // Side 6 //destP[0] = new Point3D(-11.8513, -16.3120, -31.4164); //destP[1] = new Point3D(-19.1759, 6.2306, -31.4164); //destP[2] = new Point3D(0.0000, 20.1627, -31.4164); //destP[3] = new Point3D(19.1759, 6.2306, -31.4164); //destP[4] = new Point3D(11.8514, -16.3120, -31.4164); // Side 1 destP[4] = new Point3D(11.8514, 16.3119, 31.4164); destP[3] = new Point3D(19.1759, -6.2307, 31.4164); destP[2] = new Point3D(0.0000, -20.1627, 31.4164); destP[1] = new Point3D(-19.1759, -6.2306, 31.4164); destP[0] = new Point3D(-11.8513, 16.3120, 31.4164); int[,] indexWeightsDetect = { { 0, 1, 2, 3, 4 }, { 0, 1, 2, 4, 3 }, { 0, 1, 3, 2, 4 }, { 0, 1, 3, 4, 2 }, { 0, 2, 1, 3, 4 }, { 0, 2, 1, 4, 3 }, { 0, 2, 3, 1, 4 }, { 0, 2, 3, 4, 1 }, { 0, 3, 1, 2, 4 }, { 0, 3, 1, 4, 2 }, { 0, 3, 2, 1, 4 }, { 0, 3, 2, 4, 1 } }; int[] indexCamera = { 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3 }; int[] indexDescrPars = { 3, 4, 3, 4, 3, 4, 3, 4, 3, 4, 3, 4 }; //Point3D[] destPars_fourthP = Geometry.homography_calc_pars( // new Point3D[] { destP[0], destP[1], destP[2], destP[4] }); //Point3D[] destPars_fifthP = Geometry.homography_calc_pars( // new Point3D[] { destP[0], destP[1], destP[2], destP[3] }); //Point3D[][] destPars = new Point3D[][] { destPars_fourthP, destPars_fifthP }; double distance; double distanceMin = 10000; int jMin = 0; int[] weightIndexes = new int[5]; List <Point3D> homographyRes;//, weightListCurrent; List <Point3D> homographyMin = null, weightListMin = null; Point3D[] hmMinSourcePars = null, hmMinDestPars = null; List <Point3D> weightsList = new List <Point3D>(15); foreach (WeightCenter item in weights) { weightsList.Add(new Point3D(item.x, item.y, 0)); } //if ((weights[0].rowStart == weights[1].rowStart) && // (weights[1].rowStart != weights[2].rowStart) || // (weights[3].rowStart != weights[4].rowStart)) //{ // weightsList.Reverse(); //} //ConvertXY(weightsList, rows); for (int i = 0; i < 12; i++) { Point3D[] sourcePars = Geometry.homography_calc_pars(new Point3D[] { weightsList[indexWeightsDetect[i, 0]], weightsList[indexWeightsDetect[i, 1]], weightsList[indexWeightsDetect[i, 2]], weightsList[indexDescrPars[i]] }); Point3D[] destPars = Geometry.homography_calc_pars( new Point3D[] { destP[0], destP[1], destP[2], destP[indexDescrPars[i]] }); List <Point3D> currList = new List <Point3D>(1); currList.Add(weightsList[indexCamera[i]]); homographyRes = Geometry.homography_calc(currList, sourcePars, destPars); distance = Geometry.Distance(homographyRes[0], destP[indexCamera[i]]); if (distance < distanceMin) { distanceMin = distance; homographyMin = homographyRes; hmMinSourcePars = sourcePars; hmMinDestPars = destPars; weightIndexes[0] = indexWeightsDetect[i, 0]; weightIndexes[1] = indexWeightsDetect[i, 1]; weightIndexes[2] = indexWeightsDetect[i, 2]; weightIndexes[3] = indexWeightsDetect[i, 3]; weightIndexes[4] = indexWeightsDetect[i, 4]; //// For correct order of points //if (j == 0) // This should be fixed in future //{ // Point3D pTemp = homographyMin[3]; // homographyMin[3] = homographyMin[4]; // homographyMin[4] = pTemp; // pTemp = weightListMin[3]; // weightListMin[3] = weightListMin[4]; // weightListMin[4] = pTemp; //} } } MessageBox.Show("DistMin = " + distanceMin.ToString()); if (distanceMin < 1) { List <Point3D> binarPlaces = new List <Point3D>(5); List <Point3D> weightsReordered = new List <Point3D>(5); for (int i = 0; i < 5; i++) { weightsReordered.Add(weightsList[weightIndexes[i]]); } // If true is returned, recalc parameters if (CheckOrder(weightsReordered, rows)) { // Mirrored binary places coordinates binarPlaces.Add(new Point3D(3.7171, 16.8942, 0.0000)); binarPlaces.Add(new Point3D(-14.9187, 8.7557, 0.0000)); binarPlaces.Add(new Point3D(-12.9373, -11.4829, 0.0000)); binarPlaces.Add(new Point3D(6.9230, -15.8525, 0.0000)); binarPlaces.Add(new Point3D(17.2160, 1.6855, 0.0000)); } else { binarPlaces.Add(new Point3D(-3.6793, 16.9995, 0.0000)); binarPlaces.Add(new Point3D(-17.3045, 1.7539, 0.0000)); binarPlaces.Add(new Point3D(-7.0154, -15.9155, 0.0000)); binarPlaces.Add(new Point3D(12.9687, -11.5903, 0.0000)); binarPlaces.Add(new Point3D(15.0305, 8.7524, 0.0000)); } List <Point3D> binarPlacesHomography = Geometry.homography_calc(binarPlaces, hmMinDestPars, hmMinSourcePars); WeightCenter binFound = FindBinaryPlace(binarPlacesHomography, original); if (checkBoxWriteDetected.Checked) { using (StreamWriter str = new StreamWriter("homography.txt", true)) { //List<Point3D> weightListConverted = Geometry.CloneList(weightsList); //ConvertXY(weightListConverted, rows); //foreach (Point3D pt in weightListConverted) //{ // str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString()); //} //str.WriteLine(); //foreach (Point3D pt in homographyMin) //{ // str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString()); //} //str.WriteLine(); List <Point3D> weightListConverted = Geometry.CloneList(weightListMin); ConvertXY(weightListConverted, rows); foreach (Point3D pt in weightListConverted) { str.WriteLine(pt.X.ToString() + "," + pt.Y.ToString() + "," + pt.Z.ToString()); } str.WriteLine(); } } if (checkBoxVizDetectedSpots.Checked) { //DrawPoints(data, weightListMin, rows, cols); //ConvertYX(binarPlacesHomography, rows); //DrawPoints(original, binarPlacesHomography, rows, cols); VisualizeWeightCenter(binFound, rows, cols, original); } //List<List<Point3D>> result = new List<List<Point3D>>(3); //result.Add(weightListMin); return(binarPlacesHomography); } else { return(null); } }