检测小物体-植物上的蚜虫

时间:2020-03-27 10:20:36

标签: python-3.x opencv computer-vision object-detection

我目前正在尝试在植物上创建一种蚜虫(绿色和玫瑰)检测器,但仅使用“经典”图像处理技术(无神经网络)。 这是我正在处理的图像: 'aphids.jpg'

我正在编写代码(请参见下文)。如果将其应用到图像上,则应单独放置植物。我的问题是我想分离出可以在植物上看到的蚜虫。它们很多,但我只想发现最大的或最明显的。

在代码上,我目前正在使用“ edges_detect”功能。我遇到的问题之一是,我可以检测到一些蚜虫的轮廓,但它也会采用简单的线条... 我尝试使用轮廓线的层次来放置这些线,但似乎这些线具有内部轮廓,因此我无法轻松删除它们。 我还尝试了adjust_gamma和对比度,但是效果不佳。

我正在寻找更多想法。你会尝试什么?

提前谢谢!

代码如下:

import cv2
import numpy as np
import matplotlib.pyplot as plt

def adjust_gamma(image, gamma=1.0):
    # build a lookup table mapping the pixel values [0, 255] to
    # their adjusted gamma values
    invGamma = 1.0 / gamma
    table = np.array([((i / 255.0) ** invGamma) * 255
        for i in np.arange(0, 256)]).astype("uint8")
    # apply gamma correction using the lookup table
    return cv2.LUT(image, table)

def adjust_contrast(image,alpha=1.0,beta=0):
    new = np.zeros(image.shape,image.dtype)
    for y in range(image.shape[0]):
            for x in range(image.shape[1]):
                for c in range(image.shape[2]):
                        new[y,x,c] = np.clip(alpha*image[y,x,c]+beta,0,255)
    return(new)


def img_process(img):
    (h1, w1) = img.shape[:2]    
    center = (w1 / 2, h1 / 2)
    blur = cv2.GaussianBlur(img.copy(),(5,5),0)
    hsv = cv2.cvtColor(blur,cv2.COLOR_BGR2HSV)  
    #image = img.copy()
    #Boundaries to separate plants from the image
    l_bound = np.array([20,0,0])
    h_bound = np.array([90,250,170])#green 
    mask = cv2.inRange(hsv,l_bound,h_bound)
    res = cv2.bitwise_and(img,img,mask=mask)
    #Find contour plants
    cnt,_ = cv2.findContours(mask,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    sort_cnt = sorted(cnt,key=cv2.contourArea,reverse=True)
    cnt = [sort_cnt[i] for i in range(len(sort_cnt)) if cv2.contourArea(sort_cnt[i])>300]
    cv2.drawContours(res, cnt, -1, (0,255,0), -1)
    #Inverse mask to have only the plant in the image
    mask2 = cv2.inRange(res,np.array([0,0,0]),np.array([250,250,250]))
    mask2 = cv2.bitwise_not(mask2)
    res2 = cv2.bitwise_and(img,img,mask=mask2)

        #Augment bright/contrast
    res2=res2*1.45
    res2=res2.astype('uint8')

    #Crop
        res2 = res2[:-50,int(center[0]-300):int(center[0]+550)]


    return res2

def edge_detec(img):
    (h1, w1) = img.shape[:2]    
    center = (w1 / 2, h1 / 2)
    blur = cv2.GaussianBlur(img.copy(),(5,5),0)
    gray = cv2.cvtColor(blur,cv2.COLOR_BGR2GRAY)
    edges = cv2.Canny(gray,30,70,apertureSize = 3)
    edges = edges[:-50,int(center[0]-300):int(center[0]+550)]
    #kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3))
    #edges = cv2.morphologyEx(edges, cv2.MORPH_GRADIENT, kernel)

    cnt,hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cnt = sorted(cnt,key=cv2.contourArea,reverse=True)
    listArea = list(map(cv2.contourArea,cnt))   
    sort_cnt = [x for x in cnt if cv2.contourArea(x)>10]

    cv2.drawContours(edges, sort_cnt, -1, (0,255,0), -1)

    return edges,center,img



### Debut programme
img = cv2.imread('051.jpg')

while True:

    ##Put processing function here
    img_mod = img_process(img)

    cv2.imshow('img',img_mod)

    if cv2.waitKey(1) & 0xFF == 27:
        break


cv2.destroyAllWindows()

0 个答案:

没有答案