Sunday, August 25, 2019

smorgasbord of programming tips and ideas - OpenCV etc

Here is a collection of links for various implementations of OpenCV-based medical imaging software -

  1. CUDA and OpenCV - just need to compile OpenCV on a machine with a Cuda compatible graphics card. My old machines have cards which are only supported by older versions of the CUDA SDK. Can expect a 180% speedup as per some papers.
    https://answers.opencv.org/question/62955/cudadft-speed-issues-too-slow/
    indicates that the Matlab times were wrong.

    https://opencv.org/cuda/
    https://developer.nvidia.com/cuda-gpus and legacy gpus at
    https://developer.nvidia.com/cuda-legacy-gpus
    https://developer.nvidia.com/cuda-toolkit-archive and supported GPUs at
    https://en.wikipedia.org/wiki/CUDA#GPUs_supported

  2. A nice class to capture from Spinnaker SDK to OpenCV Mat, running in a separate thread - https://github.molgen.mpg.de/MPIBR/SpinnakerCapture
  3. Writing an OpenCV Mat file to a binary format on disk - Lots of options mentioned at
    https://answers.opencv.org/question/6414/cvmat-serialization-to-binary-file/
  4. I had a thought of trying DICOM writing. But it turned out to be quite complicated to write DICOM, since so many parameters need to be initialized.
    https://stackoverflow.com/questions/36379637/read-dicom-in-c-and-convert-to-opencv
    https://github.com/marcinwol/dcmtk-basic-example
    https://itk.org/ITKSoftwareGuide/html/Book2/ITKSoftwareGuide-Book2ch1.html#x7-240001.12
    https://www.researchgate.net/post/ITK_vs_OpenCV

  5. Some tips on exporting from Octave to OBJ and STL formats - commonly used in 3D printers - https://alexbjorkholm.wordpress.com/2016/10/07/exporting-3d-objects-from-octave/
  6. Plotting using slice in Matlab -
    https://stackoverflow.com/questions/35549733/how-can-i-plot-several-2d-image-in-a-stack-style-in-matlab

Ubuntu 18.04 screen issue for File Open dialog boxes, scrollbars [fixed]

After a new install of Ubuntu 18.04, had a funny problem where Geany File Open would lead to the application being greyed out, as if the dialog box had opened, but no dialog box being visible on screen. Googling found the answer in this thread, https://ubuntuforums.org/showthread.php?t=2395250

Apparently, 18.04 configures 2 screens even when there is only one screen. Fix was to go to Settings and change to one screen.

Still there is a problem with Geany's scrollbars - the scrollbars don't redraw after scrolling, leading to the whole scrollbar becoming an orange colour instead of just the orange scrollbar handle. My initial installation was a minimal Ubuntu desktop. Later, installing unity did not help.

Edit: This post seems to have the solution - installing a newer version of Geany from the ppa solved the scrollbar issue. 
sudo add-apt-repository ppa:geany-dev/ppa 
sudo apt-get update 
sudo apt-get install geany geany-plugins-common

Friday, August 23, 2019

Visual Studio specifics of OpenCV project with Spinnaker SDK

This is linked to my previous post about the QHY -> Spinnaker implementation, the nitty-gritties of Visual Studio project changes required under Windows.

  1. The SDK and examples installed by default under Program Files in C:. If Visual Studio opens the examples there, it complains that the directory is not writable. So, make a copy, including the entire spinnaker directory - not just the src folder, since the include paths in the examples are set as ..\..\include and similarly for libs.
  2. In my case, since I had VS2017, I had to install the VS2015 toolset as mentioned in my previous post, and when opening the solution, choose the "No Upgrade" option for these projects.
  3. Once these steps were done, I could compile and run the examples. But for my project, I had to create a new project, choose the V140 toolset, add libraries, add include and lib paths, and also prevent errors due to my use of sprintf.


  4. One way would have been to make a copy of the common properties file which I had made earlier, make modifications to it, and add that to the project. In this case, I just made specific changes to the Release configuration of this particular project.
  5. Additional include dir in C/C++ -> General, Additional Include Directories would be a better place to add it than in this screenshot.


  6. Linker -> Input -> Additional Dependencies


  7. Additional Library Directories.

