4
\$\begingroup\$

Introduction

I have wanted to develop this project for more than two years. I just started writing it a couple days ago. I use it to detect plant or residue pixels in crop row images but it provides a good example of how to detect green and red color. I could not find anything like this on Stackoverflow/Stackexchange, but I did end up finding some information on the IEEE site. I think this will benefit someone that came after me.

Core Concept

The core concept is really easy. Assume that your crop row images are in this dataset.

To detect a Plant (color green), convert the RGB image to a gray scale image using the formula:

GRAY = 2*G-R-B

To detect Residue (color red), convert the RGB image to a gray scale image using the formula:

GRAY = 1.5*R-G-B

Basically, if you want to apply theses algorithm/equation above for something else, you should change the constants 1.5 and 2 to fit your task.

Project

So I decided to publish this repository. There are three main scripts:

  1. index.py analyzes all images in the "images" folder.

  2. webcamplant.py analyzes images directly from a camera streaming images in real time.

  3. covEX.py contains the actual algorithm. It is written using numpy (check out my previous question for more information).

index.py

import covEX
import time
import os
from os import listdir
from os.path import isfile, join

start_time = time.time()
pathname = 'Images/'
pathsave = 'Images-converted/'

filelist = [f for f in listdir(pathname) if isfile(join(pathname, f))]

for i, val in enumerate(filelist):
    print('processing file number '+str(i))
    filename = os.path.splitext(val)[0]
    fileextension = os.path.splitext(val)[1]
    covEX.convertTofile(pathname, filename, fileextension, pathsave)

print('Shite! I\'m done ;) ')
print("--- %s seconds ---" % (time.time() - start_time))

webcamplant.py

import covEX
import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here    
    plantimg = covEX.convertGreenSerious(frame)
    # Display the resulting frame
    plantimg = cv2.flip(plantimg,1)
    cv2.imshow('frame',plantimg)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

covEX.py

import cv2
import numpy as np
import os


def calEXG(red,green,blue):
    result = 2*green-red-blue
    return result

def calEXR(red,green,blue):
    result = 1.5*red-green-blue
    return result

def savePlants(savename,plantimg,exgimg,exgimgblurred,original,notblurred,blurred,otsu,extension):
    cv2.imwrite(savename+'/'+original+extension,plantimg)
    cv2.imwrite(savename+'/'+notblurred+extension,exgimg)
    cv2.imwrite(savename+'/'+blurred+extension,exgimgblurred)

    # ret2,th2 = cv2.threshold(exgimgblurred,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
    # cv2.imwrite(savename+'/'+otsu+extension,th2)


def convertGreenSerious(rawimg):
    blue = rawimg[:,:,0]
    green = rawimg[:,:,1]
    red = rawimg[:,:,2]
    exg = 1.5*green-red-blue
    processedimg = np.where(exg > 50, exg, 0)
    processedimg = np.uint8(np.abs(processedimg))

    return processedimg

def convertGreenPlant(rawimg):
    blue = rawimg[:,:,0]
    green = rawimg[:,:,1]
    red = rawimg[:,:,2]
    exg = 2*green-red-blue
    processedimg = np.where(exg > 50, exg, 0)
    processedimg = np.uint8(np.abs(processedimg))

    return processedimg

def convertRed(rawimg):
    blue = rawimg[:,:,0]
    green = rawimg[:,:,1]
    red = rawimg[:,:,2]
    exg = 1.5*red-green-blue
    processedimg = np.where(exg > 50, exg, 0)

    return processedimg



def convertTofile(filepath,filename,extension,savepath):
    directory = filepath+filename+extension
    plantimg = cv2.imread(directory)

    exgimg = convertGreenPlant(plantimg)
    exgimgblurred = cv2.GaussianBlur(exgimg,(5,5),0)

    if not os.path.exists(savepath+filename):
        os.makedirs(savepath+filename)

    savePlants(savepath+filename,plantimg,exgimg,exgimgblurred,'original','notblurred','blurred','otsu','.png')

How should I improve this project to be more professional and easy to integrate with other peoples projects?

PS. Some good reads for this research topic:

\$\endgroup\$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.