gpt4 book ai didi

python - OpenCV 检测水果上的划痕

转载 作者:太空狗 更新时间:2023-10-30 01:18:28 24 4
gpt4 key购买 nike

我正在用 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,255,0), 12)
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,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 channel 中的划痕,因此之前建议的灰度方法会很困难。但是在色相平面上划痕非常明显。 The scratch is quite noticeable in the hue plane

您可以使用边缘检测器来查找色调 channel 中的瑕疵。这里我使用了不同的高斯检测器(尺寸为 20 和 4)。

DOG

关于python - OpenCV 检测水果上的划痕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753017/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com