Click on the "Xiaobai Learns Vision" above, select "Star" or "Top"
Heavyweight content delivered first time
Here, we will discuss the detection of crop disease severity using OpenCV image processing techniques. This process does not involve any training part. Based on color segmentation technology, we only extract healthier plant areas and calculate disease severity based on the total plant area and the healthier areas.For this, I used some images of potato plants.
Calculation of Plant Disease Severity Based on Image Segmentation.
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. This tool is mainly applied in object tracking, image analysis, image processing, face recognition, etc.
What is HSV Color?
H – Hue, which is the color component (base color), range 0-180
S – Saturation, which is the depth of color, range 0-255
V – Brightness of the color, range 0-255HSV is a color format, like RGB. This image shows the color map of HSV. Compared to RGB, it is easier to extract the desired color areas from an image in HSV. HSV images are not affected by lighting or climate changes.
Morphological Operations
Morphological transformations or operations are some simple operations performed on an image to effect some morphological changes within the image. This operation is performed on single-channel images. The operation requires two inputs for processing, one is a binary image, and the other is a structural element called a kernel. To perform this operation, a kernel of known size is used to convolve the image. The type of operation generally depends on the type of kernel we use. Erosion and dilation are two basic morphological operations. In this project, we used morphological opening operation.
Erosion
Erosion is a morphological operation performed on an image. The basic intuition of this operation is that it uses a kernel to erode the image. After the image is eroded, noise is removed, but the image is also compressed.The input for this operation is a binary image containing values of 0 or 1. A kernel of known size slides over the image. During this operation, if all pixels of the kernel are 1, the pixel of the original image under the kernel is considered 1; otherwise, it will be eroded (turned to zero). This process is used to remove small pixel points (noise) from the image.
Dilation
Dilation is a morphological operation, but it is the opposite of erosion performed on the image. It also takes a binary image as input. In this operation, a pixel of the original image is considered 1 only if at least one pixel of the kernel is 1. This operation expands the white regions, while the erosion operation shrinks the white regions.The combination of these operations helps to eliminate unwanted noise. Erosion removes white points from the image; it is essentially noise, but the objects present in the image are shrunk. Therefore, we use the dilation operation to expand it back to its original shape.
Morphological Opening
When erosion operation is performed before dilation operation, this process is referred to as morphological opening.The proposed image segmentation algorithm uses OpenCV (image processing software tool) to perform the tasks. The algorithm consists of three steps: color segmentation, morphological operations, and severity calculation.Plant disease severity is calculated using image processing techniques in Python.The calculation steps are as follows:1.Load the ImageThe image used is downloaded from the internet. Use cv2.imread to load the image and resize it to 250x250x3.2.Convert BGR Image to HSV Image3.Extract Healthy Areas (Green Pixels)Mask the original image to the HSV range to extract healthy areas (H: 38–86, S: 0-255, V: 0–255).4. Apply Morphological OperationsThe morphological opening operation in OpenCV is a combination of erosion followed by dilation. This operation is completed to remove unwanted pixel points (noise) from the image.5.Extract Plant AreasUse the HSV range to mask the image to extract the entire plant area (H: 26–86, S: 0–255, V: 0–255).Note: HSV values can be changed as required.Figure: a- Actual image, b- HSV image, c- Plant area, d- Healthy plant area, e- Binary image of plant area, f- Binary image of healthy plant area.Figure: a- Actual image, b- HSV image, c- Plant area, d- Healthy plant area, e- Binary image of plant area, f- Binary image of healthy plant area.6. Calculate Disease SeverityDisease severity is calculated as follows:S= Disease severity ((0-1) 0- low, 1- high)HR= Area of healthy regionPR= Area of the entire plant region
import cv2, time
import numpy as np # numerical
start = time.time() # to calculate the run time 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 final image
# cv2.imshow("res",res)
# cv2.imshow("t",t)
cv2.waitKey(0)
cv2.destroyAllWindows()
The quantification of plant disease severity using OpenCV image processing techniques shows good performance in calculating plant infection levels. The algorithm is capable of calculating the disease severity of any plant.The following image shows the output results of three different development techniques. Images of healthy, mildly infected, and highly infected plants were taken to test the algorithm. The disease severity of these 3 images was calculated as 0.04, 0.22, and 0.98, respectively. A severity of 4% for healthier plants is acceptable, which may be due to some lighting changes.The evaluation of this technology is visualized by comparing the output results with the actual input images. The algorithm is capable of calculating the disease severity of plants by applying morphological and color segmentation processes. The output range of the algorithm is from 0 to 1.The developed technology has been tested on local machines as well as Raspberry Pi models. The runtime in both cases is less than 0.1 seconds. Therefore, this technology will be more effective for instantaneous severity calculation of plants and more accurately applying pesticides to unhealthy plants.
Download 1: OpenCV-Contrib Extension Module Chinese Version Tutorial
Reply "Extension Module Chinese Tutorial" in the background of "Xiaobai Learns Vision" public account to download the first OpenCV extension module tutorial in Chinese, covering installation of extension modules, SFM algorithms, stereo vision, object tracking, biological vision, super-resolution processing, and more than twenty chapters of content.
Download 2: Python Vision Practical Project 52 Lectures
Reply "Python Vision Practical Project" in the background of "Xiaobai Learns Vision" public account to download 31 practical vision projects including image segmentation, mask detection, lane detection, vehicle counting, adding eyeliner, license plate recognition, character recognition, emotion detection, text content extraction, face recognition, etc., to help quickly learn computer vision.
Download 3: OpenCV Practical Project 20 Lectures
Reply "OpenCV Practical Project 20 Lectures" in the background of "Xiaobai Learns Vision" public account to download 20 practical projects based on OpenCV to achieve advanced learning of OpenCV.
Group Chat
Welcome to join the public account reader group to communicate with peers. Currently, there are WeChat groups for SLAM, 3D vision, sensors, autonomous driving, computational photography, detection, segmentation, recognition, medical imaging, GAN, algorithm competitions, etc. (will gradually be subdivided). Please scan the WeChat number below to join the group, and note: "Nickname + School/Company + Research Direction", for example: "Zhang San + Shanghai Jiao Tong University + Vision SLAM". Please follow the format for remarks, otherwise, it will not be approved. After adding successfully, you will be invited to join relevant WeChat groups based on research direction. Please do not send advertisements in the group, otherwise, you will be removed from the group. Thank you for your understanding~