Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用阈值法检测苹果_Python_Opencv_Image Processing_Colors - Fatal编程技术网

Python 用阈值法检测苹果

Python 用阈值法检测苹果,python,opencv,image-processing,colors,Python,Opencv,Image Processing,Colors,我想用阈值法检测苹果。为此,我将图像转换为HSV,然后计算InRange()函数的下限和上限。从中取出一个二进制掩码。因为苹果会相互接触,所以我使用分水岭算法来分离它们 输入图像如下所示: 在InRange()操作和腐蚀后,灰色图像如下所示: 应用分水岭算法,输出如下所示: 问题是左下角的苹果被错误地检测到了。只有两个苹果,显示了三个轮廓,而且其中一个的圆太大了。有什么帮助吗 这是我的密码 import cv2 import numpy as np import imutils from

我想用阈值法检测苹果。为此,我将图像转换为HSV,然后计算
InRange()
函数的下限和上限。从中取出一个二进制掩码。因为苹果会相互接触,所以我使用分水岭算法来分离它们

输入图像如下所示:

InRange()
操作和腐蚀后,灰色图像如下所示:

应用分水岭算法,输出如下所示:

问题是左下角的苹果被错误地检测到了。只有两个苹果,显示了三个轮廓,而且其中一个的圆太大了。有什么帮助吗

这是我的密码

import cv2
import numpy as np
import imutils
from scipy import ndimage
from skimage.feature import peak_local_max
from skimage.morphology import watershed

img = cv2.imread('4.jpg')
img = imutils.resize(img, width=640)
# img = cv2.pyrMeanShiftFiltering(img, 21, 51)
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)

lower_1 = np.array([0,50,20])
upper_1 = np.array([80,255,255])
mask1 = cv2.inRange(hsv, lower_1, upper_1)

lower_2 = np.array([160,50,20])
upper_2 = np.array([179,255,255])
mask2 = cv2.inRange(hsv, lower_2, upper_2)

gray = mask1+mask2
kernel = np.ones((7,7),np.uint8)
gray = cv2.erode(gray,kernel,iterations = 1)
# gray = cv2.morphologyEx(gray, cv2.MORPH_OPEN, kernel)

thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]


D = ndimage.distance_transform_edt(thresh)
localMax = peak_local_max(D, indices=False, min_distance=20,
    labels=thresh)

markers = ndimage.label(localMax, structure=np.ones((3, 3)))[0]
labels = watershed(-D, markers, mask=thresh)
print("[INFO] {} unique segments found".format(len(np.unique(labels)) - 1))

for label in np.unique(labels):
    if label == 0:
        continue

    mask = np.zeros(gray.shape, dtype="uint8")
    mask[labels == label] = 255

    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
        cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    c = max(cnts, key=cv2.contourArea)

    ((x, y), r) = cv2.minEnclosingCircle(c)
    if r > 25 and r < 55:
        cv2.circle(img, (int(x), int(y)), int(r), (0, 255, 0), 2)
        cv2.putText(img, "{}".format(round(r)), (int(x) - 10, int(y)),
            cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 255), 2)

cv2.imshow('thresh', thresh)
cv2.imshow('gray', gray)
cv2.imshow('img', img)

cv2.waitKey(0)
cv2.destroyAllWindows()
导入cv2
将numpy作为np导入
导入imutils
从scipy导入ndimage
从skimage.feature导入峰值\本地\最大值
从skimage.com导入分水岭
img=cv2.imread('4.jpg')
img=imutils.resize(img,宽度=640)
#img=cv2。磁均移滤波(img,21,51)
hsv=cv2.CVT颜色(img,cv2.COLOR\U BGR2HSV)
下_1=np.数组([0,50,20])
上_1=np.数组([80255255])
mask1=cv2.inRange(hsv,下部1,上部1)
下_2=np.数组([160,50,20])
上_2=np.数组([179255255])
mask2=cv2.inRange(hsv、下_2、上_2)
灰色=mask1+mask2
内核=np.ones((7,7),np.uint8)
gray=cv2.腐蚀(gray,内核,迭代次数=1)
#gray=cv2.morphologyEx(gray,cv2.MORPH_OPEN,内核)
thresh=cv2.阈值(灰色,0,255,cv2.thresh_二进制| cv2.thresh_大津)[1]
D=nImage.distance\u transform\u edt(阈值)
本地最大值=峰值\u本地\u最大值(D,指数=假,最小距离=20,
标签=脱粒)
markers=ndimage.label(localMax,结构=np.ones((3,3))[0]
标签=分水岭(-D,标记,遮罩=阈值)
打印(“[INFO]{}找到唯一段”。格式(len(np.unique(labels))-1))
对于np.unique中的标签(标签):
如果标签==0:
持续
掩码=np.零(gray.shape,dtype=“uint8”)
掩码[标签==标签]=255
cnts=cv2.findContentours(mask.copy(),cv2.RETR_EXTERNAL,
cv2.链条(近似简单)
cnts=imutils.GRAP_轮廓(cnts)
c=最大值(CNT,键=cv2.轮廓面积)
((x,y,r)=cv2.闭合圆(c)
如果r>25且r<55:
圆(img,(int(x),int(y)),int(r),(0,255,0),2)
cv2.putText(img,“{}.”格式(round(r)),(int(x)-10,int(y)),
cv2.FONT_HERSHEY_SIMPLEX,0.6,(0,0,255),2)
cv2.imshow('thresh',thresh)
cv2.imshow(“灰色”,灰色)
cv2.imshow(“img”,img)
cv2.等待键(0)
cv2.destroyAllWindows()

尝试在
cv2.inRange()之后打开cv2.MORPH\u,以便更好地分割连接的blob