J
J
Jek2020-07-22 19:25:05
Python
Jek, 2020-07-22 19:25:05

How to get pattern coordinates in a picture in OpenCV?

Need to get image coordinates in screenshot to crop screenshot, how to do it?
The main idea is that the python scrolls through the pages in the browser, screenshots them, crops, glues the pages and lays them down in pdf. Just reading purchased books in the browser is inconvenient, every time you need to look for the place where you left off.
maybe there is already such a program in python? I would read
the code copied mainly from the official site of opencv and produces a picture as a result:

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

img = cv.imread("1.png")
img2 = img.copy()
template = cv.imread('temp.jpg')
# All the 6 methods for comparison in a list
methods = ['cv.TM_CCOEFF', 'cv.TM_CCOEFF_NORMED', 'cv.TM_CCORR',
            'cv.TM_CCORR_NORMED', 'cv.TM_SQDIFF', 'cv.TM_SQDIFF_NORMED']
for meth in methods:
    img = img2.copy()
    method = eval(meth)
    # Apply template Matching
    res = cv.matchTemplate(img,template,method)
    cv.imwrite("{}.jpg".format(meth),res)
    min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    plt.subplot(121),plt.imshow(res,cmap = 'gray')
    plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
    plt.subplot(122),plt.imshow(img,cmap = 'gray')
    plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
    plt.suptitle(meth)
    plt.show()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2020-08-01
@tumbler

res in your case is a single-channel picture containing the results of comparing the image with a sample located at the corresponding coordinates. The point containing the global extremum (depending on the method) is located by the template coordinates.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question