Algorithm Engineering is about experimentation. Here, our focus is Computer Vision, starting with OpenCV, which offers many algorithms from which to choose.
The goal is to explore theories and industry standard algorithms to see what works in the real world.
But whatís our test subject? Well what's more real world than Mother Nature with all of its randomness and variability of growing things?
Clearly, Computer Vision currently has and will continue to have big payoffs in agricultural automation, specifically, weed identification and
eradication without the use of herbicides. So we pick something close to home, like a nice lawn with its share of broadleaf Dandelions.
Weed Location Identification
Initially, this project attempted to apply general purpose OpenCV functions to this difficult object recognition problem.
Parts of the OpenCV framework proved to be applicable but a total solution to the problem remained out of reach using standard functions.
In the end, a custom algorithm was developed to address the problem in a very specific way. As a result, characteristics of the task at hand are deeply embedded in its logic.
Lesson learned is that a non-generalized, not-easily leveraged algorithm can be justified where there is significant ROI for a significant problem solved.
An Algorithm Engineering success can be declared. But further successes can be achieved by revisiting the algorithm for its applicability to similar tasks and
making it configurable and more general purpose.
What were the Challanges?
- Color differentiation not possible
- Without the flower there is no difference in hue of the green weed leaves and the green blades of grass.
- Edge detection and finding contours provides little clues
- Using OpenCV functions to create gray scales, blur, detect edges (Canny), and find contours provided data,
but that data didnít lead to additional processing opportunities.
- Large number s of small and big objects, many overlapping one another, needed to be sorted through
- Ignoring small grass objects and focusing on larger broadleaf object requires initial threshold adjustments.
- Leaf detection not the ultimate goal
- Finding just the leaves is fine for the application of herbicides since thatís where they are absorbed,
but for pulling out the weed by the root, we have to locate the stem more precisely.
Fortunately, leaves radiate outward from the stem so that helps to identify the stem and root.
The circle in the black and white image above is the center of mass of all the intersections of lines projected from the weed's leaves and
can be used to identify the location of the weed's stem or root for picking.
How does the Weed Detection Algorithm Work?
The algorithm reads in the image and converts it using OpenCVís cvtColor() function from RGB to HSV color space.
The H and S channels are not used. Only the V (Value) channel is passed to OpenCVís threshold() function using a threshold of 200 and THRESH_BINARY flag resulting in a high contrast black and white image.
An integer array the height and width in pixels of the source image is initialized with zeroís.
The black and white image is then scanned left to right starting with the top row of pixels looking for contiguous white pixels assigning object numbers to those regions. When it finds and assigns an object id to a white pixel it also looks in the same column in the row below for a white pixel to group in the same region. This look-below step is necessary to setup for scanning the next row.
When the scan moves from a black area to white area it first check to see if an object id was assigned in the look-below step. If so this newly discovered object id is used to group more contiguous white pixels as the scan continues. If not a new object id is created for this newly discovered region.
Itís possibly that the scan can move from a region marked with one object id immediately into a region marked with another object id without even scanning through black pixels because the two objects are touching. In this case the algorithm merges the two objects into one object by re-assigning all of the smaller objectís pixels to the larger object.
Next comes a scan of the objects created above to find the significant ones. There can be hundreds of object of various sizes representing blades of grass, weed leaves or parts of both.
A check is performed to eliminate any objects that have fewer than 500 pixels.
OpenCVís contourArea() and arcLength() functions are then used to check length to width to exclude regions that are likely narrow blades of grass that were still large enough to pass the size test above.
A rotated rectangle is fit to the object based on its width and length and a line projected alonng the length of the object.
The projected lines for the remaining large objects capture the directions of the leaves of the weed and are passed to a function that preforms intersections.
Finally, all on the intersections of lines are averaged together to find a center of mass, which is plotted as a circle indicating the stem of the weed.