[PYTHON] I made a program to solve (hint) Saizeriya's spot the difference

Hello. This is the first post. Waiting time when I went to Saizeriya the other day About 20 minutes I searched for mistakes in Saizeriya and could not find it ... (too much ...) So, I thought that it could be solved by image processing, so I tried it. It was also a good study for OpenCV.

Thing you want to do

Image data officially released by Saizeriya using the OpenCV library (https://www.saizeriya.co.jp/entertainment/) I want to process and automate the search for mistakes! !! !! What you actually do is

--Process the image (remove margins, split in half) --Calculate image differences --Display difference information on the original image

It's like that. I gave the code to GitHub.

Execution environment

macOS Mojave 10.14.4 Python 3.6.7 OpenCV 3.4.1

Process the image

When you download it, you will notice that "the images to be compared are stuck together" + "there is a mysterious margin". diff2.png

First of all, I would like to delete the margins. The flow of deleting blanks is --Make a grayscale image ―― Binarize --Extract the contour --Get and cut x and y from the contours that have the smallest and largest coordinates.

[https://qiita.com/trami/items/e25eb70a59a51ae4f7ba# How to remove margins](https://qiita.com/trami/items/e25eb70a59a51ae4f7ba#%E3%81%A9%E3%81 % AE% E3% 82% 88% E3% 81% 86% E3% 81% AB% E3% 81% 97% E3% 81% A6% E4% BD% 99% E7% 99% BD% E5% 89% 8A We refer to the article% E9% 99% A4% E3% 82% 92% E8% A1% 8C% E3% 81% 86% E3% 81% AE% E3% 81% 8B).

The grayscale function in OpenCV

gray.py


gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Grayscale

The binarization function

binary.py


r, binary = cv2.threshold(gray, 0, 255,cv2.THRESH_OTSU)  #Binarization

The image cropping function

cut_img.py


img = img[y1:y2,x1:x2] #(x1,y1)From(x2,y2)Cut out

It's like that. If you combine these, the code so far will be

saizeriya.py


import cv2
import numpy as np

img = cv2.imread('diff.png') #Loading images
height, width, d = img.shape #Height, width, depth
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #Grayscale
r, binary = cv2.threshold(gray, 0, 255,cv2.THRESH_OTSU)  #Binar at threshold 200
contours = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0]

#Extract contours, remove margins
x1 = [] #Minimum x coordinate
y1 = [] #Minimum y coordinate
x2 = [] #Maximum x coordinate
y2 = [] #Maximum y coordinate
for i in range(1, len(contours)):
    ret = cv2.boundingRect(contours[i])
    x1.append(ret[0])
    y1.append(ret[1])
    x2.append(ret[0] + ret[2])
    y2.append(ret[1] + ret[3])
x1_min = min(x1)
y1_min = min(y1)
x2_max = max(x2)
y2_max = max(y2)
img = img[0:600, x1_min:x2_max] #Cut off the margin

The execution result is as follows. You can see that it is cut well. スクリーンショット 2020-03-29 19.03.18.png

Then cut the image in half.

saizeriya.py


height, width,d= img.shape #Height, width, depth(The size of the cut out)
midle = int(width/2)

img1 = img[0:height,0:midle-3]
img2 = img[0:height,midle-8:width-11] #Actually, the width is a little different... 

What was ridiculous here was that the sharp edges of the two images were actually slightly different. After all, I adjusted the part to be cut manually.

Calculate image differences

Then calculate the difference between the images. The difference between the images can be subtracted between the numpy arrays, but when you actually execute it, the difference is difficult to understand (a slight difference in color and a slight deviation in location will be displayed as a difference), so this time OpenCV I used the ** absdiff ** function of.

The ** absdiff ** function can find the absolute value of the difference between two images.

saizeriya.py


#Show diff
result = np.copy(img1) #Array to store the resulting image
add = np.copy(img1) #Array to store the resulting image
#result = img1-img2 #Calculation of difference
result = cv2.absdiff(img1, img2) #Calculation of difference(absdiff)

Execution result image.png

For reference, it is the result when it is executed by subtraction between arrays. image.png

Display the difference information on the original image

With this, I don't know where the difference is, so I would like to combine it with the original image. The original gray image + difference image (color) is combined.

The function to use is the ** add ** function. Also, use the ** cvtCOLOR ** function to convert the original color image.

Color 3D array → Gray 2D array → Gray 3D array

Process with.

saizeriya.py


img3 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) #Grayscale
img3 = cv2.cvtColor(img3,cv2.COLOR_GRAY2BGR) #Make a color image with grayscale
print(img3.shape)
print(result.shape)
add = cv2.add(img3,result) #Combine images

