[PYTHON] Finding mistakes in Saizeriya (Italian ingredients)

Introduction

I tried to find mistakes in Saizeriya's Italian ingredients using OpenCV.

Image cropping (manual)

Carefully cut out the two images so that they have the same pixel size, and save them in png format.

Library import

Import libraries such as OpenCV.

import os
import subprocess
from PIL import Image
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

Image size adjustment

Store the cut out image file in the dir1 folder. Next, set the image size to 300 * 300px and store it in the dir2 folder.

dir1 = 'png'
dir2 = 'png_resize'

files1 = os.listdir(dir1)
files1.sort()

for file in files1:
    
    if '.png' in file:   
        img0 = os.path.join(dir1, file)
        img0_img = Image.open(img0)
        img1_img = img0_img.resize((300,300)) 
        img1 = os.path.join(dir2, file) 
        img1_img.save(img1)
        print(file)
        
# s1.png
# s2.png

s1.pngs2.png

Image split

Divide the image vertically and horizontally by 300 to get the b value. If the number of divisions is a fraction of 300, it is OK, but if you set it to 300, which is Max, the accuracy of the difference image will be the highest.

dir2 = 'png_resize'
files2 = os.listdir(dir2)
files2.sort()

std_data2 =[]
image_data2 = []
grid =300 #Division number

for file in files2:
   
    if '.png'  in file:        
        img = cv2.imread('./png_resize/' + file)
        h, w, c = img.shape    

        v_split = grid
        h_split = grid
        _img = img[:h // v_split * v_split, :w // h_split * h_split]

        image_data =[]
        
        for h_img in np.vsplit(_img, v_split):
            for v_img in np.hsplit(h_img, h_split):  
                b, g, r = cv2.split(v_img)
                value = round(b.mean())/100
                image_data.append(value)
        
        image_data2.append(image_data)

Difference imaging

Find the difference and create an image.

image_array2 =  np.array(image_data2)
dif_array = image_array2[0] - image_array2[1] 
dif_image=dif_array.reshape(grid,grid)
plt.imshow(dif_image)    

diff.png

check the answer

There are some parts that are difficult to understand, but it seems that all of them have been detected. ansfig.pngdiffans.png ans.png

At the end

It's a simple method, but I think it can be used enough to find mistakes. I will try even more difficult tasks.

Recommended Posts

Finding mistakes in Saizeriya (Italian ingredients)