[PYTHON] Finding the simplest mistakes with OpenCV

An acquaintance said that I needed a program to compare the differences between images, so I built it with Python and OpenCV. The point is to search for mistakes using OpenCV, but ** the condition is that there is no discrepancy between the images **, so it may not be suitable for practical use. If you need a more robust spot the difference, you may want to refer to this article. I think the implementation is about 10 times lighter than the method in this article ...

result

The following image is output.

image.png (The image is Wikipedia Quoted from)

Source code

import cv2
import matplotlib.pyplot as plt

img1 = cv2.imread('img1.png')
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2RGB)
img1 = img1/255
img2 = cv2.imread('img2.png')
img2 = cv2.cvtColor(img2,cv2.COLOR_BGR2RGB)
img2 = img2/255
dif = cv2.absdiff(img1,img2)
dif[dif>0] = 1

fig,axs = plt.subplots(2,2)
ar = axs.ravel()
ar[0].imshow(img1)
ar[1].imshow(img2)
ar[2].imshow(img1*dif*0.8 + img1*0.2)
ar[3].imshow(img2*dif*0.8 + img2*0.2)
plt.show()

The essential processing is only a few lines by OpenCV, but the preprocessing for displaying with matplotlib takes quite a few lines. The point is that you can get a difference image with cv2.absdiff, so I use it as a mask. If diff is left as it is, the difference is taken for each RGB channel, so it is masked by the operation dif [dif> 0] = 1.

The point that took more time than I expected is that the image data of OpenCV can take both ʻinttype andfloattype, which is usually convenient, but this time the mask did not work well. As a result, when the image is read, it is/ 255 and forcibly cast to float`.

Recommended Posts

Finding the simplest mistakes with OpenCV
Try blurring the image with opencv2
Try using the camera with Python's OpenCV
I tried "smoothing" the image with Python + OpenCV
I tried "differentiating" the image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
The easiest way to use OpenCV with python
Binarization with OpenCV / Python
Read the OpenCV documentation
Write letters in the card illustration with OpenCV python
Fill the background with a single color with OpenCV2 + Python
Rotate sprites with OpenCV
Data Augmentation with openCV
Visualize the appreciation status of art works with OpenCV
Easy TopView with OpenCV
Stumble with homebrew opencv3
Make thumbnails easily with opencv from the erotic video folder
I tried "gamma correction" of the image with Python + OpenCV
The story of displaying images with OpenCV or PIL (only)
Estimate the attitude of AR markers with Python + OpenCV + drone
Finding a solution to the N-Queen problem with a genetic algorithm (1)
Face recognition with Python's OpenCV
"Apple processing" with OpenCV3 + Python3
Insert the debugger with nose
The simplest AWS Lambda implementation
Image editing with python OpenCV
Camera capture with Python + OpenCV
Kill the process with sudo kill -9
[Python] Using OpenCV with Python (Basic)
Loop video loading with opencv
Real-time edge detection with OpenCV
Guess the password with klee
gethostbyaddr () communicates with the outside
Face detection with Python + OpenCV
scraping the Nikkei 225 with playwright-python
Check the code with flake8
Calibrate the model with PyCaret
Get image features with OpenCV
Call the API with python3.
Face recognition / cutting with OpenCV
Try OpenCV with Google Colaboratory
Cascade classifier creation with opencv
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Color extraction with Python + OpenCV solved the mystery of the green background
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
I want to check the position of my face with OpenCV!
The fastest way to get camera images regularly with python opencv