I would probably need to copy the dlls manually, or follow the post build steps mentioned at this earlier post

implementing opencv project with Spinnaker SDK for Point Grey FLIR cameras

This post documents my learning curve with migrating code written for the SDK for QHY cameras to the Spinnaker SDK for Point Grey FLIR cameras.

  1. On both Windows and Linux, I had to make changes to my build platform. On Linux, Ubuntu 18.04 was a supported platform, so I installed 18.04 onto a spare partition. On Windows, I was using Visual Studio 2017, and had to install the VS2015 toolset. 
  2. On Linux, even though the spinnaker examples had large and complex makefiles, the sdk had put the include and lib files in the default locations, so I just had to modify my CMakeLists.txt file minimally -


  3. On Windows, there was a much more elaborate series of steps, which I will put in a separate post.
  4. The simpler parts of implementation, like changing exposure time, gain etc could be done in a fairly straight-forward way. Unfortunately, there was no example for continuous capture with live display. ptgrey.com being redirected to flir.com, the examples obtained on googling couldn't be directly found. Finally, found this, which indicated that StreamBuffer handling had to be changed. Looking for this specific information, found the documentation at this page, which mentions its potential use in live view scenarios. Putting that in place, then Begin Acquisition and End Acquisition could be implemented outside the capture loop, and the frame rate improved.  
Edit: Here is a dump of the nodes I changed, full code is at https://github.com/hn-88/FDOCT/blob/master/BscanFFTspin.cpp (spinnaker documentation talks about the details of the nodes, and the example code helps).

(Gain is in dB)

In terms of nodes,
AcquisitionControl -> AcquisitionMode -> Continuous
AcquisitionControl -> ExposureAuto -> Off
AcquisitionControl -> ExposureTime -> 1000
AnalogControl -> GainAuto -> Off
AnalogControl -> Gain -> 0.0
AnalogControl -> Gamma -> 1.0
ImageFormatControl -> Width -> 1280
ImageFormatControl -> Height -> 960
ImageFormatControl -> OffsetX -> 0
ImageFormatControl -> OffsetY -> 0
ImageFormatControl -> PixelFormat -> Mono8
ImageFormatControl -> PixelFormat -> Mono16
ImageFormatControl -> AdcBitDepth -> Bit12
ImageFormatControl -> AdcBitDepth -> Bit10
BufferHandlingControl -> StreamBufferHandlingMode -> NewestOnly

Using Displayspin, on my machine, I got
71 fps for Bit12 Mono8
34 fps for Bit12 Mono16 (converted to Mono8 to display on screen)
71 fps for Bit10 Mono8

So, probably we can always use Bit12, since it does not seem to have much of a speed penalty.

And a small note on a mistake I made - initially I got the error  
Spinnaker: Can't clear a camera because something still holds a reference to the camera [-1004].
Putting pCam = nullptr; before camList.Clear(); removes this error.




Wednesday, August 21, 2019

IP address, MTU setting on Linux for FLIR Point Grey GigE cameras

It looks like my hardware does not support jumbo frames - https://linuxconfig.org/how-to-enable-jumbo-frames-in-linux

Even after setting up a larger MTU with ifconfig,
https://www.flir.com/support-center/iis/machine-vision/knowledge-base/lost-ethernet-data-packets-on-linux-systems/
the MTU reverted back to 1500. So, probably hardware or driver limited.

Also, Network Manager then complains that the interface is not managed,
https://askubuntu.com/questions/71159/network-manager-says-device-not-managed

But the camera works as long as the IP address is set correctly. According to the knowledge base article at
https://www.flir.in/support-center/iis/machine-vision/knowledge-base/setting-an-ip-address-for-a-gige-camera-to-be-recognized-in-linux/

setting it anywhere in the 169.254.x.y range with a 255.255.0.0 netmask should do the trick - link local address (LLA).

More info about our camera is at
https://www.flir.com/products/blackfly-s-gige/?model=BFS-PGE-16S2M-CS

