[PYTHON] I made a program that solves the spot the difference in seconds

Fujitsu Systems Web Technology Advent Calendar 2019 Posted on the 16th day. The content of the article is the personal opinion, and the content of the writing is the responsibility of the author himself. The organization to which you belong does not matter.

Introduction

This time on the 16th day, I'm familiar with Saizeriya's spot the difference because I've never cleared it on my own

** A program that can take the difference between two images and display the difference in an easy-to-understand manner **

I would like to write about how to make

Rough image diagram

スクリーンショット 2019-12-13 21.04.06.png スクリーンショット 2019-12-13 21.04.16.png

Implementation environment

Advance preparation

(There is no guarantee, but if you can use python and various libraries, it will work normally, so please just take a look.)

(Detailed installation method is'[anaconda navigator jupyter installation](https://www.google.com/search?q=anaconda+navigator+jupyter+%E3%82%A4%E3%83%B3%E3%82%B9 % E3% 83% 88% E3% 83% BC% E3% 83% AB)'I think that various things will come out.)

Source commentary

Now, here is the actual content of the source.

There are three main parts

It feels like a mouse

import or variable setting


import cv2, matplotlib
import numpy as np
from IPython.display import Image, display_png

#Original image 1
img_01 = "sample1.png "
#Original image 2
img_02 = "sample2.png "
#Please enter the name you want to give to the output image
outImg = "output.png "

Create a difference mask image


#Read the original image 1 in color
img_src01 = cv2.imread(img_01, 1)
 
#Read original image 2 in color
img_src02 = cv2.imread(img_02, 1)
 
#Mixed normal distribution(Gaussian Mixture)It seems to be a foreground / background area division algorithm based on.
bg = cv2.bgsegm.createBackgroundSubtractorMOG()
 
#Generate mask image
mask = bg.apply(img_src01)
mask = bg.apply(img_src02)

#Output mask image
cv2.imwrite("output/machigai_mask.png ", mask)

print("Original image 1")
display_png(Image(img_01))
print("Original image 2")
display_png(Image(img_02))
print("Mask image")
display_png(Image("output/machigai_mask.png "))

The output result here is as follows. スクリーンショット 2019-12-13 22.21.20.png

Put color on the mask part of the mask image


mask_img = cv2.imread("output/machigai_mask.png ", 1)

#Replace the white part of the image with red
red = [240, 20, 20]
white = [255, 255, 255]
mask_img[np.where((mask_img == white).all(axis=2))] = red

# BGR to RGB
mask_img = cv2.cvtColor(mask_img, cv2.COLOR_BGR2RGB)

#Save the result.
cv2.imwrite("output/color_mask_img.png ", mask_img)

color_mask_img = cv2.imread("output/color_mask_img.png ", 1)
display_png(Image("output/color_mask_img.png "))

The output result here is as follows. スクリーンショット 2019-12-14 0.05.36.png

Change the transparency of the mask image and combine

If you can do this at the end, it will be completed.


# src1 =Original image 1, src2=Mask image, alpha(beta) =It seems good to adjust it at a ratio of 1 in total, gamma=It felt good when it was 0
diff_image = cv2.addWeighted(src1=img_src01,alpha=0.3,src2=color_mask_img,beta=0.7,gamma=0)
cv2.imwrite(outImg, diff_image)

display_png(Image(outImg))

Yes it's done スクリーンショット 2019-12-14 0.11.25.png

I tried it with an image that looks like a spot the difference

For this reason, I searched for mistakes and made it. First of all, please try it by yourself (there are 6 mistakes)

machigai_1_2.png

Did you find the mistake?

Next, let's take a look at the results of the fully automatic ~~ egg breaking machine ~~ solved by the spot the difference machine.

it's here diff_image (1).png

How is it? It's a great correct answer rate.

However, the detection accuracy of ** that the teacher's wig has changed to watermelon ** is not very good.

