小编Dor*_*Dor的帖子

OpenCV检测水果上的划痕

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

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

这是我想要检测的缺陷:

在此输入图像描述

原始图片:

在此输入图像描述

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

在此输入图像描述

所以我找到了水果的轮廓.我怎样才能找到划痕?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, …
Run Code Online (Sandbox Code Playgroud)

python opencv

5
推荐指数
1
解决办法
1072
查看次数

标签 统计

opencv ×1

python ×1