CROP DISEASE DETECTION USING IMAGE PROCESSING TECHNIQUE AND CNN NETWORKS : Part 3

B.Thushar Marvel
7 min readApr 15, 2022

--

Quantification of plant disease severity using OpenCV image processing technique.

In part 1, we discussed about Implementing CNN based image classification model for crop disease detection.

[link : CROP DISEASE DETECTION USING IMAGE PROCESSING TECHNIQUE AND CNN NETWORKS : Part 1]

In Part 2, we discussed about Implementing Deep learning object detection models for crop disease detection.

[Link : CROP DISEASE DETECTION USING IMAGE PROCESSING TECHNIQUE AND CNN NETWORKS : Part2 ]

Here, we will be discussing about crop disease severity detection using OpenCV image processing techniques. This process doesn’t involve any training part. Based on color segmentation techniques, we just extract the healthier plant region. And compute disease severity based on total plant area vs healthier region.

I have used some Potato plant images for this purpose [ source:- field images and Internet images]

Image segmentation based plant disease severity calculation.

What is OpenCV?

OpenCV is an image processing software tool or library used for performing machine learning and computer vision tasks. The library comes with a set of built in scripts to perform image analysis, image detection etc. Main application of this tools is in object tracking, image analysis, image processing, face recognition and so on

What is HSV colour?

H- Hue is the color component(base pigment), range 0–180
S- Saturation is the amount of color depth, range 0–255
V-Value is brightness of the colour, range 0–255

Source : HSL and HSV — Wikipedia

HSV is one type of colour format like RGB. The figure shows the colour map of HSV. When compared to RGB it is easy to extract the desired colour region from the image in HSV. The HSV images are not affected by the illumination or by climatic variation.

Morphological operation

Morphological transformations or operations are some simple operations performed on images to make some morphological changes within the image. This operation is performed on a single channel image. The operation requires 2 inputs for processing, one is the binary image and other is the structuring element called kernel . To perform this operation a kernel of known size is made to convolve over the image. Type of operation generally depends on the type of kernel we use. Erosion and Dilation are the two basic morphological operations. In this project we used a Morphological Opening operation.

Erosion

Erosion is one type of morphological operation performed over the images .The basic intuition of this operation is it erodes the image using the kernel. The input for this operation is a binary image which contains value 0 or 1.The kernel of known size is made to slide over the image. During this operation a pixel of the original image which is under the kernel will be considered 1 if all the pixels under the kernel are 1, otherwise it gets eroded (made to zero). This process is used to remove small dot pixels(noise) from the images.

Source : Morphological Operations by Matt Maulion

Dilation

Dilation is one type of morphological operation but opposite to erosion performed over the images. This also takes binary images as input. In this operation a pixel of original image which is under the kernel will be considered 1 only if atleast one pixel under the kernel is 1. This operation widens the white region whereas erosion operation narrows the white region.

Source : Morphological Operations by Matt Maulion

The combination of these operations are helpful in removing the unwanted noises. Erosion removes white dots from the image; it is basically noise, but objects present in the image get shrinked. So we dilate it to reshape it to its original shape using dilation operation.

Opening

When erosion operation is done before dilation, then this process is called morphological opening operation.

The proposed image segmentation algorithm uses OpenCV (image processing software tool ) to perform the task. The proposed algorithm consists of 3 steps, i.e, color segmentation, morphological operations and severity calculation.

Plant disease severity is calculated using image processing techniques in python.

The steps followed in calculation are:

1. Loading image

The images used are downloaded from the internet. Using cv2.imread the image is loaded and resized to 250x250x3

2. Convert BGR image to HSV image

3. Extract healthy regions (green pixels).

Masking the original image to HSV range for Healthy region extraction (H: 38–86, S: 0- 255,V:0–255 )

4. Apply morphological operations

Morphological open operation in OpenCV is the combination of erosion followed by dilation. This operation is done to remove unwanted dot pixels (due to illumination) from image

5. Extract plant region

Masking the image using HSV range for Entire plant region extraction (H:26–86,S: 0–255,V:0–255)

[Note: HSV values can be altered based on requirement]

Figure : a-Actual image, b-hsv image, c-Plant region, d- Plant healthy region, e-binary image of plant region, f-binary image of plant healthy region

Figure : a-Actual image, b-hsv image, c-Plant region, d- Plant healthy region, e-binary image of plant region, f-binary image of plant healthy region

6. Calculate disease severity

Disease severity is calculate using,

S= Severity of disease ((0–1) 0-low,1- high)

HR=Area of plant healthy region

PR=Area of entire plant region

import cv2, time
import numpy as np # numeraical
start = time.time() # to calculate the run tie required
img = cv2.imread("path/to/image file) # image reading
img=cv2.resize(img,(250,250)) # image resizing
hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # Converting it to hue saturation value image
range1=(26,0,0)
range2=(86,255,255)
mask1=cv2.inRange(hsv,range1,range2)
#apply morphological operations
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5)) # create structuring element
mask2 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, kernel1) # Apply mask to images
mask2 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, kernel1) # Apply morphological
# open and close function
res=cv2.bitwise_and(img,img,mask=mask2) # these are done to display images
range1, range2 = (38,0,0), (86,255,255)
mask = cv2.inRange(hsv,range1,range2)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (7,7))
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
t=mask
mask = cv2.merge([mask,mask,mask])
mask_inv = 255 - mask
white = np.full_like(img, (255,255,255))
img_masked = cv2.bitwise_and(img, mask)
white_masked = cv2.bitwise_and(white, mask_inv)
result = cv2.add(img_masked, mask_inv)
cv2.imwrite("green_plant_mask.png", t)
cv2.imwrite("green_plant_white_background.jpg", result)
cv2.imwrite("plant_region.jpg", res)
cv2.imwrite("binary.jpg",mask1)
cv2.imwrite("hsv_image.jpg",hsv)
x=cv2.countNonZero(t)
y=cv2.countNonZero(mask2)
print(x,y)
print("severity of disease is {}".format(1-(x/y)))
end = time.time() # to stop the time and to calculate runtime
print(f"Runtime of the program is {end - start} seconds")
cv2.imshow("img",img)# to display original image
cv2.imshow("binary", mask1) # to display binary image
cv2.imshow("hsv", hsv) # to display hsv image
cv2.imshow("result", result) # to display finalimage
# cv2.imshow("res",res)
# cv2.imshow("t",t)
cv2.waitKey(0)
cv2.destroyAllWindows()

Quantification of disease severity in plants using open cv image processing technique performed well enough in calculating the level of infection in plants. The algorithm was able to calculate the severity of disease in any plant. The output results of the developed technique was shown in the below figure for three different plants. Healthy, mild infected and highly infected plant images were taken for testing the algorithm. The severity of disease for these 3 images were calculated as 0.04,0.22 and 0.98 respectively. For a healthier plant 4% of severity is acceptable, it may be due to some illumination changes.

Evaluation for this technique is by means of visualizing the output results with actual input image. The algorithm was able to calculate the disease severity in plants by means of applying morphological and color segmentation processes. The output of this algorithm was ranging from 0 to 1. The developed technique is tested both in local machine as well as in the raspberry pi model. The run time for both cases was less than 0.1 seconds. So this technique will be more effective for instantaneous severity calculation in plants and to precisely apply agrochemicals.

Please a clap/follow if the content is useful
Thank you

--

--