脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

2021-10-19 08:59Dream丶Killer Python

这篇文章主要介绍了Python opencv缺陷检测的实现及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

题目描述

利用opencv或其他工具编写程序实现缺陷检测。

实现过程

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-
'''
作者 : 丁毅
开发时间 : 2021/4/21 15:30
'''
import cv2
import numpy as np
from pil import image, imagedraw, imagefont
import matplotlib.pyplot as plt
 
 
#用于给图片添加中文字符的函数
def cv2imgaddtext(img, text, left, top, textcolor=(0, 255, 0), textsize=20):
    # 判断是否opencv图片类型
    if (isinstance(img, np.ndarray)):
        img = image.fromarray(cv2.cvtcolor(img, cv2.color_bgr2rgb))
    # 创建一个可以在给定图像上绘图的对象
    draw = imagedraw.draw(img)
    # 字体的格式
    fontstyle=imagefont.truetype("font/simsun.ttc",textsize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textcolor, font=fontstyle)
    # 转换回opencv格式
    return cv2.cvtcolor(np.asarray(img), cv2.color_rgb2bgr)
 
# plt绘图显示中文
plt.rcparams['font.family'] = 'simhei'
img0 = cv2.imread(r"c:\users\pc\desktop\0.bmp")
cv2.imshow('img', img0)
# 彩色图转灰度图
img1 = cv2.cvtcolor(img0, cv2.color_bgr2gray)
 
# 缺陷检测
for i in range(1, 6):
    defect_img0 = cv2.imread(r"c:\users\pc\desktop\%d.bmp"%i)
    # 获取灰度图像
    defect_img1 = cv2.cvtcolor(defect_img0, cv2.color_bgr2gray)
    # 获取原图像的灰度直方图
    hist0 = cv2.calchist([img1], [0], none, [256], [0.0, 255.0])
    # 获取待检测图像的灰度直方图
    hist1 = cv2.calchist([defect_img1], [0], none, [256], [0.0, 255.0])
    # 为图像添加标题
    plt.title("原图与待检测img%d对比"%i)
    # 添加图例
    plt.plot(hist0, label='原图')
    plt.plot(hist1, label='待检测img%d'%i)
    # 相似度比较
    rst = cv2.comparehist(hist0, hist1, method=cv2.histcmp_correl)
    # res >= 0.95即认为合格
    cv2.imshow(str(i) + ".img", cv2imgaddtext(defect_img0, "合格" if rst >= 0.95 else "不合格", 20, 20, (255, 0, 0), 25))
    # 设置x轴的数值范围
    plt.xlim([0, 256])
    plt.legend(loc='upper left')
    plt.show()
cv2.waitkey(0)

运行结果

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

Python opencv缺陷检测的实现及问题解决

问题及解决方法

1.获取原图的直方图

参考链接
方式:
cv2.calchist(images, channels, mask, histsize, ranges[, hist[, accumulate ]])

images:输入的图像channels:选择图像的通道mask:是一个大小和image一样的np数组,其中把需要处理的部分指定为1,不需要处理的部分指定为0,一般设置为none,表示处理整幅图像。histsize:使用多少个bin(柱子),一般为256ranges:像素值的范围,一般为[0,255]表示0~255

该函数结果返回一个二维数组,该数组反应画面中亮度的分布和比例。

2.比较两个直方图的相似度
参考链接
方式:
cv2.comparehist(h1, h2, method)

h1:第一个直方图数组h2:第二个直方图数组(与第一个纬度相同)method:所使用的方式

该函数返回一个[0,1]的相似度值,值越接近一就表名相似度越高。

3.相似度参数微调
由于comparehist函数返回一个[0,1]的值,需要自己调整一个阈值来选取哪些合格,经过调整后,发现阈值取[0.90, 0.95]能够正确选取与实验的结果,代码中取的是0.95,即待检测图与原图之间的相似度如果小于0.95则不合格。

4.通过plot显示原图与待检测图的关系折线
参考链接
通过calchist函数返回的hist数组值,运用matplotlib绘制原图和待检测图之间的关系折线图。对比两个曲线的差异。

到此这篇关于python opencv缺陷检测的实现的文章就介绍到这了,更多相关opencv缺陷检测内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_43965708/article/details/

延伸 · 阅读

精彩推荐