First, realize the function
Within the specified coordinate range is determined whether there is similarity of images is greater than n, and returns the coordinates.
Second, the basic idea
A = You need to find the picture
B = intercept of the current page in the specified range image
A position is determined using opencv B,
In this position A, taken with the same size image C in FIG.
Similarity contrast picture with the picture A, C
Third, to achieve snippets
1, install the required libraries
pip install opencv-python
pip install pywin32
2, the interception of specified coordinates Pictures
Parameter Description
filename: save filename
hwnd: Please find a way to get the window handle
pos: a position coordinate [x1, y1, x2, y2]. x1, y1 are the coordinates of the upper left corner, x2, y2 refers to the lower right corner coordinates.
This function can not return to the top level program screenshot.
def window_capture(filename,hwnd=0,pos=None): HWND = HWND # numbered window number 0 indicates that the current active window # acquired window handle the window according to the device context the DC (Divice the Context) hwndDC = win32gui.GetWindowDC (HWND) # acquisition window according mfcDC the DC mfcDC = win32ui.CreateDCFromHandle (hwndDC ) # mfcDC create compatible DC SaveDC = mfcDC.CreateCompatibleDC () # create bigmap ready to save the picture saveBitMap = win32ui.CreateBitmap () # get monitor information MoniterDev = win32api.EnumDisplayMonitors (None, None) IF POS == None: x1=0 y1=0 w = MoniterDev[0][2][2] h = MoniterDev[0][2][3] else: x1=pos[0] y1 = post [1 ] w=pos[2]-pos[0] H = POS [. 3] -POS [. 1 ] # Print W, image size H # # of open space bitmap saveBitMap.CreateCompatibleBitmap (mfcDC, MoniterDev [0] [2] [2], MoniterDev [0] [2] [3 ]) # height saveDC, the screenshot is saved to saveBitmap saveDC.SelectObject (saveBitMap) # taken image from the upper left corner (0,0) of length and width (w, h) of saveDC.BitBlt ((x1, y1), (w , H), mfcDC, (X1, Y1), win32con.SRCCOPY) saveBitMap.SaveBitmapFile (SaveDC, filename)
3, in the position A is determined using opencv B is
Parameter Description
target: cv2.imread ( " Picture B")
template: cv2.imread ( "Picture A")
DEF find_picture (target, Template): # obtained template image size Aspect theight, TWIDTH template.shape = [: 2 ] # performing template matching, the match is in the cv2.TM_SQDIFF_NORMED Result = cv2.matchTemplate (target, Template, CV2 .TM_SQDIFF_NORMED) # normalization process cv2.normalize (Result, Result, 0,. 1, cv2.NORM_MINMAX, -1 ) # maximum and minimum values match with an array (one-dimensional array as a vector, defined by Mat) of results and position min_val, MAX_VAL, min_loc, max_loc = cv2.minMaxLoc (result) # match value to a string # for cv2.TM_SQDIFF and cv2.TM_SQDIFF_NORMED methods and approaches 0 min_val the better the matching, the matching position taken min_loc # for other methods max_val tends to 1, the better the match, the matching position taken max_loc strmin_val =STR (MIN_VAL) # draw a bounding rectangle, the matching area marked out # min_loc: Rectangular point # (min_loc [0] + TWIDTH, min_loc [. 1] + theight): The width and height of rectangle # (0,0,225): a rectangular frame color; 2: a rectangular border width cv2.rectangle (target, min_loc, (min_loc [0] + TWIDTH, min_loc [. 1] + theight), (0,0,225), 2 ) # display the results, and the value is displayed in the title match the bar # cv2.imshow ( "a MatchResult ---- MatchingValue =" + strmin_val, target) # cv2.waitKey () # cv2.destroyAllWindows () X = min_loc [0] The = min_loc [1 ] return X,Y
4, the return of the designated location specified coordinates specified picture
# Target original picture # X, Y start coordinate # W, H returned width to length DEF get_pic_from_pic (X, Y, W, H, target): region = target[y:y+h,x:x+w] retrun region
5, compare two pictures of similarity
DEF compare_picture (frame ImageA, frame ImageB): # grayscale images Compare grayA = cv2.cvtColor (frame ImageA, cv2.COLOR_BGR2GRAY) grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY) (score, diff) = compare_ssim(grayA, grayB, full=True)return float(score)