About background subtraction method

In the first place, this background subtraction method is used. This is a method for detecting moving objects on a moving image.

in short

With a fixed point camera ** Ah, people passed by ** It ’s a way to judge something like

There are two major problems when using this background subtraction method for finding mistakes.

So, if you try to make something that can be used for general purposes,

I feel like I have to make such software ()

Finally

Let's do our best to make such software next time (white eyes)

reference

-Background subtraction method -OpenCV basic knowledge -Image composition / transparency -Image fill

Recommended Posts

I made a program that solves the spot the difference in seconds
I made a program to solve (hint) Saizeriya's spot the difference
I made a program that automatically calculates the zodiac with tkinter
I made a Caesar cryptographic program in Python.
I made a program to check the size of a file in Python
I made a prime number generation program in Python
I made a prime number generation program in Python 2
I tried "a program that removes duplicate statements in Python"
I wrote a script that splits the image in two
I made a Python program for Raspberry Pi that operates Omron's environmental sensor in the mode with data storage
I made a program to collect images in tweets that I liked on twitter with Python
I made a prime number table output program in various languages
I made a slack bot that notifies me of the temperature
I made a command to display a colorful calendar in the terminal
I made a program in Python that changes the 1-minute data of FX to an arbitrary time frame (1 hour frame, etc.)
[Python] A program that rounds the score
I made a calendar that automatically updates the distribution schedule of Vtuber
I made a web application in Python that converts Markdown to HTML
I made a Discord bot in Python that translates when it reacts
I made a simple timer that can be started from the terminal
I made a library konoha that switches the tokenizer to a nice feeling
[IOS] I made a widget that displays Qiita trends in Pythonista3. [Python]
I made a mistake in fetching the hierarchy with MultiIndex of pandas
A memo that I wrote a quicksort in Python
A program that removes duplicate statements in Python
A program that searches for the same image
A shell program that displays the Fibonacci sequence
I made a LINE BOT that returns a terrorist image using the Flickr API
[Beginner] What happens if I write a program that runs in php in Python?
I made an appdo command to execute a command in the context of the app
I wrote a PyPI module that extends the parameter style in Python's sqlite3 module
I made a familiar function that can be used in statistics with Python
[Python] A program that calculates the difference between the total numbers on the diagonal line.
In Python, I made a LINE Bot that sends pollen information from location information.
I made a program to look up words on the window (previous development)
I made a github action that notifies Slack of the visual regression test
I made a program in Python that reads CSV data of FX and creates a large amount of chart images
I made a twitter app that decodes the characters of Pricone with heroku (failure)
[Python] A program that counts the number of valleys
〇✕ I made a game
[Python] A program that finds the shortest number of steps in a game that crosses clouds
I made a VM that runs OpenCV for Python
I made a script to put a snippet in README.md
A shell program that becomes aho in multiples of 3
I made a demo that lets the model learned in the Tensorflow mnist tutorial distinguish the handwritten numbers written on the canvas.
A memo that I touched the Datastore with python
I made a command to markdown the table clipboard
A note that runs an external program in Python and parses the resulting line
[Python / C] I made a device that wirelessly scrolls the screen of a PC remotely.
[Python] A program that compares the positions of kangaroos.
I made a calendar that automatically updates the distribution schedule of Vtuber (Google Calendar edition)
I made a class to get the analysis result by MeCab in ndarray with python
A program that automatically resizes the iOS app icon to the required image size in Python
[Atcoder] [C ++] I made a test automation tool that can be used during the contest
A story that stumbled when I made a chatbot with Transformer
A Python program in "A book that gently teaches difficult programming"
I made a simple typing game with tkinter in Python
I made a LINE BOT that returns parrots with Go
A general-purpose program that formats Linux command strings in python
I tried the super-resolution algorithm "PULSE" in a Windows environment
Mezzanine introduction memo that I got stuck in the flow