Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static public Dictionary<Tuple<int, int>, int[]> EdgeRecalculation(Image<Bgr, byte> img, Image<Bgr, byte> imgCpy, Dictionary<int, double[]> obj_list, Dictionary<Tuple<int, int>, int[]> piece_resemblance, int macro, Tuple<int, int> micro)
- {
- unsafe
- {
- MIplImage final = img.MIplImage;
- MIplImage copy = imgCpy.MIplImage;
- byte* dataPtr = (byte*)final.imageData.ToPointer(); // Pointer to the final image
- byte* dataPtrCpy = (byte*)copy.imageData.ToPointer(); // Pointer to the original image
- byte* sqr1Ptr, sqr2Ptr, AuxPtr;
- byte* dataPtrStatic = dataPtr;
- int nChan = final.nChannels;
- int widthStep = final.widthStep;
- Tuple<int, int> piecesCombination; // key piece_resemblance
- List<int> keyList = new List<int>(obj_list.Keys);
- int[] labelArray = new int[keyList.Count];
- double edgeSize1, edgeSize2;
- double n, m, u, v;
- int i, t;
- int gray1, gray2;
- if (obj_list.Count < 2)
- return null;
- foreach (Tuple<int, int> pieceIteration in piece_resemblance.Keys.ToList())
- {
- if (pieceIteration.Item1 == micro.Item1
- || pieceIteration.Item1 == micro.Item2
- || pieceIteration.Item2 == micro.Item1
- || pieceIteration.Item2 == micro.Item2)
- piece_resemblance.Remove(pieceIteration);
- }
- i = 0;
- // Pass label values from list to array
- foreach (int keyIterator in keyList)
- {
- labelArray[i] = keyIterator;
- i++;
- }
- for (i = 0; i < labelArray.Length; i++)
- {
- if (labelArray[i] != macro)
- {
- if (labelArray[i] < macro)
- piecesCombination = new Tuple<int, int>(labelArray[i], macro);
- else
- piecesCombination = new Tuple<int, int>(macro, labelArray[i]);
- // Get the corners for each object
- obj_list.TryGetValue(piecesCombination.Item1, out double[] corners1);
- obj_list.TryGetValue(piecesCombination.Item2, out double[] corners2);
- if (corners1[5] == 0)
- sqr1Ptr = dataPtrCpy;
- else
- sqr1Ptr = dataPtr;
- if (corners2[5] == 0)
- sqr2Ptr = dataPtrCpy;
- else
- sqr2Ptr = dataPtr;
- // Edge difference created here to generate a 'new' variable. Otherwise, it would overwrite the other edges.
- // Init with max contrast
- // [0] Left edge; [1] Right edge; [2] Top edge; [3] Bottom edge;
- int[] edgeDiff = new int[4] { 255, 255, 255, 255 }; // value piece_resemblance
- // ---------------------------------------------------------- Left & Right edges
- // Left edges
- edgeSize1 = corners1[3] - corners1[1] + 1;
- edgeSize2 = corners2[3] - corners2[1] + 1;
- if (edgeSize1 == edgeSize2) // Level2. Length of edge must be the same
- {
- for (t = 0; t < edgeSize1; t++) // Iterate along the edge
- {
- n = corners1[1];
- v = corners2[1];
- // Left side x -> Right side x
- m = corners1[0];
- u = corners2[2];
- AuxPtr = sqr1Ptr + (int)(n + t) * widthStep + (int)m * nChan;
- gray1 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- AuxPtr = sqr2Ptr + (int)(v + t) * widthStep + (int)u * nChan;
- gray2 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- edgeDiff[0] += Math.Abs(gray1 - gray2);
- // Right side x -> Left side x
- m = corners1[2];
- u = corners2[0];
- AuxPtr = sqr1Ptr + (int)(n + t) * widthStep + (int)m * nChan;
- gray1 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- AuxPtr = sqr2Ptr + (int)(v + t) * widthStep + (int)u * nChan;
- gray2 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- edgeDiff[1] += Math.Abs(gray1 - gray2);
- }
- // Get the average difference of values between edges
- edgeDiff[0] = (int)Math.Round((edgeDiff[0] - 255) / edgeSize1); // [0 ... 255]
- edgeDiff[1] = (int)Math.Round((edgeDiff[1] - 255) / edgeSize1); // [0 ... 255]
- }
- // ---------------------------------------------------------- Top & Bottom edges
- // Top edges
- edgeSize1 = corners1[2] - corners1[0] + 1;
- edgeSize2 = corners2[2] - corners2[0] + 1;
- if (edgeSize1 == edgeSize2) // Level2. Length of edge must be the same
- {
- for (t = 0; t < edgeSize1; t++) // Iterate along the edge
- {
- m = corners1[0];
- u = corners2[0];
- // Top side y -> Bottom side y
- n = corners1[1];
- v = corners2[3];
- AuxPtr = sqr1Ptr + (int)n * widthStep + (int)(m + t) * nChan;
- gray1 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- AuxPtr = sqr2Ptr + (int)v * widthStep + (int)(u + t) * nChan;
- gray2 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- edgeDiff[2] += Math.Abs(gray1 - gray2);
- // Bottom side y -> Top side y
- n = corners1[3];
- v = corners2[1];
- AuxPtr = sqr1Ptr + (int)n * widthStep + (int)(m + t) * nChan;
- gray1 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- AuxPtr = sqr2Ptr + (int)v * widthStep + (int)(u + t) * nChan;
- gray2 = (int)Math.Round((AuxPtr[0] + AuxPtr[1] + AuxPtr[2]) / 3.0);
- edgeDiff[3] += Math.Abs(gray1 - gray2);
- }
- // Get the average difference of values between edges
- edgeDiff[2] = (int)Math.Round((edgeDiff[2] - 255) / edgeSize1); // [0 ... 255]
- edgeDiff[3] = (int)Math.Round((edgeDiff[3] - 255) / edgeSize1); // [0 ... 255]
- }
- // Store comparison with edge difference
- piece_resemblance.Add(piecesCombination, edgeDiff);
- }
- }
- return piece_resemblance;
- }
- } // End of Edge Recalculation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement