Color Histogram Data Extraction

I am trying to write a macro to extract the color histogram data from each frame in a video and save it to a .csv file. The video is recorded as a .MOV, but I have converted it to an uncompressed .avi using FFMPEG’s command:

ffmpeg -i DSC_0161.MOV -pix_fmt nv12 -f avi -vcodec rawvideo DSC_0161.avi

I can import the avi that ffmpeg produces using the File>Import>AVI… tool. As a curious aside, it is not readable by bio-formats (the error is “sorry, there was an error during import”), and I’m struggling to find information on how to make an avi that works for bio-formats.

The real problem I am facing is how to extract the RGB histogram from each frame. For an individual extracted image I have written a macro that converts the RGB Color image to an RGB stack, and then traverses the channels extracting data manually. If I try to convert the avi frames to RGB stack I run out of memory real fast so I was hoping to use the color histogram function to extract the RGB values from each frame. I am failing hard at finding a script or the functions that will allow me to extract the color histogram data in the same way that one could manually click “Analyze>Color Histogram”, click the “list” button, and then copy and paste the values from the resulting spreadsheet that is created into a text file.

Any and all advice here would be very appreciated, right from the start on how to get bio-formats to properly import the avi, all the way down to how to call the “list” button from a macro.

Below is my little script for traversing the histogram of an image. I can’t remember where I got it from at this point, but many thanks to whoever the original author was!

function analyzeHistogramData(output, file_name, output_name, colour) {
	
  nBins = 256;
  run("Clear Results");
  row = 0;
  getHistogram(values, counts, nBins);
  
  for (i=0; i<nBins; i++) {
      setResult("Intensity", row, values[i]);
      setResult(colour + " Count", row, counts[i]);
      row++;
   }
   
  updateResults();
  saveAs("Results", output + "/" + "Histogram_" + file_name + "_" + output_name + ".csv");
}

Thank you!

Good day,

you may have a look at the following maco code:
https://imagej.nih.gov/ij/macros/RGB_Histogram.txt

Of course you must adapt it to your needs by using the appropriate macro functions listed here:
https://imagej.nih.gov/ij/developer/macro/functions.html

Have success

Herbie

Hi,

just as an additional explanation to Herbies’s post, the trick for measuring only one color in RGB images is setRGBWeights(1,0,0) for the red channel, etc.

Concerning the ffmpeg conversion, there are several pixel formats for uncompressed video.
ImageJ natively reads the following (only color formats are listed here):

  • Plain RGB (24 or 32 bits/pixel), AYUV (largest files with 24 or 32 bits/pixel, color information for each pixel)
  • UYVY=Y422, cyuv, V422, YUY2=YUNV=YUYV, YVYU (common color information “U”, “V” for two adjacent pixels in a row, which results in a file size of 16 bits/pixel plus header). These formats are typical for frame grabbers.
  • I420=IYUV, YV12, NV12, NV21 (common color information for groups of 2x2 pixels, resulting in 12 bits/pixel). These formats are typical for webcams.
    All of these have brightness information (“Y”) for each pixel.
    Compressed video formats have color information for 2x2 pixel groups only (“chroma subsampling”), so for minimal file size the best option is choosing one of the last category.

I did not find which war video pixel formats can be read by BioFormats. I am pretty sure it understands plain RGB, but tht gives files almost twice as large as I420, NV12, etc.

To see which of these are supported by ffmpeg, type ffmpeg -pix_fmts. Plain RGB is ffmpeg’s rgb24, and I420 is ffmpeg’s yuv420p. The latter is a bit more popular than NV12, so you might give it a try (I don’t have BioFormats on the computer where I am typing, so I can’t try).

That’s great info Schmid! Thank you.

The ffmpeg command has helped me figure out video conversions.

I have also figured out the issues I was facing with Bio-Formats. In order for Bio-Formats to load a MOV, you must have three things:

  • ImageJ running off of 32 bit Java
  • QuickTime and QuickTimeJava installed
  • A copy of QTJava.zip in Java’s jre\lib\ext directory

Huge shoutout to Mark Hiner over at Fiji.sc for the in depth response of this bug report: http://fiji.sc/bug/1063.html, the summary of which is below.

Hello,

>Please invoke the 32-bit JVM (-d32) to utilize QTJava functionality.

If you click on the status bar (pictured here[1]) does your Fiji report a 64-bit version of Java? If so, you can get a 32-bit version of java (the x86 downloads[2]) and use that to launch Fiji[3]. Hopefully that will solve your issues.

If not, you can also try changing the QuickTime settings for Bio-Formats itself[4]. However, I suspect both the QuickTime readers are already being checked here, based on your error output.

If you're still having problems, the Bio-Formats team maintains their own bug reporting system[5]. They usually can take a look at your dataset and determine what's wrong.

[1] http://fiji.sc/File:MemoryStatus.png
[2] http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html
[3] http://fiji.sc/FAQ#How_do_I_launch_ImageJ_with_a_different_version_of_Java.3F
[4] http://fiji.sc/Bio-formats#Bio-Formats_Plugins_Configuration
[5] https://www.openmicroscopy.org/site/support/bio-formats5.1/about/bug-reporting.html