public static Blob MergeBlobs(Blob LowerBlob, Blob UpperBlob) { Blob MergedBlob = new Blob(); MergedBlob.BlobNumber = Math.Min(UpperBlob.BlobNumber, LowerBlob.BlobNumber); MergedBlob.PixelCount = LowerBlob.PixelCount + UpperBlob.PixelCount; MergedBlob.Xmax = Math.Max(LowerBlob.Xmax, UpperBlob.Xmax); MergedBlob.Xmin = Math.Min(LowerBlob.Xmin, UpperBlob.Xmin); MergedBlob.Xsum = LowerBlob.Xsum + UpperBlob.Xsum; MergedBlob.Ymax = Math.Max(LowerBlob.Ymax, UpperBlob.Ymax); MergedBlob.Ymin = Math.Min(LowerBlob.Ymin, UpperBlob.Ymin); MergedBlob.Ysum = LowerBlob.Ysum + UpperBlob.Ysum; MergedBlob.X_Centroid = MergedBlob.Xsum / MergedBlob.PixelCount; MergedBlob.Y_Centroid = MergedBlob.Ysum / MergedBlob.PixelCount; return MergedBlob; }
public void ConsolidateBlobs() { if (Blobs.Length <= 1) { return; } Array.Sort(Blobs, (x, y) => x.BlobNumber - y.BlobNumber); int BlobCount = 1; for (int iBlob = 1; iBlob < Blobs.Length; iBlob++) { if (Blobs[iBlob].BlobNumber != Blobs[iBlob-1].BlobNumber) { BlobCount++; // count number of unique blobs (if blob number is the same do not add to the blob count } } Blob[] MergedBlobs = new Blob[BlobCount]; int CurrentBlob = 0; MergedBlobs[CurrentBlob] = Blobs[0]; for (int iBlob = 1; iBlob < Blobs.Length; iBlob++) { if ((Blobs[iBlob].BlobNumber == Blobs[iBlob - 1].BlobNumber)) { MergedBlobs[CurrentBlob] = Blob.MergeBlobs(Blobs[iBlob],MergedBlobs[CurrentBlob]); } else { CurrentBlob++; MergedBlobs[CurrentBlob] = Blobs[iBlob]; // reset the merge process } } Blobs = MergedBlobs; // now add in the blob bw image for (int iBlob = 0; iBlob < Blobs.Length; iBlob++) { int Width = Blobs[iBlob].Xmax - Blobs[iBlob].Xmin + 1; int Height = Blobs[iBlob].Ymax - Blobs[iBlob].Ymin + 1; int Xmin = Blobs[iBlob].Xmin; int Ymin = Blobs[iBlob].Ymin; Blobs[iBlob].BW_Image = new bool[Height, Width]; for (int i = Blobs[iBlob].Ymin; i <= Blobs[iBlob].Ymax; i++) { for (int j = Blobs[iBlob].Xmin; j <= Blobs[iBlob].Xmax; j++) { if (AssignedBlob[i, j] == Blobs[iBlob].BlobNumber) { Blobs[iBlob].BW_Image[i - Ymin, j - Xmin] = true; } } } } }
public void MatchBlobs() { if (BlobCount > 0) { int LowerEnd = 0; int UpperEnd = 0; int LowerStart = 0; int UpperStart = 0; int[] XstartPts = new int[BW.GetLength(1)]; int[] XendPts = new int[XstartPts.Length]; for (int iPt = 0; iPt < XstartPts.Length; iPt++) { XstartPts[iPt] = Array.FindIndex(Blobs, x => x.Xmin == iPt); XendPts[iPt] = Array.FindLastIndex(Blobs, x => x.Xmin == iPt); } for (int iCol = 1; iCol < XstartPts.Length; iCol++) { LowerStart = XstartPts[iCol - 1]; UpperStart = XstartPts[iCol]; LowerEnd = XendPts[iCol - 1]; UpperEnd= XendPts[iCol]; if (LowerStart == -1 || UpperStart == -1) { continue; } Blob[] LowerRowBlobs = new Blob[LowerEnd - LowerStart + 1]; Blob[] UpperRowBlobs = new Blob[UpperEnd - UpperStart + 1]; // Assign the blobs for (int iUpper = 0; iUpper < UpperRowBlobs.Length; iUpper++) { UpperRowBlobs[iUpper] = Blobs[UpperStart + iUpper]; } for (int iLower = 0; iLower < LowerRowBlobs.Length; iLower++) { LowerRowBlobs[iLower] = Blobs[LowerStart + iLower]; } for (int iLower = 0; iLower < LowerRowBlobs.Length; iLower++) { int yp1 = LowerRowBlobs[iLower].Ymin; int yp2 = LowerRowBlobs[iLower].Ymax; for (int iUpper = 0; iUpper < UpperRowBlobs.Length; iUpper++) { int y1 = UpperRowBlobs[iUpper].Ymin; int y2 = UpperRowBlobs[iUpper].Ymax; bool BlobMatch = Overlap(y1, y2, yp1, yp2); if (BlobMatch) { int MinBlobNumber = Math.Min(LowerRowBlobs[iLower].BlobNumber, UpperRowBlobs[iUpper].BlobNumber); LowerRowBlobs[iLower].BlobNumber = MinBlobNumber; UpperRowBlobs[iUpper].BlobNumber = MinBlobNumber; } } } // Reassign the blobs for (int iUpper = 0; iUpper < UpperRowBlobs.Length; iUpper++) { Blobs[UpperStart + iUpper] = UpperRowBlobs[iUpper]; } for (int iLower = 0; iLower < LowerRowBlobs.Length; iLower++) { Blobs[LowerStart + iLower] = LowerRowBlobs[iLower]; } } } }
public void Get_1D_Blobs() { this.Blobs = new Blob[BW.Length]; BlobCount = -1; Blobs[0] = new Blob(); for (int j = 0; j < BW.GetLength(1); j++) { for (int i = 0; i < BW.GetLength(0); i++) { if (BW[i,j]) { if (i == 0 || BW[i - 1, j] == false) { BlobCount++; Blobs[BlobCount] = new Blob(); Blobs[BlobCount].BlobNumber = BlobCount + 1; Blobs[BlobCount].Ymin = i; Blobs[BlobCount].Xmin = j; Blobs[BlobCount].Xmax = j; } Blobs[BlobCount].PixelCount++; Blobs[BlobCount].Ymax = i; Blobs[BlobCount].Ysum += i; Blobs[BlobCount].Xsum += j; } } } Array.Resize(ref this.Blobs, BlobCount); BlobCount = BlobCount + 1; }