SoFunction
Updated on 2024-10-29

python opencv implementation of image defect detection (explaining histograms and the correlation coefficient comparison method)

First, the use of histograms for batch picture defect detection (simple method)

II. Steps (see end for complete code)

2.1 Grayscale conversion (separate grayscaling of the original image and the image to be tested for comparison)

Graying is useful because later histogram comparisons need to be correlated on a pixel 256 basis

img = ("")
# Original image grayscale conversion
gray = (img, cv2.COLOR_RGB2GRAY)

# Loop through the graphs to be detected, all grayed out
for i in range(1, 6):
 t1=((str(i)+".bmp"),cv2.COLOR_RGB2GRAY)

2.2 Histogram calculations (the results are actually two-dimensional graphs - presented as drawings)

Explanation of calcHist parameters

  • First parameter: must be a list [], even if there is only one image, image input image
  • channels:: the channels of the incoming image, if it is a grayscale image, it goes without saying that there is only one channel with a value of 0. If it is a color image (with 3 channels), then the value will be one of 0,1,2, corresponding to each channel of the BGR. This value must also be passed in with [].
  • mask: mask image. If the whole plot is counted, then none. Mainly, if you want to count the histogram of a part of the plot, you have to construct the corresponding inflammation mask to calculate it.
  • histSize: number of gray levels, requires parentheses, e.g. [256].
  • ranges:the range of pixel values, usually [0,256], some images if not 0-256, for example, you go back and forth to various transformations resulting in negative pixel values, very large, will need to be adjusted to be able to.

# Histogram computed as a function that responds to the distribution of gray values
    hist = ([gray], [0], None, [256], [0.0,255.0])

    h1 = ([t1], [0], None, [256], [0.0,255.0])

2.3 Comparison of correlations

(H1, H2, method)

Among them:

  • H1, H2 are the histograms of the images to be compared respectively
  • method - method of comparison
  • Comparison method (method)
  • Correlation Comparison (method=cv.HISTCMP_CORREL) The higher the value, the higher the correlation, with a maximum value of 1 and a minimum value of 0. ----------------------- It's certainly not very rigorous to use just one, but here's a demonstration, and it's almost as good to adjust the threshold higher (taking it to be greater than or equal to 0.9)
  • Cardinality Comparison (method=cv.HISTCMP_CHISQR The smaller the value, the higher the correlation, the maximum value has no upper bound, the minimum value 0
  • Bachmann Distance Comparison (method=cv.HISTCMP_BHATTACHARYYA) The smaller the value, the higher the correlation, with a maximum value of 1 and a minimum value of 0
  • # Correlation calculated using correlation coefficients
  •     result = (hist,h1,method=cv2.HISTCMP_CORREL)

2.4 Presentation of results (judgment thresholds)

Reference table for meaning of correlation coefficient

 im = (str(i) + ".bmp")

 draw = (im)
 fnt = (r'C:\Windows\Fonts\', 30)
 #This is regarded as = 0.9 as similar, i.e., qualified
 if result >=0.9:
  ((5, 10), u'Qualified', fill='red', font=fnt)
 else:
  ((5, 10), u'Failed', fill='red', font=fnt)
 ("result" +str(i) + ".png")

III. Complete code

# -*- coding: UTF-8 -*-
import cv2
from PIL import Image, ImageDraw, ImageFont

img = ("")
# Original image grayscale conversion
gray = (img, cv2.COLOR_RGB2GRAY)

for i in range(1, 6):
 t1=((str(i)+".bmp"),cv2.COLOR_RGB2GRAY)

 # Histogram calculated as a function that responds to the distribution of gray values
 hist = ([gray], [0], None, [256], [0.0,255.0])

 h1 = ([t1], [0], None, [256], [0.0,255.0])
 # Correlation calculated using correlation coefficients
 result = (hist,h1,method=cv2.HISTCMP_CORREL)
 im = (str(i) + ".bmp")

 draw = (im)
 fnt = (r'C:\Windows\Fonts\', 30)
 #This is regarded as = 0.9 as similar, i.e., qualified
 if result >=0.9:
  ((5, 10), u'Qualified', fill='red', font=fnt)
 else:
  ((5, 10), u'Failed', fill='red', font=fnt)
 ("result" +str(i) + ".png")

Refer to the blog post:

CompareHist function in Python-Opencv for histogram comparison for comparing images:

https:///article/

OpenCV-Python Histogram-1: Finding, Plotting, and Analyzing | XXVI:/s?id=1655424859576397139&wfr=spider&for=pc
I hope this helps everyone understand histograms and the role of comparative functions!!!!

summarize

To this point, this article on python opencv image defect detection (explain the histogram and correlation coefficient comparison method) of the article on this, more related python opencv defect detection content, please search for my previous posts or continue to browse the following related articles I hope you will support me more in the future!