// Get array of objects images internal Blob[] GetObjectsWithArray(ZagImage <byte> SourceImage) { byte[] src = SourceImage.Data; int width = SourceImage.Width; int height = SourceImage.Height; // process the image ProcessImage(src, width, height); int[] labels = ObjectLabels; int count = ObjectsCount; // image size int i = 0, label; // --- STEP 1 - find each objects coordinates // create object coordinates arrays int[] x1 = new int[count + 1]; int[] y1 = new int[count + 1]; int[] x2 = new int[count + 1]; int[] y2 = new int[count + 1]; for (int k = 1; k <= count; k++) { x1[k] = width; y1[k] = height; } // walk through labels array for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, i++) { // get current label label = labels[i]; // skip unlabeled pixels if (label == 0) { continue; } // check and update all coordinates if (x < x1[label]) { x1[label] = x; } if (x > x2[label]) { x2[label] = x; } if (y < y1[label]) { y1[label] = y; } if (y > y2[label]) { y2[label] = y; } } } // --- STEP 2 - get each object Blob[] objects = new Blob[count]; // create each array for (int k = 1; k <= count; k++) { int xmin = x1[k]; int xmax = x2[k]; int ymin = y1[k]; int ymax = y2[k]; int objectWidth = xmax - xmin + 1; int objectHeight = ymax - ymin + 1; bool[,] array = new bool[objectWidth, objectHeight]; int p = ymin * width + xmin; int labelsOffset = width - objectWidth; // for each line for (int y = ymin; y <= ymax; y++) { // copy each pixel for (int x = xmin; x <= xmax; x++, p++) { if (labels[p] == k) { array[x - xmin, y - ymin] = true; } else { array[x - xmin, y - ymin] = false; } } p += labelsOffset; } objects[k - 1] = new Blob(array, xmin, ymin, objectWidth, objectHeight); } return(objects); }
internal Blob[] GetObjectsWithoutArray(ZagImage <byte> SourceImage) { byte[] src = SourceImage.Data; int width = SourceImage.Width; int height = SourceImage.Height; // process the image ProcessImage(src, width, height); int[] labels = ObjectLabels; int count = ObjectsCount; int i = 0, label; // --- STEP 1 - find each objects coordinates // create object coordinates arrays int[] x1 = new int[count + 1]; int[] y1 = new int[count + 1]; int[] x2 = new int[count + 1]; int[] y2 = new int[count + 1]; //For Area int[] area = new int[count + 1]; //For Center of Gravity long[] xc = new long[count + 1]; long[] yc = new long[count + 1]; for (int k = 1; k <= count; k++) { x1[k] = width; y1[k] = height; } // walk through labels array for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++, i++) { // get current label label = labels[i]; // skip unlabeled pixels if (label == 0) { continue; } // check and update all coordinates if (x < x1[label]) { x1[label] = x; } if (x > x2[label]) { x2[label] = x; } if (y < y1[label]) { y1[label] = y; } if (y > y2[label]) { y2[label] = y; } area[label]++; xc[label] += x; yc[label] += y; } } // --- STEP 2 - get each object Blob[] objects = new Blob[count]; // create each array for (int k = 1; k <= count; k++) { int xmin = x1[k]; int xmax = x2[k]; int ymin = y1[k]; int ymax = y2[k]; int objectWidth = xmax - xmin + 1; int objectHeight = ymax - ymin + 1; objects[k - 1] = new Blob(xmin, ymin, objectWidth, objectHeight); objects[k - 1].CenterOfGravityX = xc[k] / (float)area[k]; objects[k - 1].CenterOfGravityY = yc[k] / (float)area[k]; objects[k - 1].Area = area[k]; } return(objects); }