Crop diseases and insect pests is one of the main agricultural disasters in China. It has the characteristics of many kinds, great influence and frequent outbreak. Its occurrence range and severity often cause great losses to our national economy, especially to agricultural production.
With the rapid development of computer science and technology, there are a lot of AI methods applied to the detection and control of diseases and insect pests, and then use modern technology to help the donation out of poverty!
In this paper, starting from computer vision technology, using Python language to analyze the research and application of target detection in crop diseases and insect pests.
catalog
1. Project introduction
1.1 project overview
The purpose of this project is to use Python language to analyze and elaborate the application of target detection in computer vision technology in crop diseases and insect pests. Specifically, we will use Python language to run and get the area of the target leaf that has suffered from diseases and insect pests, and then analyze whether it is necessary to carry out pesticide spraying and other prevention and control of diseases and insect pests, so as to help farmers better manage crops, reduce losses and increase yields
1.2 preliminary preparation
First of all, look for the detection target: Here we detect and process a part of leaves suffering from diseases and insect pests
Using python language and some third-party libraries for blade analysis and processing
ad locum:
Python environment: 3.8.2
python compiler: JetBrains pycharm2018.1.2 x64
Third party libraries: OpenCV, ilmutils, easygui, numpy, PIL, etc
2. Project analysis
2.1 code details
Import all libraries used
import cv2 import imutils import easygui from PIL import Image, ImageDraw, ImageFont import numpy as np
In advance, the leaves with diseases and insect pests shall be painted (painted white) as background
Import pictures and do black and white processing
# Folagenew as the background picture is a picture that is painted with a brush to white in advance where there are pests and diseases on the leaves, and read it PSpicture = cv2.imread(r"E:\foliageNew.png") # Convert picture folagenew to black and white PSpicture = cv2.cvtColor(PSpicture, cv2.COLOR_BGR2GRAY)
Gauss processing of background image
# Gaussian processing of folagenew PSpictureGS = cv2.GaussianBlur(PSpicture, (21, 21), 0)
At the same time, the target detection blade shall be painted (painted white) in advance
Do the same for it
# foliageWhite is the leaf target detection map, read it originalPicture = cv2.imread(r"E:\foliageWhite.png") # Convert picture foliageWhite to black and white originalPicture = cv2.cvtColor(originalPicture, cv2.COLOR_BGR2GRAY) # Gaussian processing of folagewhite originalPictureGS = cv2.GaussianBlur(originalPicture, (21, 21), 0)
Make a difference between the two processed pictures, and the returned value represents the difference
# Compare the images foliageNew and foliageWhite, and the returned results represent their differences pictureDelta = cv2.absdiff(PSpictureGS, originalPictureGS)
Because relevant data is needed later, check the pixel size of the picture in advance
# x. Y is the pixel size of the picture x, y = pictureDelta.shape print(x, y)
This value is the same as shown in the picture properties, which is exactly what we expect
Of course, here we can do edge detection to further confirm the detection target area we want
# pictureDelta is the area of the image, canny is the outline of the image (white area) img = cv2.GaussianBlur(pictureDelta, (3, 3), 0) # Canny edge detection canny = cv2.Canny(img, 0, 100)
Determine the target detection area (this is the contour area, not the entire image area)
That is, the detection area is determined on the second Gaussian image (the area with white pixel value is the target area we want)
# Draw the outline, store the position of the pixel value to be recognized, and record it in the distinguishLeaf array for i in range(x): for j in range(y): if any(originalPicture[i, j] == [255, 255, 255]): # When the color is white, space occupying distinguishLeaf.append([i, j])
Traverse the target area (stored in the array, and then operate on the array)
Where LeafArea is the area of the target detection leaf (the accumulated value of multiple pixels)
greenLeafArea is the area of the green part of the target leaf (the cumulative value of multiple pixels)
Because the gray-scale processing (img image) has been done before, you only need to check whether the pixel value is black (that is, whether the value is equal to 0)
It is easy to get that the non black part is the green part of the blade, so once the non black part is determined, the number of pixel points + 1
for t in distinguishLeaf: k, l = t LeafArea = LeafArea + 1 if img[k, l] != 0: # print(canny0[k, l]) greenLeafArea += 1
So far, more than half of them have been successful. What we need to do next is to output the proportion of pests and diseases in the leaf area
scale = 100 - (greenLeafArea/LeafArea)*100 percentage = "The proportion of diseases and insect pests on the leaf surface is:" + str(scale) + ' %' print(percentage)
Of course, we can further reflect the following: if the disease and insect damage to the leaves reaches a certain value, timely remind the agricultural uncle to spray pesticides for control.
if scale < 95: easygui.msgbox('Warning! Leaves suffer from diseases and insect pests! Please spray pesticide as soon as possible!')
Let's repeat here, that is, the image processing results of each step in the running code can be output, just like this
cv2.imwrite("Here is the address for storing local pictures", Here is the code name of which image to output) canny0 = cv2.imread("Here is the address for storing local pictures") cv2.imshow('Here is the image title name', imutils.resize(canny0))
2.2 general code
# Guide library import cv2 import imutils import easygui from PIL import Image, ImageDraw, ImageFont import numpy as np # Folagenew as the background picture is a picture that is painted with a brush to white in advance where there are pests and diseases on the leaves, and read it PSpicture = cv2.imread(r"E:\foliageNew.png") # Convert picture folagenew to black and white PSpicture = cv2.cvtColor(PSpicture, cv2.COLOR_BGR2GRAY) # Gaussian processing of folagenew PSpictureGS = cv2.GaussianBlur(PSpicture, (21, 21), 0) # foliageWhite is the leaf target detection map, read it originalPicture = cv2.imread(r"E:\foliageWhite.png") # Convert picture foliageWhite to black and white originalPicture = cv2.cvtColor(originalPicture, cv2.COLOR_BGR2GRAY) # Gaussian processing of folagewhite originalPictureGS = cv2.GaussianBlur(originalPicture, (21, 21), 0) # Make a difference (comparison) between the pictures folagenew and folagewhite, and the returned results represent their differences pictureDelta = cv2.absdiff(PSpictureGS, originalPictureGS) # x. Y is the pixel size of the picture x, y = pictureDelta.shape # print(x, y) # pictureDelta is the area of the image, canny is the outline of the image (white area) img = cv2.GaussianBlur(pictureDelta, (3, 3), 0) # Canny edge detection canny = cv2.Canny(img, 0, 100) # Define the total area of the outline (a leaf) LeafArea = 0 # Define the area of green leaves (not affected by pests and diseases) greenLeafArea = 0 # Definition list, used to store the location of pixel points to be identified distinguishLeaf = [] # Draw the outline, store the position of the pixel value to be recognized, and record it in the distinguishLeaf array for i in range(x): for j in range(y): if any(originalPicture[i, j] == [255, 255, 255]): # When the color is white, space occupying distinguishLeaf.append([i, j]) canny0 = cv2.add(originalPictureGS, canny) # Judge leaf color for t in distinguishLeaf: k, l = t LeafArea = LeafArea + 1 if img[k, l] != 0: # print(canny0[k, l]) greenLeafArea += 1 # Statistical proportion of green leaves scale = 100 - (greenLeafArea/LeafArea)*100 percentage = "The proportion of diseases and insect pests on the leaf surface is:" + str(scale) + ' %' print(percentage) # cv2.imwrite("here is the address to store the local image", here is the code name of the image to be output) # canny0 = cv2.imread("here is the address for storing local pictures") # cv2.imshow('Here is the image title name ', imutils.resize(canny0)) if scale < 95: easygui.msgbox('Warning! Leaves suffer from diseases and insect pests! Please spray pesticide as soon as possible!') # This line of code is used to avoid flashback of the output picture key = cv2.waitKey(0)
2.3 project operation results
3. Summary and Prospect
From the above results, 17% of the leaf area has been crossed by diseases and insect pests. This value has exceeded the initial setting value of the minimum disease, so the last pop-up window displays "warning! Leaves suffer from diseases and insect pests! Please spray as soon as possible! "
This project uses simple examples to introduce the application of computer vision technology in agriculture, which has played a certain role in helping farmers get rid of poverty by donating production.
This is the realization of the Internet plus intelligent agriculture project.
Copyright notice: this column is all for CSDN blogger "it"_ The original article of "change" follows CC 4.0 BY-SA copyright agreement.
For reprint, please attach the original source link and this statement.
Thank you for reading! Thank you for your support! Thank you for your attention!
I hope this article can help readers to learn and understand computer vision technology, and please comment and correct!
At the end of May 2020 in Datong, Shanxi
END