python-opencv 进行饼乾烤焦检测

这组程序是用来判断饼乾的颜色是否烤焦,不只可以应用在食品业上,在工业上也可以判断金属在热处理後的颜色等等。撷取图片的部分像下面一样,把饼乾撷取出来尽量不要有背景对影像处理比较好,使用HSV的原因是在辨识烤焦时HSV是一个比较powreful的工具,所以使用HSV影像来做直方图比对。(最後下面形态学做比较多次的原因是因为原本这组程序是用在形状比较复杂的饼乾上,可是因为有签保密协定所以不能将图片公布)

https://ithelp.ithome.com.tw/upload/images/20220403/20147977ceyqBB8PnB.jpg
https://ithelp.ithome.com.tw/upload/images/20220403/20147977JhVOeJluLu.png

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
#这个专案是先将图中的饼乾撷取,所以前面做一些形态学把连通区找出...
#将图片找出後再将其转换成HSV进行颜色比对来判断饼乾是否烤焦
#using morphology to find the cookies
#and convert to HSV image to judging cookies is normal or overcook


def eggroll(im,im1):
    global date
    date=cv.absdiff(im, im1)
    return date

def binary(date):
    global binaryim
    ret,binaryim=cv.threshold(date, 10, 255, cv.THRESH_BINARY)
    return binaryim

def close(binaryim):
    global closing
    kernel=np.ones((15, 15),np.uint8)
    clim0=cv.morphologyEx(binaryim,cv.MORPH_CLOSE,kernel)
    closing=cv.morphologyEx(clim0,cv.MORPH_CLOSE,kernel)
    return closing

def open(closing):
    global opening
    kernel=np.ones((15, 15),np.uint8)
    op=cv.morphologyEx(closing,cv.MORPH_OPEN,kernel)
    opening=cv.morphologyEx(op,cv.MORPH_OPEN,kernel)
    return opening

def findedge(img1,img0):
    global conim
    contours,conim=cv.findContours(img1, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    c = max(contours, key = cv.contourArea)
    x,y,w,h = cv.boundingRect(c)
    cv.rectangle(img1,(x, y), (x+w, y+h), (0, 255, 0), 2)
    crop_img = im0[y:y+h, x:x+w]
    return crop_img

def findedge2(img3,img4):
    global conim2
    contours1,conim1=cv.findContours(img3, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
    c1 = max(contours1, key = cv.contourArea)
    x1,y1,w1,h1 = cv.boundingRect(c1)
    cv.rectangle(im4,(x1, y1), (x1+w1, y1+h1), (0, 255, 0), 2)
    crop_img2 = im4[y1:y1+h1, x1:x1+w1]
    return crop_img2

def hsv(im5,im6):
    img2 = cv.cvtColor(im5, cv.COLOR_BGR2HSV)
    img6 = cv.cvtColor(im6, cv.COLOR_BGR2HSV)
    h, s, v = img2[:, :, 0], img2[:, :, 1], img2[:, :, 2]
    hist_h = cv.calcHist([h], [0], None, [256], [0, 256])
    hist_s = cv.calcHist([s], [0], None, [256], [0, 256])
    hist_v = cv.calcHist([v], [0], None, [256], [0, 256])
    h1, s1, v1 = img6[:, :, 0], img6[:, :, 1], img6[:, :, 2]
    hist_h1 = cv.calcHist([h1], [0], None, [256], [0, 256])
    hist_s1 = cv.calcHist([s1], [0], None, [256], [0, 256])
    hist_v1 = cv.calcHist([v1], [0], None, [256], [0, 256])
    match1 = cv.compareHist(hist_h, hist_h1, cv.HISTCMP_BHATTACHARYYA)
    match2 = cv.compareHist(hist_h, hist_h1, cv.HISTCMP_CORREL)
    match3 = cv.compareHist(hist_v, hist_v1, cv.HISTCMP_CHISQR)
    return match2

def judgment(match2):
    date=match2
    if date>=0.95:
        print('excellent')
    elif date>=0.9:
        print('great')
    elif date>=0.85:
        print('normal')
    else:
        print('fail')
        return

im0=cv.imread('dark2.jpg')
im=cv.imread('dark2.jpg',0)
im1=cv.imread('bg.jpg',0)
im3=cv.imread('light3.jpg',0)
im4=cv.imread('light3.jpg')
v=eggroll(im, im1)
v1=binary(v)
v2=close(v1)
v3=open(v2)
v4=findedge(v3, im)
v5=eggroll(im3,im1)
v6=binary(v5)
v7=close(v6)
v8=open(v7)
v9=findedge2(v8,im4)
v10=hsv(v4,v9)
print('correlation',v10)
v11=judgment(v10)
print('judgment',v11)
cv.imshow("cropd",v9)
cv.imshow("cropped", v4)
cv.waitKey(0)

<<:  RegExp vs includes vs match

>>:  【C#】Abstract Class vs Interface

爬虫怎麽爬 从零开始的爬虫自学 DAY14 python条件运算式

前言 各位早安,书接上回我们练习了条件判断语法 if else elif 的用法,今天我们要来介绍它...

Day26 Java String(Ⅰ)

String类提供了1.equals() 2. equalsIgnoreCase()方法来比较两个字...

Day2:安装Azure AD Connector同步至M365遇到TLS 1.2卡卡要怎麽办

当我们正准备将企业AD帐号透过传送门(Azure AD Connector)运送到Microsoft...

纯JS实作照片上传、下载与预览

更多会员限定文章可以到patreon观看 完整code可以到以下gist Client端HTML &...

Day-21 Excel位址精选题目练习

经过了一晚的思考,我认为昨天讲的还不够清楚,因此今天我找了三题相关题目,今天来为大家做更深入的讲解,...