Cannabis Bud Area

Detecting number of cannabis cola and bud growth using computer vision

Cannabis Computer Vision

I gleamed my inspiration from PyImageSearch.com yet again. I’m basically counting bright green spots, by combing some of the tricks from the last post on Leaf Area Analysis.

Detecting multiple bright spots in an image with Python and OpenCV

Dependencies

In this docker image I have all of the dependencies for both Leaf Area and Bud Area including working copies and works in progress. We’re going to be using the same setup as with the Leaf Area notebook. OpenCV will be used for preprocessing the image and pixel selection by range. Matplotlib is used from rendering images and graphs. We take advantage of skimage.measure.label for taking measurements and label them. We take these components and use OpenCV to find and annotate the contours.

HSV Range Detector

https://gist.github.com/TonsOfFun/514a5f9dc66e1b4dd64e53bb5c9d7c74

https://gist.github.com/TonsOfFun/514a5f9dc66e1b4dd64e53bb5c9d7c74#file-mac-osx-docker-xquartz-windows-preview

I pulled the first few lines of the script below from this comment. It was need to run the range_detector used in this and the previous blog post.

You can checkout this gif to get an idea of how I go about defining the HSV boundary values.

Creating the initial HSV Mask

https://gist.github.com/TonsOfFun/f9fa0998e670db0c76d2446fc5943b9a

Preview the mask

We can easily mask the original image with OpenCV’s bitwise_and which takes the original image and the mask and outputs a masked image.

https://gist.github.com/TonsOfFun/73d6d3272873a139b9a8c807b8602a79

Cannabis Computer Vision

This is a side-by-side view of the HSV colorspace which the computer uses to isolate the buds and the masked preview. We use the numpy.vstack to vertically stack the images. I chose vstack for the gists here and hstack in the notebook, but it really is a matter of developer preference.

https://gist.github.com/TonsOfFun/71466a09755331778c45e8b72139cf9f

Refining the Mask

We can refine the mask using the same binary threshold, erode, and dilate technique used on the Leaf Area. This will clean up the specs of frost leave from the actual Bud Area.

https://gist.github.com/TonsOfFun/2bd2592db3c82bb6e211aa37e74ccbc9

Before and After Cleaning Up the Contours

You can see the vstack comparing the contours pre and post cleanup.

You can still see a few smaller contours that escape this sweep, but they’re technically smaller bud sites below the top layer of canopy. Either way we’ll filter them in the next pass below.

Enhance Mask with Labeled Components

Using skimage.measure.label we are further filtering out noise by ignoring blobs smaller than 100px. This means buds have to be this size or greater to get added to the labeled mask.

https://gist.github.com/TonsOfFun/1d73013380aa8f971920990155356194

Label the Buds

Now we have what we need to label the bud sites on the preview image.

https://gist.github.com/TonsOfFun/388d663dd4a1af33e1419dfedf65c5d3#file-label-buds-ipynb

You can see the smaller spots, which actually appear to be smaller bud sites below the canopy are not labeled. We can remove or lower the threshold of 100px to further refine the camera’s calibration, but this is a great result for this stage.

A Full Copy of the Notebook Below

Below is the full copy of the notebook. I also have a docker image tonsoffun/tensorflow-notebook which I’ve simply added OpenCV 3.3 via anaconda. This docker image supports both Bud and Leaf Area Analysis as well. Keep following along as I show you how we can use our OpenCV masks to train a Mask RCNN.

https://gist.github.com/TonsOfFun/09d62185e836375d358e54cc7f38e4bf

top: HSV Original, bottom: Final Mask Before Contour Filtering