#Image display
cv2.imshow('all',img)
cv2.imshow('image',add)
cv2.imshow('result',result)
cv2.waitKey(0) #Wait until some key is pressed
cv2.destroyAllWindows() #Destroy all windows

Execution result image.png

Now you can see what's different.

I can only find 8! !! !! !!

Please tell me two more ...

It was just an auxiliary tool.

I found 9 spot the difference in December

image.png

Recommended Posts

I made a program to solve (hint) Saizeriya's spot the difference
I made a program that solves the spot the difference in seconds
I made a program to check the size of a file in Python
Former image processing engineer tried to solve Saizeriya's spot the difference with OpenCV
I made a command to markdown the table clipboard
I made a program to look up words on the window (previous development)
Write a program to solve the 4x4x4 Rubik's Cube! 1. Overview
I made a function to check the model of DCGAN
Let's write a program to solve the 4x4x4 Rubik's Cube! 2. Algorithm
Let's write a program to solve the 4x4x4 Rubik's Cube! 3. Implementation
I wanted to solve the ABC164 A ~ D problem with Python
I made a command to display a colorful calendar in the terminal
I made a program that automatically calculates the zodiac with tkinter
I made a payroll program in Python!
I made a script to display emoji
Let's write a program to solve the Rubik's Cube (Part 2: IDA * Search)
[Django] I made a field to enter the date with 4 digit numbers
I made a kitchen timer to be displayed on the status bar!
I made a program to notify you by LINE when switches arrive
I made a program to input what I ate and display calories and sugar
I made a library konoha that switches the tokenizer to a nice feeling
I made a function to see the movement of a two-dimensional array (Python)
I made a tool to compile Hy natively
I made a tool to get new articles
I made a Caesar cryptographic program in Python.
I made a tool to estimate the execution time of cron (+ PyPI debut)
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I made an appdo command to execute a command in the context of the app
I made a tool to generate Markdown from the exported Scrapbox JSON file
I made a tool to automatically back up the metadata of the Salesforce organization
I made a script to record the active window using win32gui of Python
I made a prime number generation program in Python
〇✕ I made a game
I made a library to separate Japanese sentences nicely
I made a program to convert images into ASCII art with Python and OpenCV
[Python] I made a system to introduce "recipes I really want" from the recipe site!
I tried to solve the soma cube with python
I made a script to put a snippet in README.md
I made a Python module to translate comment outs
I made a code to convert illustration2vec to keras model
I made a small donation to the non-profit organization "Open Source Robot Foundation" OSRF
I made a prime number generation program in Python 2
I tried to solve the problem with Python Vol.1
I made a python library to do rolling rank
I made a command to wait for Django to start until the DB is ready
How to test the current time with Go (I made a very thin library)
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
I'm always impatient when ordering a cafe, so I made a React app to solve it.
I made a tool to get the answer links of OpenAI Gym all at once
I made a class to get the analysis result by MeCab in ndarray with python
I made a program to collect images in tweets that I liked on twitter with Python
I made a function to crop the image of python openCV, so please use it.
I made a tool to automatically generate a simple ER diagram from the CREATE TABLE statement
I wanted to solve the Panasonic Programming Contest 2020 with Python
I scraped the Organization member team and made a ranking
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
I made a box to rest before Pepper gets tired
Write a python program to find the editing distance [python] [Levenshtein distance]
I made a command to generate a table comment in Django
I made a tool to create a word cloud from wikipedia