plantcv
plantcv copied to clipboard
An issue with importing plantcv.
Hi plantcv team, I am interested in plantcv for my research project. I installed the plantcv as instruction, however, it has error when I import the required packages as below. Could you please guide me how to solve this problem. Best regards, Lam
%matplotlib notebook
Import statements
import os import argparse import matplotlib import numpy as np from plantcv import plantcv as pcv
ModuleNotFoundError Traceback (most recent call last) Input In [3], in <cell line: 7>() 5 import matplotlib 6 import numpy as np ----> 7 from plantcv import plantcv as pcv
File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv_init_.py:20, in
File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\transform_init_.py:15, in
File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\transform\warp.py:10, in
File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\visualize_init_.py:3, in
File ~.conda\envs\plantcv\lib\site-packages\plantcv\plantcv\visualize\histogram.py:10, in
File ~.conda\envs\plantcv\lib\site-packages\plotnine_init_.py:9, in
File ~.conda\envs\plantcv\lib\site-packages\plotnine\stats_init_.py:7, in
File ~.conda\envs\plantcv\lib\site-packages\plotnine\stats\stat_density_2d.py:3, in
ModuleNotFoundError: No module named 'matplotlib._contour'
Hi @lamvanbio, we recently ran into this issue, it appears that the latest version of matplotlib
breaks the latest version of plotnine
. I think if you delete your current plantcv environment and reinstall it like the following it should fix the issue:
# Remove the plantcv environment
conda env remove -n plantcv
# Install and limit the upper version of matplotlib
conda create -n plantcv plantcv 'matplotlib<3.6'
Hi Noah, Thank you for your reply. I have remove plantcv environment and reinstall it as you suggested. However, I do not see jupyter notebook in plantcv environment in Anaconda Navigator. I did try to run below codes in Anaconda Prompt, I got this message: (plantcv) C:>from plantcv import plantcv as pcv 'from' is not recognized as an internal or external command, operable program or batch file.
import os import argparse import matplotlib import numpy as np from plantcv import plantcv as pcv
Could you please suggest what I should do. Thank you. Best regards,
To add jupyter to your environment, with Anaconda Prompt:
conda activate plantcv
conda install -c conda-forge nb_conda jupyterlab ipympl nodejs
This is also possible with the Anaconda Navigator program, I am just less familiar with it myself.
Once those packages install you can run with Anaconda Prompt:
jupyter lab
I activated plantcv and run the second code you suggested on Anaconda Prompt. However, I still get this issue: Preparing transaction: done Verifying transaction: done Executing transaction: / Enabling nb_conda_kernels... CONDA_PREFIX: C:\Program Files\Anaconda3\envs\plantcv Status: enabled
- Config option
kernel_spec_manager_class
not recognized byEnableNBExtensionApp
. Enabling notebook extension nb_conda/main... - Validating: ok Enabling tree extension nb_conda/tree... - Validating: ok Config optionkernel_spec_manager_class
not recognized byEnableServerExtensionApp
. Enabling: nb_conda - Writing config: C:\Program Files\Anaconda3\envs\plantcv\etc\jupyter
- Validating... nb_conda 2.2.1 ok
done
(plantcv) C:>from plantcv import plantcv as pcv 'from' is not recognized as an internal or external command, operable program or batch file.
You are getting that error because you are in Anaconda Prompt but not in Python. You can start a Python prompt in Anaconda by running python
but I would recommend starting jupyter lab
instead
That's great. It works now. Thank you for your support.
Hi Noah, I have a problem as below when reading the image using this code: img, path, filename=pcv.readimage(filename=args.image)
Please suggest me how to deal with this. Thank you.
RuntimeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_9476\800198661.py in <cell line: 1>() ----> 1 img, path, filename=pcv.readimage(filename=args.image)
C:\Program Files\Anaconda3\envs\plantcv\lib\site-packages\plantcv\plantcv\readimage.py in readimage(filename, mode) 49 50 if img is None: ---> 51 fatal_error("Failed to open " + filename) 52 53 # Split path from filename
C:\Program Files\Anaconda3\envs\plantcv\lib\site-packages\plantcv\plantcv\fatal_error.py in fatal_error(error) 11 :return: 12 """ ---> 13 raise RuntimeError(error)
RuntimeError: Failed to open imgs/10.9.1.29_pos-153-001-003_2019-10-26-19-05.jpg
It is saying it cannot open the image - typically this would be caused by the path to the image being incorrect. Based on the path imgs/10.9.1.29_pos-153-001-003_2019-10-26-19-05.jpg
the folder imgs
would need to be in the same location as your Jupyter notebook, otherwise the path needs to be updated.
I am trying to follow your tutorial on youtube (https://www.youtube.com/watch?v=zvn_05cE0L4&t=691s) and I want to use the picture you showed. I am trying to file the folder imgs in my computer but I could not find it. I am wondering when we install plantcv, we will have that folder with images in our computer? Or do I need to copy that folder to my computer?
Thank you.
Hi @lamvanbio, the workshop materials are available separately from plantcv installation and can be downloaded from here (for that workshop): https://github.com/danforthcenter/plantcv-nappn2021-workshop
Thanks Noah, I can load the image now. I am going to take the image of my plants soon. I am wondering what is background for the image?
Thank you. Lam
Hi Noah, I got error when I try below codes. Could you please help. Thank you.
Plot histogram of grey scale
hist=pcv.visualize.histogram(gray_img=a)
TypeError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_13620\4182240071.py in <cell line: 2>() 1 # Plot histogram of grey scale ----> 2 hist=pcv.visualize.histogram(gray_img=a)
TypeError: histogram() got an unexpected keyword argument 'gray_img'
pcv.visualize.histogram
was updated to support both RGB and grayscale input images, so the keyword was changed from gray_img
to img
: https://plantcv.readthedocs.io/en/stable/visualize_histogram/
This page in the documentation records all changes to function inputs and outputs by version: https://plantcv.readthedocs.io/en/stable/updating/#plantcvvisualizehistogram
Thanks Noah,
I got it now. I am working on wheat and below are what I have processed. I attached two photos at the step of Set Threshold and obj. As I see, wheat leaves are not flat (they have some twists) and at the twists the "white color" is broken. I am wondering if you have a solution for this.
Thank you. Best regards, Lam
Hi @lamvanbio ,
I often find improved segmentation of tricky images by combining multiple masks from different channels. It's hard for me to say which channel might be best for picking up those parts of leaves without getting any background without having the original image to test on. I'd recommend running colorspace_img = pcv.visualize.colorspaces(rgb_img=img)
and then use pcv.image_add
or another method to combine. An example workflow where we use more than one colorspace can be found here.
Another approach that sometimes works is to be aggressive/inclusive with the first threshold to avoid major gaps, and then use clean-up functions (such as pcv.fill
, pcv.fill_holes
, or pcv.erode
) to remove the noise that remains.
Thanks Haley, I will try that. Best regards, Lam
Hi Haley,
I have tried what you suggested. I am happy with that. However, I have two plants/image. Please suggest me how I can get the results of them separately?
Thank you.
Best regards,
Here is what I get.
@lamvanbio awesome!
Next, to separate your objects I would usually define one ROI per object and then use a Python for
loop to filter out each plant, do object_composition, and all measurements. We have a tutorial here that has an example of how to analyze multiple plants within a single workflow.
Thanks Haley,
I will try that. I have a issue to open Jupyter Notebook after I install plantcv in my Macbook. It work well in my windows computers. However, I could not open Jupyter Notebook in plantcv environment. Could you please suggest me how to solve that.
Best regards, Lam
Hi @lamvanbio, did you install Jupyter with your plantcv environment? We don't currently do that automatically because it's not strictly a dependency for using PlantCV. If you haven't yet, I posted the commands above: https://github.com/danforthcenter/plantcv/issues/961#issuecomment-1261646222
Hi Noah,
I did install it using the code you sent me last time. However, when I launched the Jupyter Notebook, it did not open the Jupyter (web). Here is the image of plantcv with Jupyter Notebook.

Hi @lamvanbio, all I am seeing in the screenshot is Anaconda Navigator. Does clicking the Launch button on the Jupyter Notebook tile not work?
I only use Miniconda and launch Jupyter from the command-line so I'm not as familiar with using Anaconda
Hi Noah, I could not open Jupyter Notebook but I can open JupyterLab. So, that's fine with me. At this stage, my plants are big so I take only one plant/photo. I am wondering if I can select by using "rectangular" instead of using circle as below code:
Define region of interest
roisl, roi_hierarchyl=pcv.roi.multi(img=rot_img, coord=(1600, 1200), radius=1100, spacing=(500, 415), nrows=1, ncols=1)
I did try to use below code and it works but then I cannot run following codes. Could you please suggest me to solve that?
Define the region of interest (ROI)
Inputs:
img - RGB or grayscale image to plot the ROI on
x - The x-coordinate of the upper left corner of the rectangle
y - The y-coordinate of the upper left corner of the rectangle
h - The height of the rectangle
w - The width of the rectangle
roisl, roi_hierarchy= pcv.roi.rectangle(img=rot_img, x=330, y=1000, h=1300, w=2900)
We need the error you are seeing in order to help
Hi Noah, If I use circle to select area of interest, it does not cover all. Can I use rectangular or other shape instead? I also have error as in the last image.




Can I use rectangular or other shape instead?
Yes, the functions are meant to be modular so you can use the ROI functions interchangeably (mostly).
Also note that the tutorials are meant to be example workflows, they can be modified any way you need to work with your specific data.
When you use the ROI with pcv.roi_objects
and set roi_type="partial"
, the ROI does not need to fully enclose the entire plant - all objects that overlap with the ROI are kept. In your case, because the outer leaves are being detected as separate objects, they don't overlap with the ROI. But you are correct that a rectangular ROI would be easier to use in this case.
I also have error as in the last image.
The error is saying that args.filename
is not defined. You could comment out that line, it's saving the plotted image to a file. Or, replace args.filename
with a variable containing the input image filename. For example, if you have a line like this:
img, filepath, filename = pcv.readimage(filename="input_image.png")
Then filename
is the name of the input image file and can be used instead of args.filename
Thanks Noah,
Now I am trying to run analysis. I follow the workshop from youtube. I will try with the images you provide on github. As I understand, I need to code files: completed_multi_plant_notebook.py and multi-plant-analysis.config. Is that correct? Also, I think I need to download these files into my working directory. Is that right? I try to download this file into my working directory but I could not find the way to download them. Could you please let me know how to get it.
Thank you.