如何用给定的一组正确的、错误的样本图像来检测给定的图像是否正确

2024-04-24 06:24:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个图像集,一个包括5个正确的图像和其他集包括10个故障图像,故障图像有2种类型

correct image

fault image type 1

fault image type 2

两种类型都有细微差别,图像正确。你知道吗

故障类型1具有不同的盒子形状,故障类型2具有比正确图像更多的暗像素。你知道吗

我想用Python和OpenCV创建一个模型来检测何时给出一个图像,它是正确的图像还是错误的图像。你知道吗

曾想过使用haar级联,但我没有信心,因为负面图像只是略有不同,积极的形象。你知道吗

解决这个问题哪种方法更好?(traincascade或任何其他?)你知道吗


Tags: 模型图像image类型type错误像素opencv
1条回答
网友
1楼 · 发布于 2024-04-24 06:24:26

我的建议是使用OpenCV和Nump,以简化问题。你知道吗

方法是:

  1. 将无故障图像(基准图像)与输入图像进行比较。你知道吗
  2. 查看检测到的轮廓的大小。你知道吗

首先,我们导入两个主要库:

import cv2
import numpy as np

其次,我们定义了一个名为find\ u faulture\ u type的函数:

def find_faulty_type(benchmark_img, input_img):
    _, benchmark_img = cv2.threshold(benchmark_img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
    _, input_img = cv2.threshold(input_img, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    temp_img = benchmark_img - input_img
    if np.sum(temp_img) == 0:
        print ("No Fault")
    else:
        _, cnts, _ = cv2.findContours(temp_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        area_threshold = 6
        mean_area = 0
        for cnt in cnts:
            mean_area += cv2.contourArea(cnt)
        mean_area /= len(cnts)
        if mean_area >= area_threshold:
            print ("Type 1 Fault")
        else :
            print ("Type 2 Fault")

        output = cv2.cvtColor(temp_img, cv2.COLOR_GRAY2BGR)
        cv2.drawContours(output, cnts, -1, (0, 0, 255), 3)
        cv2.imshow("type result", output)
        cv2.waitKey()

类型1故障的示例结果图像:

enter image description here

类型2故障的示例结果图像:

enter image description here

第三,我们加载不同的图像并测试它们的错误类型,方法类似于:

ok_img = cv2.imread("ok_img.png", 0)
type1_img = cv2.imread("type1_img.png", 0)
type2_img = cv2.imread("type2_img.png", 0)

find_faulty_type(benchmark_img=ok_img, input_img=ok_img)
find_faulty_type(benchmark_img=ok_img, input_img=type1_img)
find_faulty_type(benchmark_img=ok_img, input_img=type2_img)

尽情享受吧,干杯。你知道吗

相关问题 更多 >