想要在两个相似的图像中找到相同的角点

时间:2019-05-17 07:25:25

标签: c#

`

public unsafe Point ThresholdUA(BitmapData bDataR, BitmapData bDataL)
        {
            StreamWriter sw1 = new StreamWriter("C:\\Users\\SHUBHAM\\Desktop\\log.txt");
            byte bitsPerPixel = Convert.ToByte(bDataL.Stride / img1.Width);
            //This time we convert the IntPtr to a ptr
            byte* bDataL_scan0 = (byte*)bDataL.Scan0.ToPointer();
            byte* bDataR_scan0 = (byte*)bDataR.Scan0.ToPointer();
            int size = 5;
            Double minSum = 99999.0F;
            Point matchingPos = new Point(0, 0);
            for (int i = 0; i < bData2.Height-size; i++)
            {
                for (int j = 0; j < bData2.Width-size; j++)
                { 
                    double sum = 0;
                    for (int p = 0; p < size; p++)
                        for (int q = 0; q < size; q++)
                        {
                            byte* bDataRp = bDataR_scan0 + (i + p) * bDataR.Stride + (j + q) * bitsPerPixel;
                            byte* bDataLp = bDataL_scan0 + p * bDataL.Stride + q * bitsPerPixel;
                            byte R1 = bDataLp[2];
                            byte G1 = bDataLp[1];
                            byte B1 = bDataLp[0];
                            byte R2 = bDataRp[2];
                            byte G2 = bDataRp[1];
                            byte B2 = bDataRp[0];
                            Double diffR = Math.Abs(R1 - R2); 
                            Double diffG = Math.Abs(G1 - G2);
                            Double diffB = Math.Abs(B1 - B2);
                            sum += diffR * diffR + diffG * diffG + diffB * diffB;
                        }
                    if (sum < minSum)
                    {
                        minSum = sum; matchingPos = new Point(i, j);
                        sw1.WriteLine("i=" + i.ToString() + "    j=" + j.ToString() + "     Sum:" + sum.ToString() + "      matchingPos" + matchingPos.ToString());
                    }
                }
            }
            Debug.WriteLine("Inside ThresholdUA "+matchingPos.X.ToString()+", "+matchingPos.Y.ToString());
            sw1.Close();
            return matchingPos;
        }

        private void pictureBox_MouseClick(object sender, MouseEventArgs e)
        {
            this.Cursor = new Cursor(Cursor.Current.Handle);

            int xCoordinate = Cursor.Position.X;
            int yCoordinate = Cursor.Position.Y;

            Debug.WriteLine(xCoordinate.ToString(), yCoordinate.ToString());
            if (manualMatching == true)
            {
                if (lastPos.X == 0 && lastPos.Y == 0)
                    lastPos = new PointF(Cursor.Position.X, Cursor.Position.Y);
                else
                {

                    PointF[] measurement = new PointF[2];
                    measurement[0]=lastPos;
                    measurement[1]= new PointF(Cursor.Position.X, Cursor.Position.Y);
                    measurements.Add(measurement); lastPos = new PointF(0, 0);
                }
            }
            else
            { 
                BitmapData bDataL;
                bDataL = img1.LockBits(new Rectangle(xCoordinate, yCoordinate, xCoordinate+5, yCoordinate+5), ImageLockMode.ReadOnly, img2.PixelFormat);
                Point image2Matched =  ThresholdUA(bData2, bDataL);
                drawCursor(pictureBox, image2Matched.X, image2Matched.Y, bDataL);
                img1.UnlockBits(bDataL);
            }
        }

private void drawCursor(PictureBox pBox, int x, int y, BitmapData bData1)
    {
        Point img2Matched = new Point(x + (pictureBox.ClientSize.Width/2), y);
        Graphics g1 = pBox.CreateGraphics();
        Debug.WriteLine("Matching position "+ img2Matched.X.ToString()+","+ img2Matched.Y.ToString ());
        g1.DrawLine(Pens.Red, new Point(img2Matched.X - 5, img2Matched.Y - 5), new Point(img2Matched.X + 5, img2Matched.Y + 5));
        g1.DrawLine(Pens.Red, new Point(img2Matched.X + 5, img2Matched.Y - 5), new Point(img2Matched.X - 5, img2Matched.Y + 5));
    }

` 我想在两个相似的图像中找到一个共同的角点。就像我单击图像1中的特定点一样,该软件可以在另一幅图像的同一点上绘制一些东西(例如小十字)。使用此代码,我可以在随机位置绘制光标。但是我的目标是在与单击第一张图片完全相同的位置绘制一个光标。

Image 1

Image 2

0 个答案:

没有答案