public double[] GetIntensityGrouped(ImageMatrix CurrentImage, int[][][] borders, double threshold, int groupsize) //get intensity of (groups of) pixel(s) of an image. Threshold can be adjusted realtime { double localintensitycurrent = new double(); double localintensitycurrentgroup = new double(); double localintensity = new double(); double[] intensity = new double[borders.Length + 1]; // addition first region for (int x = 0; x < borders[0].Length - 2 - groupsize; x += groupsize) { for (int y = 0; y < borders[0][x][1]; y += groupsize) { localintensitycurrentgroup = 0; for (int a = 0; a < groupsize; a++) // for nr pixels in square of groupsize x groupsize, add up intensities from current frame and add up intensities from last frame. { for (int b = 0; b < groupsize; b++) { localintensitycurrent = CurrentImage.data[x + a, y + b]; localintensitycurrentgroup += localintensitycurrent; } } localintensity = localintensitycurrentgroup / (Math.Pow(groupsize, 2)); // get mean localintensity of current group if (localintensity > threshold) { intensity[0] += localintensity; } } } // addition in-between regions for (int i = 1; i < borders.Length; i++) { for (int x = 1; x < borders[0].Length - 2 - groupsize; x++) { for (int y = borders[i - 1][x][1]; y < borders[i][x][1]; y++) { localintensitycurrentgroup = 0; for (int a = 0; a < groupsize; a++) // for nr pixels in square of groupsize x groupsize, add up intensities from current frame and add up intensities from last frame. { for (int b = 0; b < groupsize; b++) { localintensitycurrent = CurrentImage.data[x + a, y + b]; localintensitycurrentgroup += localintensitycurrent; } } localintensity = localintensitycurrentgroup / (Math.Pow(groupsize, 2)); // get mean localintensity of last group if (localintensity > threshold) { intensity[i] += localintensity; } } } } // addition last region for (int x = 0; x < borders[0].Length - 2 - groupsize; x++) { for (int y = borders[borders.Length - 1][x][1]; y < CurrentImage.height - groupsize; y++) { localintensitycurrentgroup = 0; for (int a = 0; a < groupsize; a++) // for nr pixels in square of groupsize x groupsize, add up intensities from current frame and add up intensities from last frame. { for (int b = 0; b < groupsize; b++) { localintensitycurrent = CurrentImage.data[x + a, y + b]; localintensitycurrentgroup += localintensitycurrent; } } localintensity = localintensitycurrentgroup / (Math.Pow(groupsize, 2)); // get mean localintensity of last group if (localintensity > threshold) { intensity[borders.Length] += localintensity; } } } return(intensity); }
public int[] HoughTransform(ImageMatrix originalImage, int desiredLines, float[,] ImageData) // use houghtransform to detect most probable lines (in form of y = rc_param*x+ verticalpixel) on output of convolution { float rc_param = 0.005f; // controls how steep the lines can be. 0.004 is a slope of ~12.5 percent. The max value depends on nr of channels float channelwidth_param = 70; // Controls over how many pixels the mean is taken when extracting the middle of a channel double Line = new double(); int StripLength = (int)(width / 2); int StripHeigth = height; int NrAngles = 20; double[] rc = new double[2 * NrAngles + 1]; double decPart = new double(); int intPart = new int(); double[] BrightestAngle = new double[40]; double[] LineValues = new double[StripHeigth]; double[] Slope = new double[2 * NrAngles]; double[] BestSlope = new double[StripHeigth]; // array with 'best' angles for every pixel for (int x = 0; x < StripHeigth; x++) // For every vertical pixel: add the brightness values of 100 horizontal pixels at different angles. { for (int y = -NrAngles; y < NrAngles; y++) // For number of angles of lines through pixel { rc[y + NrAngles] = y * rc_param; for (int z = 0; z < width; z++) // For every horizontal pixel in the line rc*x { decPart = Math.Abs((rc[y + NrAngles] * z)) % 1; // Decimal part of heigth of pixel in line rc*x intPart = (int)(rc[y + NrAngles] * z) + x; // Integer part of heigth of pixel in line rc*x if (intPart >= 0 && intPart < (StripHeigth - 1)) // If pixel is INSIDE of picture (because lines can go out of the picture) { Line += ImageData[z, intPart] * decPart + ImageData[z, intPart + 1] * (1 - decPart); //Sum of brightness values in line rc*x } else // If pixel is outside of picture neglect its contribution. (so add brightness 0) { Line = 0f; } } BrightestAngle[y + NrAngles] = Line; // Array of lines, all a different angle, with their brightness values Line = 0; // reset line value } LineValues[x] = BrightestAngle.Max(); // Array of brightness values of lines, the lightest out of NrAngles. BestSlope[x] = rc[Array.IndexOf(BrightestAngle, BrightestAngle.Max())]; //LineValues[x] = BrightestAngle.Min(); // Array of brightness values of lines, the darkest out of NrAngles. } double[] LineValuesUnsorted = new double[LineValues.Length]; //store original copy of LineValues to preserve indexes Array.Copy(LineValues, LineValuesUnsorted, LineValues.Length); Array.Sort(LineValues); //sort: first value the smallest Array.Reverse(LineValues); //sort: last value the smallest double[] LineValuesMeans = new double[LineValuesUnsorted.Length]; //calculates mean over .. neighbouring pixels per pixel int boundary = (int)channelwidth_param / desiredLines; double sum = 0; for (int c = 0; c < LineValuesUnsorted.Length; c++) { try { for (int d = -boundary; d < boundary; d++) { sum += LineValuesUnsorted[c + d]; } } catch (System.IndexOutOfRangeException) { } LineValuesMeans[c] = sum / (2 * boundary); sum = 0; } double[] lineValuesPart = new double[(int)height / desiredLines]; double MaxPart = new double(); int[] ChannelIndex = new int[desiredLines]; for (int e = 0; e < desiredLines; e++) { for (int f = 0; f < lineValuesPart.Length; f++) { lineValuesPart[f] = LineValuesMeans[e * lineValuesPart.Length + f]; } MaxPart = lineValuesPart.Max(); ChannelIndex[e] = Array.IndexOf(lineValuesPart, MaxPart) + (e * lineValuesPart.Length); } return(ChannelIndex); }