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()
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 Bitmap img1 = new Bitmap(@"C:\(путь)\1.bmp", true);
public Bitmap img2 = new Bitmap(@"C:\(путь)\4.bmp", true);
public BitmapImage bi;
public Bitmap grayImage;
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);
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;
}
}
}