Detecting leaf disease with opencv

Leaf Disease detection using image processing OpenCV Python

Leaf disease detection using image processing, OpenCV, and Python is a non-invasive and efficient way to detect the diseases in the plant.

There are some benefits for this First, it is much faster, allowing farmers to inspect large fields of crops quickly and easily. Second, it is more accurate, as algorithms can be trained to identify diseases that are difficult to spot with the naked eye. Third, it is non-invasive, meaning that leaves do not need to be damaged or destroyed for testing.

We are going to present the code which use the color segmentation technique to segment the real time frames from the camera. Once frame is captured, it is passed to the color segmenation based code. That code detect the dominant color of the image. Then using a simple rule based approach the disease is predicted with healthy or diseased plant.

Python Code for Leaf Disease Detection using Color Segmentation

Here is the complete code for this project

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

def dominateColor(images):
    #Check the distribution of red values 
    v = []
    c = ['Red','Green','Blue']
    red_value = np.mean(images[:, :, 2])

    # Check the distribution of green values 
    green_value = np.mean(images[:, :, 1])

    # Check the distribution of blue values 
    blue_value = np.mean(images[:, :, 0])
    return v.index(max(v))

def detect_leaf(img):
    kernel = np.ones((7,7),np.uint8)
    hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    # find the brown color
    mask_brown = cv2.inRange(hsv, (8, 60, 20), (30, 255, 200))
    # find the yellow and green color in the leaf
    mask_yellow_green = cv2.inRange(hsv, (10, 39, 64), (86, 255, 255))
    # find any of the three colors(green or brown or yellow) in the image
    mask = cv2.bitwise_or(mask_yellow_green, mask_brown)
    mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
    mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
    #output = cv2.bitwise_and(img, img, mask=mask)
    return mask

redCount = 0
greenCount = 0
  # Capture frame-by-frame
  ret, frame =
  if ret:
    mask = detect_leaf(frame)
    contours, hierarchy = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    output = frame
    if contours:
        # find the biggest countour (c) by the area
        c = max(contours, key = cv2.contourArea)
        x,y,w,h = cv2.boundingRect(c)
        crop_img = frame[y:y+h, x:x+w]
        dc = dominateColor(crop_img)
        if dc == 0:
            redCount = redCount+1
            greenCount = 0
        if dc == 1:
            #hist2 = cv2.calcHist([crop_img],None,[256],[0,256])
            hist2 = cv2.calcHist([crop_img],[1],None,[256],[0,256])
            histogram, bin_edges = np.histogram(
                crop_img[:, :, 1], bins=10, range=(0, 10)
            if sum(histogram)>140:
                greenCount = greenCount+1
                redCount = 0
                redCount = redCount + 1
                greenCount = 0

            #hsv_croped = cv2.cvtColor(crop_img, cv2.COLOR_BGR2HSV)
            #lower = np.array([22, 93, 0])
            #upper = np.array([45, 255, 255])
            #mask = cv2.inRange(hsv_croped, lower, upper)
            #o2 = cv2.bitwise_and(crop_img, crop_img, mask=mask)
    if redCount> 10:
        redCount = 0
        greenCount = 0
        print('Deseased Leaf')
    elif greenCount>10:
        greenCount = 0
        redCount = 0
        print('Healthy Leaf')
    if cv2.waitKey(1) & 0xFF == ord('q'):
    print('no frame')
# When everything done, release the capture

Code language: Python (python)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.