M
M
mIka012021-02-07 22:49:28
Algorithms
mIka01, 2021-02-07 22:49:28

Finding special points?

Hello, I can't write an algorithm for finding singular points in two photos.

I tried to write algorithms from this article .
Wrote without libraries (algorithms).
I was able to find points, but not descriptors. As a result, the essence of the algorithm failed.
I decided to write in python (I don't know him). I used the ORB algorithm, I didn't really like it.

import cv2
import numpy as np

COUNT = 10
CONST = 10
l = 15
x3 = 0
y3 = 0

img1 = cv2.imread("the_book_thief.jpg", cv2.IMREAD_GRAYSCALE)
img2 = cv2.imread("me_holding_book.jpg", cv2.IMREAD_GRAYSCALE)

# ORB Detector
orb = cv2.ORB_create()
kp1, des1 = orb.detectAndCompute(img1, None)
kp2, des2 = orb.detectAndCompute(img2, None)

# Brute Force Matching
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key=lambda x: x.distance)

i = 0

w = img1.shape[:2][::-1]
h = img2.shape[:2][::-1]

for item in matches[:COUNT]:
    p1 = kp1[item.queryIdx].pt
    p1 = (round(p1[0]), round(p1[1]))

    p2 = kp2[item.trainIdx].pt
    p2 = (round(p2[0]), round(p2[1]))

    print('[{}] {}<----->{}'.format(i, p1, p2))
  if math.fabs(round(p1[1]) - round(p2[1])) < CONST:
    a = 90 - (round(p1[0]) - (w/2)) * 0.0624
    b = 90 + (round(p2[0]) - (w/2)) * 0.0624
    y = (180 - a - b)
    z = (l * math.sin(b * (math.pi / 180)) * math.sin(a * (math.pi / 180))) / (math.sin(y * (math.pi / 180)))
    x = math.sqrt(math.pow((z / math.sin(y * (math.pi / 180))), 2) - math.pow(z, 2))
    y = math.sqrt(math.pow((x/(round(p1[0])/(math.sqrt(math.pow(round(p1[0]),2) + math.pow(round(p1[1]),2))))),2) math.pow(x ,2))
    print(x ' ' y ' ' z)
    i += 1

matching_result = cv2.drawMatches(img1, kp1, img2, kp2, matches[:COUNT], None, flags=2)

cv2.imshow("Img1", img1)
cv2.imshow("Img2", img2)
cv2.imshow("Matching result", matching_result)
cv2.waitKey(0)
cv2.destroyAllWindows()

Problems are selected, all points (I need to be able to distinguish the percentage of similarity). If necessary, the adjustment is made by the line COUNT (number of points). Also python is slow.

Help me find ready-made code using libraries (I worked a little with them, I didn’t understand OpenCV).
In c# language.

Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mIka01, 2021-02-09
@mIka01

Help, it seems to have done but not exactly. Expert opinion required.
Libraries:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Diagnostics;

using System.Drawing;
using System.Drawing.Imaging;

using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Windows.Forms;

Public variables (You can do without them):
public Bitmap img1 = new Bitmap(@"C:\(путь)\1.bmp", true);
        public Bitmap img2 = new Bitmap(@"C:\(путь)\4.bmp", true);
        public BitmapImage bi;
        public Bitmap grayImage;

The code itself:
Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            
            img1 = filter.Apply(img1);
            img2 = filter.Apply(img2);

            Stopwatch st = new Stopwatch();
            st.Start();

            
            SusanCornersDetector scd = new SusanCornersDetector(30, 18);
            List<IntPoint> points = scd.ProcessImage(img1);

           
            ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching(12, 36);
            
            List<BlockMatch> matches = bm.ProcessImage(img1, points, img2);

            Console.WriteLine(matches[0].MatchPoint.X);
            Console.WriteLine(matches[0].MatchPoint.Y);
            Console.WriteLine(matches[0].Similarity);
            Console.WriteLine(matches[0].SourcePoint.X);
            Console.WriteLine(matches[0].SourcePoint.Y);

If necessary, then the full code with linking points:
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Diagnostics;

using System.Drawing;
using System.Drawing.Imaging;

using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Windows.Forms;

namespace углы
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public Bitmap img1 = new Bitmap(@"C:\(путь)\1.bmp", true);
        public Bitmap img2 = new Bitmap(@"C:\(путь)\4.bmp", true);
        public BitmapImage bi;
        public Bitmap grayImage;

        private void button1_Click(object sender, EventArgs e)
        {
            float consta = (float)Convert.ToDouble(textBox1.Text);

            Grayscale filter = new Grayscale(0.2125, 0.7154, 0.0721);
            
            img1 = filter.Apply(img1);
            img2 = filter.Apply(img2);

            Stopwatch st = new Stopwatch();
            st.Start();

            
            SusanCornersDetector scd = new SusanCornersDetector(30, 18);
            List<IntPoint> points = scd.ProcessImage(img1);

           
            ExhaustiveBlockMatching bm = new ExhaustiveBlockMatching(12, 36);
            
            List<BlockMatch> matches = bm.ProcessImage(img1, points, img2);

            //Console.WriteLine(matches[0].MatchPoint.X);
            //Console.WriteLine(matches[0].MatchPoint.Y);
            //Console.WriteLine(matches[0].Similarity);
            //Console.WriteLine(matches[0].SourcePoint.X);
            //Console.WriteLine(matches[0].SourcePoint.Y);


            st.Stop();
            TimeSpan elapsed = st.Elapsed;
            label1.Text = "Elapsed time = " + elapsed.ToString();


            BitmapData data = img1.LockBits(
                new System.Drawing.Rectangle(0, 0, img1.Width, img1.Height),
                ImageLockMode.ReadWrite, img1.PixelFormat);

            foreach (BlockMatch match in matches)
            {
                
                AForge.Imaging.Drawing.FillRectangle(data,
                    new System.Drawing.Rectangle(match.SourcePoint.X - 1, match.SourcePoint.Y - 1, 3, 3),
                    System.Drawing.Color.Yellow);
                
                AForge.Imaging.Drawing.Line(data, match.SourcePoint, match.MatchPoint, System.Drawing.Color.Red);

                //if (match.Similarity > 0.98f)
                if (match.Similarity > consta)                
                {
                    // process block with high similarity
                }
            }

            img1.UnlockBits(data);
            pictureBox1.Image = img1;
            pictureBox2.Image = img2;
        }
    }
}

I have vague doubts about the inoperability of the program or the camera is bad.
Help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question