Team:TAS Taipei/Software

fork me

Software

Software Mechanics

Overview

Read the code described below here: https://github.com/usymmij/TAS_Taipei/releases/tag/1.0

VisulapH provides an inexpensive, fast, accurate, and precise result for pH measurement. Common pH probes require proper storage and maintenance, and large ones cannot be used for solutions of small volume. In that case, smaller, more expensive probes are needed. Indicator solutions are useful, but they do not provide very precise or accurate results when used with the human eye. To reach the same level of precision as probes, very expensive equipment is needed. VisualpH allows the precise measurement of pH without any of that expensive equipment, just a computer, camera, and pH indicator.
VisualpH works by first receiving a user defined mask that defines where the solutions are inside the image or video. It then compiles the hue data, and converts it into pH data.

Figure 1: The process of how VisualpH works: masking, averaging, linearization and plotting

Color and Light

Figure 2: Spectral Sensitivities for Humans (BenRG, 2009)

Visible light itself is a specific spectrum of electromagnetic radiation. Our perception of colour is an abstraction of this data, and we have built our computers to perceive this data in a similar way. However, this also means that our concept of colour is not entirely based on the physical properties of it. Fortunately, HSV (Hue, Saturation, Value) colourspace allows us to transform this representation of colour into something closer to what light really is (figure 3). Since our perception of colour comes from 3 different cones, every perceivable colour can be expressed in a 1x3 matrix.3 While HSV is also represented in a 1x3 matrix, rather than being representations of each RGB component, the values in the matrices represent the hue, saturation,and value of the colour.

Figure 3: HSV Colourspace (SharkD, n.d.)

Although HSV is not an exact representation of the physical properties of light, it makes it much easier to make assumptions and correlations about the origin of the light source. Value represents the brightness of the pixel. When a large amount of hits a certain pixel, it will have a high value. The saturation shows the intensity of the colour, relative to pure white, and the hue correlates with the wavelength of the colour. However, it should be noted that this is not entirely correct, as the purplish-pink hues don’t correspond with a specific wavelength, but a certain combination of r, g, b, values that appear unique to the human eye. VisualpH uses the hue of the image, and not saturation or brightness, and in doing so most of errors that are caused lighting differences will not factor into the result, as long as the light source used in the image emits light of most wavelengths in the visible spectrum.

Masking

VisualpH takes an input of a set of input masks, as well as the original image/video. A mask is an image matrix that is comprised of only 0’s (black) and 1’s (white). By multiplying a mask with the original video frame or image, you get a new image that is black everywhere except for where the mask specified, where the image pixels retain their original values. An example with a 3 × 3 image and mask is shown in figure 4. Using a mask, we can isolate the pixels where the solution is located.

matrix

Figure 4: Masking a 3 x 3 image (from left to right: image, mask, result)

Hue to pH

After isolating the pixels of each solution, the average hue can be calculated, From this average hue, the following equation can be used to convert hue to pH.

equation

pH is the logarithm of the concentration of hydrogen ions, so, assuming that the hue is relatively linear in correlation with that concentration this equation models the relationship somewhat well. We use a logarithmic function for this relation between hue and pH because of the mutual relationship with the concentration. Since the hue of the solution increases from red to orange, but pH decreases, the inverse of the hue is used instead. The h variable represents pH, while x represents the hue. While the overall shape of the function matches the shape of the relation, scaling factors and offsets are needed to make the equation match. The constant a is a linear scaling factor, while b scales the curvature of the logarithm. Both c and d are offsets, in the x and y direction respectively.

The constants for this equation are empirically derived, and work for phenol red. If other indicators are used, then the constants would need to be tuned for those indicators as the range of the hues are different.

The calibration process is relatively simple. A standard of multiple pH values with a certain indicator is created, and a photo is taken of it. It is fed into the tool, but the hue data is collected rather than the pH data. Then, the pH is correlated with the hue data and regressed using the function above. Finally, the a, b, c, d values in the stats.py file are adjusted to match those in the regression.

Usage in Our Project

We use visualpH in our project extensively as the main method of quantifying the data we collect. Our models and even experimental procedures are developed around recording videos of the pH transformation over time and feeding them into the tool for analyses. We have used the tool for our sensitivity test, specificity tests, as well as all of our modelling experiments examining the effect of certain factors on the reaction.

Potential/Future Applications

VisualpH can be used not only with pH indicators, but also for the quantification of any data that correlates with colour and hue. For example, activation of mCherry, or GFP could be quantified using a similar technique, which could be additional data to build upon in an experiment. It could also be used in place of spectrophotometers in many cases, as it is easier, cheaper, and allows for measurements of other properties to occur at the same time. However, it does not provide the exact same measurements as a spectrophotometer does, and is not as precise, so it is not meant to completely replace it.

This tool expands a lot of possibilities for virus testing, as it could be included in a testing kit to provide analyses on testing results. Python provides a lot of cross compatibility, so the tool could be adapted to be put in a multitude of different devices, so that testing could be done entirely at home. Imagine, if someone could remotely request a test, have it delivered to them, and then self administer and confirm test results within a few hours. The user would be able to examine their test results just by taking a photo of it, and a mobile app will immediately return the details of the test, like pH and viral load. Furthermore, given biological details about the user, details such as symptoms or age, the app could also potentially predict future events and how severe the disease will be for that specific user. Machine learning networks have been able to do this in a similar way for diseases like the Influenza (Yang et al., 2020). The benefits of this prediction, could be very beneficial to the prevention and response to pandemics, as patients who will in the end only experience benign effects do not need to overload the medical response system. On the other hand, the patients who are most seriously affected could be given more attention earlier, hopefully making a recovery for them faster and less expensive. When a disease progresses, it gets harder to treat, but giving early intervention to those who may end up very ill could save a lot of medical resources and expenses.

References

BenRG. (2009). The Stockman and Sharpe (2000) 2° cone fundamentals, as found here, plotted against the most accurate sRGB spectrum I could manage. Now in SVG. Own work. https://commons.wikimedia.org/wiki/File:Cone-fundamentals-with-srgb-spectrum.svg

SharkD. (n.d.). English: The HSV color model mapped to a cylinder. POV-Ray source is available from the POV-Ray Object Collection. Own work. Source-code available at the POV-Ray Object Collection. Retrieved October 25, 2020, from https://commons.wikimedia.org/wiki/File:HSV_color_solid_cylinder.png

Yang, C.-T., Chen, Y.-A., Chan, Y.-W., Lee, C.-L., Tsan, Y.-T., Chan, W.-C., & Liu, P.-Y. (2020). Influenza-like illness prediction using a long short-term memory deep learning model with multiple open data sources. The Journal of Supercomputing, 76(12), 9303–9329. https://doi.org/10.1007/s11227-020-03182-5