A plug-in for real-time histograms in Scanbox

A Scanbox user requested to add the ability to display a histogram of image values in real-time.

Here, I use a the general mechanism of memory mapped files in Scanbox to show how easy is to add such “plug-ins” to provide additional functionality to the system.

The Matlab code necessary to add real-time histograms in Scanbox is very concise and commented below:


% Display real-time histogram in Scanbox

% Prepare the figure for display

close all;
h=figure(1);
h.MenuBar = 'None';
h.NumberTitle = 'off';
h.Name = 'Scanbox ChA real time histogram';
drawnow;

% Open memory-mapped file to have access to image stream

mmfile = memmapfile('scanbox.mmap','Writable',true, ...
'Format', { 'int16' [1 16] 'header' } , 'Repeat', 1);

% Some parameters for display

nframes = 5; % how often we want to display histogram
nbins = 128; % how many bins in the histogram
margin = 20; % size of the margins to crop around the central ROI

% Start processing until Scanbox stops...

k = 0;

flag = 1; % Necessary to set image format the first time

while(true)

   while(mmfile.Data.header(1)<0) % wait for a new frame to arrive
      if(mmfile.Data.header(1) == -2) % exit if Scanbox finished acquiring data
         return;
      end
   end

   if(flag) % First time? Format chA according to lines/columns in header
      mmfile.Format = {'int16' [1 16] 'header' ; ...
      'uint16' double([mmfile.Data.header(2) mmfile.Data.header(3)]) 'chA'};
      flag = 0;
   end

   if(mod(k,nframes==0)) % update histogram every k frames
      imhist(intmax('uint16')-mmfile.Data.chA(margin:end-margin,margin:end-margin),nbins); % display histogram
      drawnow;
   end

   mmfile.Data.header(1) = -1; % signal Scanbox that frame has been consumed!
   k = k+1;
end

clear(mmfile) % close memory-mapped file

That’s all…

Check the mmap configuration variable is 1 and start Scanbox.

Open a new Matlab process and run the script above.

Start scanning and you should see a real-time histogram being displayed, each of the frames will look like this:

scanboxhist

You can add other features to this script, such as displaying the number of pixels saturated, and so on.

Importantly, notice that the memory-mapped mechanism can easily be accessed by plug-ins written in other languages, like Python, so you are not bound to use Matlab.

If you develop useful real-time plug-ins for Scanbox let us know and we will include them as part of the future releases.