https://www.flir.com/support-center/iis/machine-vision/knowledge-base/technical-documentation-bfs-gige/

Getting Started Manual which talks about Windows installation
Technical Reference which gives frame rates, pin-outs etc

Edit - found this page which has a good explanation of the bandwidth used, effect of lower MTU (slightly higher CPU usage), multiple GigE cameras and so on.

Sunday, August 18, 2019

Remmina, RDP and sound

For convenience, I was trying out using Sound Forge and other audio editing software on my laptop, after logging in with a Remote Desktop Connection using Remmina as the client. With the default setting, Sound Forge etc would complain that there is no suitable audio output device. Googling, found that RDP has various options - taking sound to remote client, leaving the sound on local machine, or no sound at all. Remmina default was no sound. Tried bringing sound to local device, but that did not work for DirectSound, since I'm on Linux Mint. Changing it to leave at local, SF etc would work properly.

image.png

tried ubuntu install from mint

 https://help.ubuntu.com/lts/installation-guide/i386/apds04.html

but apt update after chroot seemed to be taking the values from sylvia.

So, simpler to just use a bootable usb to create and install the new ubuntu partition.

Wednesday, August 14, 2019

moving mailing list to google groups

The process begun in Dec 2017 was completed now, in mid-Aug. Transitioning our mailing list with 60k subscribers to google groups. Had to move slowly due to the limits set on directly adding users, 100 per day per group, and only in batches of 10 email ids per addition.

Had asked PB to unconfirm users who have been moved to groups instead of unsubscribe, because unsubscribe will send them a mail saying they have been unsubscribed.

Points to note: 
(a) any user you add will get an email
(b) for a strictly mailing list only functionality, remove posting permissions for everyone except owner and manager, under Permissions -> Posting Permissions, like
Inline image 1

Google groups does handle bounces, and it is advisable to remove ids which bounce -
https://productforums.google.com/forum/#!msg/apps/5PCc-uDVowo/S4A4YPnnzXcJ

(Automating signup though google docs or google forms or something would be great. An example is given here,
https://sites.google.com/site/sandorexampleclub/how-to/step-11
  PS wrote his own implementation which is on script.google.com as a GSuite script, I think.)

Tuesday, August 13, 2019

easy 3D volumetric rendering with imagej

ImageJ, or its full-featured version Fiji, makes it easy to create volumetric renderings and flythroughs using the 3D Viewer plugin. Plugins are installed by just copying the jar file into the plugins folder, found using locate on my system at ~/.imagej/plugins. Multi-layer tifs or gifs can be used as input. Gimp cannot create multilayer tifs, but it can create gif animations, which are sufficient. Open as layers in Gimp to select multiple layer files, save as animated gif, open in ImageJ.

Inside ImageJ, Image -> Scale allows setting of proper scale. Making one axis longer etc. The Image menu also has Crop, Duplicate, Transform and Zoom. Image -> Adjust has Threshold, Brightness/Contrast, etc. Then, Plugins -> 3D Viewer.

The 3D Viewer plugin has nice recording features, with a built-in 'Record 360 degree rotation' under its View menu. It opens another Movie window, which can then be exported using ImageJ's File -> Save as and choosing Gif.

ezgif.com helps to crop and compress gifs easily.



Monday, August 12, 2019

creating animated gif or video from Octave

My Octave install does not have the campos function, but the latest version seems to have it. So, this could be useful in the future.

This post has some good guidelines - using
set(0, 'defaultfigurevisible', 'off');
to speed up things, and using print to save as png. Then using mencoder etc to create the movie.
 mencoder mf://output/*.png -mf w=640:h=480:fps=25:type=png -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:trell -oac copy -o output.avi

Sunday, August 11, 2019

bulk renaming from commandline on linux

Using the rename tool,
An example is
rename 's/\.html$/\.php/' *.html
From
we see that a normal string substitution would be like
s/dog/cat/

rename -n 's/SV_2019_08_10_PM_SOME_LONG_NAME/SV_2019_08_10_SHORTNAME/' SV*mp3
shows what it will do, and -v will actually do it and also display the result.