IG wins! Boss Wang is mad! If Boss Wang eats hot dogs like this, of course we have to find him in the crowd!

How to find King Sicong in 3 seconds when you see an art student’s work?



I looked for a long time, this picture is really magic, eyes are spent, wasted my precious working time. I have to admit, as an engineering student, I can’t find it in three seconds. But my computer can.

Even art students are out to make fun of Wang Sicong, how can engineering students sit still! Then I will take the advantage of work, to do an automatic find Wang Sicong implementation.

Now that I’m at work, I need to conduct my work in a professional manner.

Project objective: To find Wang Sicong eating hot dogs in a group of ducks

Project implementation: Python, OpenCV

Project results: found in 0.3 seconds

Project technical route:

  1. Wang Sicong, the size of a duck;
  2. In the original global search, matching the position of Wang Sicong;
  3. Draw a red box around Wang Sicong;
  4. Check computer speed;

Specific implementation of the project:

Image processing, as a very large class of computer algorithms, has a ready-made code base that is very suitable for doing so

1. Get Wang Sicong the same size as a duck — resize (OpenCV)

This part is mainly for data preprocessing, which is very professional. In fact, it is to intercept a duckling with the screenshot tool of the computer, whose size is about 36*36. Accordingly, we resize Wang Sicong to the same size as the duckling, and resampling is carried out by interpolating inter_CUBIC.

Wang sicong’s drawing is from the lovely art student.



#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" Created on Wed Nov 7 11:42:02 2018 @author: aaron """
import cv2

template = cv2.imread('wangsicong.png')
template = cv2.resize(template, (40, 40),
interpolation=cv2.INTER_CUBIC)Copy the code

2. Search globally in the original image to match the location of Wang Sicong — matchTemplate (OpencV)


OpenCV as a more versatile image processing library, can provide many image processing basis, such as edge monitoring function can be directly used to monitor the boundary of the image (OpenCV also provides canny operator, Sobel operator, etc.).

Here we use the matchTemplate algorithm, which helps the algorithm find a specific target in an image. The function takes four arguments,

  • The original Image
  • Monitoring target detect
  • Matching result graph result
  • Matching measure method
  • CV_TM_SQDIFF, square variance
  • CV_TM_SQDIFF_NORMED, the mean square deviation normalization
  • CV_TM_CCORR, correlation
  • CV_TM_CCORR_NORMED, normalization of relevance
  • CV_TM_CCOEFF, correlation coefficient
  • CV_TM_CCOEFF_NORMED, normalization of correlation coefficient

Therefore, the search is mostly pixel-level matches, with no scaling;

In our current task, The tone of Wang Sicong has not changed, so the difference between either method is not very big.

import numpy as np
from matplotlib import pyplot as plt
 
img = cv2.imread('image.jpg')
template = cv2.imread('detect.png')   

# Apply template Matching
res = cv2.matchTemplate(img,template,eval('cv2.TM_CCOEFF'))

# Get the size of template
w, h = template[:,:,0].shape[::- 1]Copy the code

3. Draw a red box around Wang Sicong — minMaxLoc (OpenCV);

The result of the matchTemplate function is a gray value graph, which shows the matching degree of each detect range in the image. The larger the gray value is, the higher the similarity degree is.

To draw the most likely position, we use OpenCVv’s minMaxLoc function to get the exact position of the border, and further use OpenCV’s Rectangle function to draw the box.

# Get anchor for templateMatch result
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

# draw rectangle
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
imgplt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.rectangle(imgplt,top_left, bottom_right, 255, 2)

# show image
plt.imshow(imgplt)
plt.title('Detected results'), plt.xticks([]), plt.yticks([])
plt.show()
Copy the code



As it turned out! Success!

4. Computer speed — timeit.

Python’s timeit module provides a count of code runtime, with a total of two times recorded during use of the module. The total amount of time the code took to run once before all the code was run and once after all the code was run.

Timeit time counts are listed here, but when running, you need to place all of the above code in the middle of the two time counts, in line 4 below.

import timeit
start = timeit.default_timer()

# All your code above

stop = timeit.default_timer()
print('Time: ', stop - start) 

Copy the code

I ran it an average of ten times, and the 2018 MacBook Air averaged 0.2 seconds in basic configuration!

Goal achieved!

PS, 👆 is to show the style of OpenCV. Therefore, a traditional pattern matching process is used. If we were to face the same problem in practice.

  1. Firstly, wang Sicong’s face color is different from that of a duck, and his face color is unique to him.
  2. You can use the dropper function to get the RGB of Wang Sicong’s face color;
  3. Search for and highlight parts of the image that have RGB of the face color;
  4. Find Wang Sicong!

OpenCV is a very important class library in computer vision, which can support both traditional computer vision processing and deep learning computer vision. His bottom is C++, the operation speed is very fast, but also provides Python call interface, really save a lot of things!

Thanks for watching!