Exemplo n.º 1
0
        public static Color RecalcPixelColor(List <Point3D> polygon, LockBitmap bitmap, int width, int height)
        {
            if (polygon.Count != 4)
            {
                return(Color.Empty);
            }

            int xMin = (int)Min4(polygon[0].X, polygon[1].X, polygon[2].X, polygon[3].X);
            int xMax = (int)Max4(polygon[0].X, polygon[1].X, polygon[2].X, polygon[3].X) + 1;
            int yMin = (int)Min4(polygon[0].Y, polygon[1].Y, polygon[2].Y, polygon[3].Y);
            int yMax = (int)Max4(polygon[0].Y, polygon[1].Y, polygon[2].Y, polygon[3].Y) + 1;

            double  RSum = 0, GSum = 0, BSum = 0, sa = 0;
            Point3D u1 = new Point3D();
            Point3D u2 = new Point3D();

            for (int xi = xMin; xi <= xMax - 1; xi++)
            {
                for (int yi = yMin; yi <= yMax - 1; yi++)
                {
                    u1.SetCoordinates(xi, yi, 0);
                    u2.SetCoordinates(xi + 1, yi + 1, 0);
                    Color c;
                    if ((xi < 1) || (xi > width) || (yi < 1) || (yi > height))
                    {
                        c = Color.White;
                    }
                    else
                    {
                        c = bitmap.GetPixel(xi - 1, yi - 1);
                    }
                    double s = Math.Abs(Surface4x2(polygon[0], polygon[1], polygon[2], polygon[3], u1, u2));
                    sa   += s;
                    RSum += c.R * s;
                    GSum += c.G * s;
                    BSum += c.B * s;
                }
            }

            if (sa == 0)
            {
                sa = 1;
            }
            int R = (int)Math.Round(RSum / sa);
            int G = (int)Math.Round(GSum / sa);
            int B = (int)Math.Round(BSum / sa);

            return(Color.FromArgb(R, G, B));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Translate pixel from one picture to other.
        /// </summary>
        /// <param name="Source">Main Image</param>
        /// <param name="Common">New Image</param>
        /// <param name="SoureRef">Point in main image</param>
        /// <param name="CommonRef">Same point in new image(It should be in different location, but is not necessary)</param>
        /// <param name="first">True for first bmp. False if image is second bmp.</param>
        private void TranslatePixel(Bitmap Source, Bitmap Common, Point3D SoureRef, Point3D CommonRef, bool first)
        {
            LockBitmap SourceLock = new LockBitmap(Source);
            LockBitmap CommonLock = new LockBitmap(Common);

            SourceLock.LockBits();
            CommonLock.LockBits();
            int width;
            int start;

            if (first)
            {
                int index;
                if (LeftCase)
                {
                    index = CropSecondBmpPoint(FirstPoints);
                    start = (int)FirstPoints[index].X + 1;
                    width = SourceLock.Width;
                }
                else
                {
                    index = CropFirstBmpPoint(FirstPoints);
                    width = (int)FirstPoints[index].X + 1;
                    start = 0;
                }
            }
            else
            {
                width = SourceLock.Width;
                start = 0;
            }


            int xDiff = (int)(SoureRef.X - CommonRef.X);
            int yDiff = (int)(SoureRef.Y - CommonRef.Y);

            for (int i = start; i < width; i++)
            {
                for (int j = 0; j < SourceLock.Height; j++)
                {
                    Color c = SourceLock.GetPixel(i, j);
                    CommonLock.SetPixel(i - xDiff, j - yDiff, c);
                }
            }
            SourceLock.UnlockBits();
            CommonLock.UnlockBits();
        }
Exemplo n.º 3
0
        private void RecalculateColor(Bitmap Original, Bitmap Destination, List <List <Point3D> > HomographySecondToFirst)
        {
            //Recalculate new color of the pixel.
            LockBitmap SecondBmpLock = new LockBitmap(Original);

            SecondBmpLock.LockBits();
            LockBitmap SecondBmpResultLock = new LockBitmap(Destination);

            SecondBmpResultLock.LockBits();

            int widthSecondOriginal  = Original.Width;
            int heightSecondOriginal = Original.Height;

            ProgBar.Maximum = HomographySecondToFirst.Count;
            ProgBar.Minimum = 0;
            ProgBar.Step    = 1;
            ProgBar.Value   = 0;
            for (int j = 0; j < HomographySecondToFirst.Count - 2; j++)
            {
                for (int i = 0; i < HomographySecondToFirst[j].Count - 2; i++)
                {
                    //Surrounded pixel from Points
                    List <Point3D> currList = new List <Point3D>();
                    currList.Add(HomographySecondToFirst[j][i]);
                    currList.Add(HomographySecondToFirst[j][i + 1]);
                    currList.Add(HomographySecondToFirst[j + 1][i + 1]);
                    currList.Add(HomographySecondToFirst[j + 1][i]);

                    // Only if the distorted polygon is inside the bitmap
                    if (!CompareTwoRetangle(currList, widthSecondOriginal, heightSecondOriginal))
                    {
                        Color c = RemapPixels.RecalcPixelColor(currList, SecondBmpLock, widthSecondOriginal, heightSecondOriginal);
                        SecondBmpResultLock.SetPixel(i, j, c);
                    }
                }
                ProgBar.PerformStep();
            }

            SecondBmpLock.UnlockBits();
            SecondBmpResultLock.UnlockBits();
        }