'Find the min max coordinates using bruteforce matcher

Using the famous brute force cv2 code https://docs.opencv.org/4.x/dc/dc3/tutorial_py_matcher.html I'm tring to get all the good points coordinate to get the area

import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt

img1 = cv.imread('images/template.png',cv.IMREAD_GRAYSCALE)          # queryImage
img2 = cv.imread('screen.png',cv.IMREAD_GRAYSCALE) # trainImage
# Initiate SIFT detector
sift = cv.SIFT_create()
# find the keypoints and descriptors with SIFT
kp1, des1 = sift.detectAndCompute(img1,None)
kp2, des2 = sift.detectAndCompute(img2,None)
# BFMatcher with default params
bf = cv.BFMatcher()
matches = bf.knnMatch(des1,des2,k=2)
# Apply ratio test
good = []
for m,n in matches:
    if m.distance < 0.75*n.distance:
        good.append([m])

# GETTING THE COORDINATE
p = []
for match in good:
  p.append(kp2[match[0].trainIdx].pt)

As you can see in the end I get all the good point founded in the img2

p dump is:

[(549.0333862304688, 821.7780151367188)
..some othe points
(549.0333862304688, 821.7780151367188)
(630.9489135742188, 822.0816040039062)]

So I try to designe the polyline:

isClosed = True
# Blue color in BGR
color = (255, 0, 0)
# Line thickness of 2 px
thickness = 2

img2 = cv.polylines(img2, np.int32([p]), 
                      isClosed, color, thickness)

plt.imshow(img2),plt.show()

And I get this image (wait!!! the black rectangle is draw by my self):

Result

How can the get min max X and Y points to get the external coordinate of the black rectangle??



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source