Visualizzazione post con etichetta python. Mostra tutti i post
Visualizzazione post con etichetta python. Mostra tutti i post

martedì 1 luglio 2014

Scanning transmittance NIR imaging system using LEDs matrix and Python.

NIR transmittance imaging is a techniques used in cultural heritage diagnostic. Similarly to reflectography, NIR transmittance imaging used a NIR source and a sensor able to detects these wavelengths. However in NIR transmittance imaging the painting is between the NIR source and the detector.  Normally a Halogen lamp is used but this has several disadvantages:

  • Warms the painting. 
  • Some light is reflected and diffused from the surrounding objects, and so is quite difficult to obtain a pure transmittance image.
  • Not so easy to handle.
Using a little LED matrix can resolve all this problems adding further advantages:

  • LEDs produce narrow light spectra, so generally you don't need a IR-PASS filter in your camera, this means that theoretically you could generally achieve a better MTF.
  • There are available different type of IR LEDs operating in different NIR region (940 nm, 850 nm etc. etc.) so basically you can build cheaply a multispectral imaging system. Or even multispectral polarized imaging system... 
There is only a big problem: how can you analyze a large surface using a little LEDs lamp? I have tried to solve it using a simple algorithm.

NIR transmittance imaging using a little NIR LEDs  matrix  at 940 nm .



Suppose you are in a completely dark room recording a video, the painting appear black in the area where is not lighted, moving the lamp when a different area is lighted the pixel values increase. So when you scan the painting surface with your lamp, keeping your lamp at a fixed distance basically what you need is to substitute the unlighted with the lighted ones. So the algorithm compare the same pixel and keep the highest value. You can compare the same pixel of an acquisition with the next acquisition or you can set a threshold (something like: if pixel>background and pixel < 255: result_pixel=pixel). The first solution is quite simple to achieve using Python, keeping all the snapshot in a folder you can use this script:
from PIL import Image,ImageChops
from os import listdir,sep
def FolderElab():
    foldername=r'/home/giacomo/Documents/Foldername'
    folder=listdir(foldername)
    fused=Image.open(foldername+sep+folder[0])
    for i in folder:
        orig=Image.open(foldername+sep+i)
        fused=ImageChops.lighter(orig,fused)        
    return fused 

I've used an old 0.31MP webcam after removing the ICF and substituting it with a piece of floppydisk (as mentioned earlier you can use it without an IR-PASS filter, however sometimes could be easier install one to operate in lit rooms). This is the first result, merging all the images.

Resulting image made of different images as the previous one.

Python script to collect LEDs matrix data.

Analyzing a LEDs matrix one LED by one reporting data in a spreadsheet and then analyzing it could be very time consuming. A simple python script can speed up a little bit the tests, organizing the data and plotting the result. The resulting image, automatically generated using matplotlib, show the mean and the standard deviation for every LED and resistance in the circuit. You can specify the number of rows and columns depending on your matrix.

Code is hosted on gitHub.

lunedì 16 giugno 2014

Python Gimp plugin to compare multi-spectral imaging images

When you have many images of the same subjects in different spectral bands some times you want to compare them. In this case the simple things to do is to juxtapose them showing local differences with a marker (an arrow or a circle) that indicates what is visible in one spectral band and not in the other. That's could be a quite tedious process, I have developed a Python Gimp plug-in to perform this task quickly. Basically you can load the images as different layers and juxtapose them. Furthermore you can add a transparent layer that can be duplicated over the others layer, so you can put in it markers that will be automatically superimposed over the same area in the others layer.  This video shows how it works.


Plugi-in are hosted on GitHub

giovedì 5 giugno 2014

Analyzing and reporting spectra: Python to LaTeX Raman spectra analyzer

Most of the chemists don't know much about programming indeed mathematicians, physicists and of course IT engineers are familiar with Python and LaTeX and so they already know about their great potential. Analytical chemists use Excel (or software design for them like OriginPro) and Word processors, these are indeed great tools for data analysis but sometimes are not so efficient and convenient for repeated tasks. In academia these tools are very common (just look at some academic posters to recognize who uses Excel or OriginPro) and so they teach you to use them thinking that you will analyze only o bunch of spectra for the publication and then you will move to another project and another instrument. In industry many times you begin your career with an instrument and you finish it with him, so reduce time to analyze and report his spectrum is a great advantage. LIS (LIMS) are normally used to organize better data, but indeed sometimes we only need something between these two antipodes, I will show you my workflow using Python and LaTeX for analyzing and reporting Raman spectra in in this post. I begin to deal with spectra when I was a Raman spectroscopist this was a typical spectrum saved as .csv I used to work with:
How my spectra looked like

I needed only the DATA to plot them so I have to avoid other things like parameters and additional informations. I learnt how to create a filter with Origin to skip the first rows but, how can I avoid to import the peaks found by the spectrometer's software? This was a more difficult task for Origin, even the peaks analyzer was a very slow process. Then when I finally insert the image in the Word file I used to found errors and so I had to repeat the whole process! So I begin to develop my own tools to analyze the spectra (found peaks...) and report them directly in TeX. I posted my work in GitHub basically it consists in a general python script to analyze peaks in Raman spectra (positive peaks) and the filter to import spectra form Xantus-1 spectrometer as an example of how everyone can make his own filter to import their own spectra. When you obtain the matplotlib image you can save it but I think that exporting it as LaTeX code is more interesting and lead to many advantages. These are some of the advantages in using the whole methods:
  • Very fast importing methods
  • Print a pice of TeX code that lead to images with publication resolution and all the advantages of LaTeX.
  • If you need you can edit the LaTeX code and so the image directly in a second step.
  • You can add directly into the caption the acquisition parameters for every spectrum, extracting them directly from the spectrum file.
This video shows how it works: