加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – OpenCV检测水果上的划痕

发布时间:2020-12-16 22:27:30 所属栏目:Python 来源:网络整理
导读:对于Python中的一个小实验我正在做我想要找到水果的小划痕.划痕非常小,很难被人眼检测到. 我正在使用高分辨率相机进行该实验. 这是我想要检测的缺陷: 原始图片: 这是我的结果,只有很少的代码行: 所以我找到了水果的轮廓.我怎样才能找到划痕? RGB值与水果

对于Python中的一个小实验我正在做我想要找到水果的小划痕.划痕非常小,很难被人眼检测到.

我正在使用高分辨率相机进行该实验.

这是我想要检测的缺陷:

enter image description here

原始图片:

enter image description here

这是我的结果,只有很少的代码行:

enter image description here

所以我找到了水果的轮廓.我怎样才能找到划痕? RGB值与水果的其他部分类似.那么如何区分划痕和水果的一部分呢?

我的代码:

# Imports
import numpy as np
import cv2
import time

# Read Image & Convert
img = cv2.imread('IMG_0441.jpg')
result = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

# Filtering
lower = np.array([1,60,50])
upper = np.array([255,255,255])
result = cv2.inRange(result,lower,upper)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
result = cv2.dilate(result,kernel)

# Contours
im2,contours,hierarchy = cv2.findContours(result.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
result = cv2.cvtColor(result,cv2.COLOR_GRAY2BGR)
if len(contours) != 0:
    for (i,c) in enumerate(contours):
        area = cv2.contourArea(c)
        if area > 100000:
            print(area)
            cv2.drawContours(img,c,-1,(255,0),12)
            x,y,w,h = cv2.boundingRect(c)            
            cv2.rectangle(img,(x,y),(x+w,y+h),(0,12)

# Stack results
result = np.vstack((result,img))
resultOrig = result.copy()

# Save image to file before resizing
cv2.imwrite(str(time.time())+'_0_result.jpg',resultOrig)

# Resize
max_dimension = float(max(result.shape))
scale = 900/max_dimension
result = cv2.resize(result,None,fx=scale,fy=scale)

# Show results
cv2.imshow('res',result)

cv2.waitKey(0)
cv2.destroyAllWindows()
最佳答案
我将图像更改为HSL色彩空间.
我看不到L通道中的划痕,因此前面提到的灰度方法将很难.
但是在色调平面上划痕非常明显.

The scratch is quite noticeable in the hue plane

您可以使用边缘检测器找到色调通道中的瑕疵.在这里,我使用了高斯探测器的差异(尺寸为20和4).

DOG

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读