使用圆形检测和颜色检测识别物体

时间:2018-10-11 10:40:16

标签: python opencv

我正在使用颜色检测(特别是紫色)和圆形检测来检测下图中的花粉对象(紫色圆形)。

enter image description here

然后,在检测到的对象中写字母“ P”。不幸的是,它没有按我预期的那样工作。

enter image description here

如果更改半径,可以对其进行修复,但这不是一个好主意,因为我仍然需要处理许多不同半径的相似图像。我认为主要要点是如何知道此图像中紫色的确切范围。通常,我想知道如何获取图像中任意颜色的范围。有人给了我一个示例代码,但是效果不好。

这是我的程序。

import cv2
import numpy as np


# In[2]:

path = "./sample.JPG"
font = cv2.FONT_HERSHEY_COMPLEX


# In[3]:

def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image

    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)

    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)

    # return the resized image
    return resized


# In[4]:

iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,width=960)


# In[5]:

hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
hsv = cv2.medianBlur(hsv,5)
#mask = cv2.inRange(hsv, (120, 180, 50), (160, 255, 255))
mask = cv2.inRange(hsv, (105, 100,50), (160, 255, 255))

#mask = cv2.inRange(hsv, (126, 142, 57), (145, 255, 255))
#cv2.GaussianBlur(cimg, (9,9),3)
#cimg = cv2.medianBlur(cimg,5)
output = cv2.bitwise_and(hsv, hsv, mask = mask)
#circles = cv2.HoughCircles(mask[:,:,0],cv2.HOUGH_GRADIENT,1,mask.shape[0]/16,param1=15,param2=20,minRadius=18,maxRadius=38)
circles = cv2.HoughCircles(output[:,:,0],cv2.HOUGH_GRADIENT,1,output.shape[0]/16,param1=15,param2=20,minRadius=15,maxRadius=30)
print(len(circles))
circles = np.uint16(np.around(circles))[0,:]


# In[6]:

for i in circles:
     cv2.putText(img,'P',(i[0],i[1]), font, 0.5,(0,255,0),1,cv2.LINE_AA)


# In[7]:

cv2.imwrite("./result.jpg",img)

1 个答案:

答案 0 :(得分:1)

请注意,此答案并不是要提供解决方案,而可能是完成任务的新观点。即使它在某些情况下可以工作,也可能不足以使任何流程自动化。话虽这么说,转换为HSV色彩空间的问题在于,如果图像(如您的情况)上绘制了相似的颜色对象,那么将很难用cv2.inRange()来将一个对象与另一个对象区分开。我试图对您的代码进行一些改动,并举例说明了如何实现此目标。

首先,您可以尝试在OTSU保留在图像上之后查找所有轮廓,并使用您选择的标准过滤掉最大的(甜甜圈)和其他小的轮廓。

一旦有了,就可以围绕该轮廓进行投资回报。然后,我将尝试在每个ROI上执行cv2.inRange()

此后,我将再次在每个ROI上搜索轮廓,并计算白色像素或为轮廓制定“圆度”标准。如果它们通过,则表示它的范围内有很多像素,并画出字母T。希望对您有所帮助。干杯!

示例:

import cv2
import numpy as np


# In[2]:

path = "./purplecirc4.JPG"
font = cv2.FONT_HERSHEY_COMPLEX


# In[3]:

def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image

    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)

    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)

    # return the resized image
    return resized


# In[4]:

iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,width=960)

# Threshold with OTSU to get all contours
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
_,thresh = cv2.threshold(blur,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
_, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

# Empty list for contours that could be positive
ROIs=[]


# Append possible contours to list
# (I have selected height to eliminate unwanted noise)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    if 200 > h > 20:
        x1 = x-20
        x2 = x+w+20
        y1 = y-20
        y2 = y+h+20
        roi = img[y1:y2, x1:x2]
        ROIs.append(roi)

# Iterate through list of ROIS and transform to HSV
# (I made a little adjustment in values )
for i in ROIs:
    hsv = cv2.cvtColor(i,cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, (115,100,50), (160,255,255))
    # Search for contours on every ROI in list and select the biggest one
    _, contours, hierarchy = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    # Draw them whole on hsv then transform to gray and perform OTSU threshold and search for contoures
    cv2.drawContours(hsv, [cnt], 0, 255, -1)
    gray = cv2.cvtColor(hsv, cv2.COLOR_BGR2GRAY)
    _,thresh = cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
    _, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
    cnt = max(contours, key=cv2.contourArea)
    # Make a "roundness" criterion and draw the letter
    x,y,w,h = cv2.boundingRect(cnt)
    perimeter = cv2.arcLength(cnt,True)
    radius = perimeter/(2*np.pi)
    area = cv2.contourArea(cnt)
    circ = 4*area/(np.pi*(radius*2)**2)
    if circ > 0.70:
        cv2.putText(i,'P',(int(x+(w/2.5)),int(y+(h/2))), font, 0.5,(0,255,0),1,cv2.LINE_AA)

# Display result:
resized = cv2.resize(img, (0,0), fx=0.5, fy=0.5) 
cv2.imshow("roi",resized)
cv2.waitKey(0)
cv2.destroyAllWindows()

结果:

enter image description here

enter image description here

enter image description here