How Can I Export Histogram Data to a CSV File and Store It on SD Card

Hi, I would like to ask how to extract data from the histogram (using the Statistics class) and save it to a .csv or Excel compatible file format to the SD card.

I want to collect all the values from the Statistics object such as mean(), median(), mode(), stdev(), min(), max(), and others and save them line by line into a CSV file for later analysis ( for example in Excel).

Could someone please guide me on how to format the output correctly and save it to the SD card using OpenMV?

Thank you in advance!

hist = img.get_histogram()
statistic = hist.get_statistics()
 
#Extract key statistics
mean_value = statistic.mean()
median_value = statistic.median()
mode_value = statistic.mode()
stdev_value = statistic.stdev()
min_value = statistic.min()
max_value = statistic.max()

l_mean = statistic.l_mean()
l_median = statistic.l_median()
l_mode = statistic.l_mode()
l_stdev = statistic.l_stdev()
l_min = statistic.l_min()
l_max = statistic.l_max()

a_mean = statistic.a_mean()
a_median = statistic.a_median()
a_mode = statistic.a_mode()
a_stdev = statistic.a_stdev()
a_min = statistic.a_min()
a_max = statistic.a_max()

b_mean = statistic.b_mean()
b_median = statistic.b_median()
b_mode = statistic.b_mode()
b_stdev = statistic.b_stdev()
b_min = statistic.b_min()
b_max = statistic.b_max()


# Print to serial monitor for debugging (line by line)
print("Grayscale Stats:")
print("Mean:", mean_value)
print("Median:", median_value)
print("Mode:", mode_value)
print("Stdev:", stdev_value)
print("Min:", min_value)
print("Max:", max_value)
print("-" \* 40)

print("LAB - L Channel:")
print("L_mean:", l_mean)
print("L_median:", l_median)
print("L_mode:", l_mode)
print("L_stdev:", l_stdev)
print("L_min:", l_min)
print("L_max:", l_max)
print("-" \* 40)

print("LAB - A Channel:")
print("A_mean:", a_mean)
print("A_median:", a_median)
print("A_mode:", a_mode)
print("A_stdev:", a_stdev)
print("A_min:", a_min)
print("A_max:", a_max)
print("-" \* 40)

print("LAB - B Channel:")
print("B_mean:", b_mean)
print("B_median:", b_median)
print("B_mode:", b_mode)
print("B_stdev:", b_stdev)
print("B_min:", b_min)
print("B_max:", b_max)
print("-" \* 40)
import sensor, image, time

# Initialize camera
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)

filename = "hist_stats.csv"
with open(filename, "w") as f:
    # write header row
    f.write("channel,mean,median,mode,stdev,min,max\n")

while True:
    img = sensor.snapshot()
    # get histogram and statistics
    hist = img.get_histogram()
    stat = hist.get_statistics()
    
    # build CSV lines for L, A, B channels
    rows = [
      ("l",    stat.l_mean(), stat.l_median(), stat.l_mode(), stat.l_stdev(), stat.l_min(), stat.l_max()),
      ("a",    stat.a_mean(), stat.a_median(), stat.a_mode(), stat.a_stdev(), stat.a_min(), stat.a_max()),
      ("b",    stat.b_mean(), stat.b_median(), stat.b_mode(), stat.b_stdev(), stat.b_min(), stat.b_max()),
    ]
    
    with open(filename, "a") as f:
        for chan, mean, med, mode, sd, mn, mx in rows:
            f.write("{},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f},{:.2f}\n".format(
                chan, mean, med, mode, sd, mn, mx))
    
    print("Wrote stats to", filename)
    time.sleep(2)  # adjust as needed

Please use ChatGPT to answer such questions; writing a CSV file is just basic Python.

Thank you sir!