public static void DoMatch(ImageBufferFloat imageToSearch, ImageBufferFloat imageToFind, ImageBufferFloat result) { result = new ImageBufferFloat(imageToSearch.Width, imageToSearch.Height, 32, new BlenderBGRAFloat()); if (imageToSearch.Width >= imageToFind.Width && imageToSearch.Height >= imageToFind.Height && imageToSearch.BitDepth == imageToFind.BitDepth) { int floatsPerPixel = imageToSearch.BitDepth / 32; int searchDistanceBetweenPixels = imageToSearch.GetFloatsBetweenPixelsInclusive(); int findDistanceBetweenPixels = imageToFind.GetFloatsBetweenPixelsInclusive(); float[] searchBuffer = imageToSearch.GetBuffer(); float[] findBuffer = imageToFind.GetBuffer(); float[] resultBuffer = imageToFind.GetBuffer(); int resutsBufferOffset = 0; for (int matchY = 0; matchY <= imageToSearch.Height - imageToFind.Height; matchY++) { for (int matchX = 0; matchX <= imageToSearch.Width - imageToFind.Width; matchX++) { double currentLeastSquares = 0; for (int imageToFindY = 0; imageToFindY < imageToFind.Height; imageToFindY++) { int searchBufferOffset = imageToSearch.GetBufferOffsetXY(matchX, matchY + imageToFindY); int findBufferOffset = imageToFind.GetBufferOffsetY(imageToFindY); for (int findX = 0; findX < imageToFind.Width; findX++) { for (int byteIndex = 0; byteIndex < floatsPerPixel; byteIndex++) { float aByte = searchBuffer[searchBufferOffset + byteIndex]; float bByte = findBuffer[findBufferOffset + byteIndex]; int difference = (int)aByte - (int)bByte; currentLeastSquares += difference * difference; } searchBufferOffset += searchDistanceBetweenPixels; findBufferOffset += findDistanceBetweenPixels; } } resultBuffer[resutsBufferOffset] = (float)currentLeastSquares; resutsBufferOffset++; } } } }
LineSegmentFloat GetInterpolatedSegment(LineSegmentFloat segmentA, LineSegmentFloat segmentB) { #if false float colorAStart = Math.Min(imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentA.start.x, (int)segmentA.start.y)], 1); float colorAEnd = Math.Min(imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentA.end.x, (int)segmentA.end.y)], 1); Vector2 directionA = segmentA.end - segmentA.start; double offsetA = 1 - (colorAEnd + colorAStart) / 2.0; directionA *= offsetA; float colorBStart = Math.Min(imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentB.start.x, (int)segmentB.start.y)], 1); float colorBEnd = Math.Min(imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentB.end.x, (int)segmentB.end.y)], 1); #else float colorAStart = imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentA.start.x, (int)segmentA.start.y)]; float colorAEnd = imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentA.end.x, (int)segmentA.end.y)]; Vector2 directionA = segmentA.end - segmentA.start; double offsetA = 1 - (colorAEnd + colorAStart) / 2.0; directionA *= offsetA; float colorBStart = imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentB.start.x, (int)segmentB.start.y)]; float colorBEnd = imageToMarch.GetBuffer()[imageToMarch.GetBufferOffsetXY((int)segmentB.end.x, (int)segmentB.end.y)]; #endif Vector2 directionB = segmentB.end - segmentB.start; double ratioB = 1 - (colorBEnd + colorBStart) / 2.0; directionB *= ratioB; double offsetToPixelCenter = .5; LineSegmentFloat segment = new LineSegmentFloat( (segmentA.start.x + directionA.x) + offsetToPixelCenter, (segmentA.start.y + directionA.y) + offsetToPixelCenter, (segmentB.start.x + directionB.x) + offsetToPixelCenter, (segmentB.start.y + directionB.y) + offsetToPixelCenter); return(segment); }