Пример #1
0
        public void CreateLineSegments()
        {
            LineSegments.Clear();
            float[] buffer         = imageToMarch.GetBuffer();
            int     strideInFloats = imageToMarch.StrideInFloats();

            for (int y = 0; y < imageToMarch.Height - 1; y++)
            {
                int offset = imageToMarch.GetBufferOffsetY(y);
                for (int x = 0; x < imageToMarch.Width - 1; x++)
                {
                    int   offsetWithX = offset + x;
                    float point0      = buffer[offsetWithX + strideInFloats];
                    float point1      = buffer[offsetWithX + 1 + strideInFloats];
                    float point2      = buffer[offsetWithX + 1];
                    float point3      = buffer[offsetWithX];
                    int   flags       = (point0 > threshold) ? 1 : 0;
                    flags = (flags << 1) | ((point1 > threshold) ? 1 : 0);
                    flags = (flags << 1) | ((point2 > threshold) ? 1 : 0);
                    flags = (flags << 1) | ((point3 > threshold) ? 1 : 0);

                    bool wasFlipped = false;
                    if (flags == 5)
                    {
                        float average = (point0 + point1 + point2 + point3) / 4;
                        if (average < threshold)
                        {
                            flags      = 10;
                            wasFlipped = true;
                        }
                    }
                    else if (flags == 10)
                    {
                        float average = (point0 + point1 + point2 + point3) / 4;
                        if (average < threshold)
                        {
                            flags      = 5;
                            wasFlipped = true;
                        }
                    }

                    AddSegmentForFlags(x, y, flags, wasFlipped);
                }
            }
        }
Пример #2
0
        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++;
                    }
                }
            }
        }