danforthcenter / plantcv

Plant phenotyping with image analysis
Mozilla Public License 2.0
631 stars 258 forks source link

Analyze Thermal Values can not work #753

Open Ove7comer opened 3 years ago

Ove7comer commented 3 years ago

Describe the bug hi there, When I input a PNG type image (Gray, alpha channel=no, choose 'gray')in Analyze Thermal Values notebook, in Analysis step, the error happens:

SystemError Traceback (most recent call last)

in 9 # label - Optional label parameter, modifies the variable name of observations recorded. (default `label="default"`)filled_img = pcv.morphology.fill_segments(mask=cropped_mask, objects=edge_objects) 10 ---> 11 analysis_img = pcv.analyze_thermal_values(thermal_array=thermal_data, mask=kept_mask, histplot=True) /usr/local/lib/python3.8/site-packages/plantcv/plantcv/analyze_thermal_values.py in analyze_thermal_values(thermal_array, mask, histplot) 31 max_value = np.amax(thermal_array) 32 # Calculate histogram ---> 33 hist_thermal = [float(i[0]) for i in cv2.calcHist([np.float32(thermal_array)], [0], mask, [256], [0, max_value])] 34 bin_width = max_value / 256. 35 b = 0 SystemError: returned NULL without setting an error the sample image I used is: ![1](https://user-images.githubusercontent.com/43070293/114746056-a8efb580-9d92-11eb-84e7-edf29d14e58c.png) looking forward your reply!
nfahlgren commented 3 years ago

Hi @Ove7comer,

The pcv.analyze_thermal_values function expects the input image to be a 64-bit floating-point number rather than the typical 8-bit or 16-bit unsigned integer value of images. This is because the thermal data we work with are calibrated and reported as temperature values. Because your input image is a PNG file you likely have 8-bit unsigned integer values. To work around this you can run the analyze function like this:

import numpy as np
therm_hist = pcv.analyze_thermal_values(thermal_array=thermal_data.astype(np.float64), mask=kept_mask, histplot=True)
Ove7comer commented 3 years ago

That is great, Thanks. However, the histplot I got is a little bit weird:

Screen Shot 2021-04-15 at 02 56 04

Hope you can help me!

nfahlgren commented 3 years ago

Looks like the issue is that there are fewer unique temperature values observed than there are histogram bins (I think the default is 30?). It looks like we did not add an input to control the number of bins as we have in other functions. This is something we would need to add in order to be able to generate a smoother looking histogram