Functions and types of ThermovisorImages

ThermovisorImages.ThermovisorImagesModule
ThermovisorData is a package designed to process thermal images stored as matrices.

Each element of thermal image represents a temperature value. The package enables users to load images from files, calculate temperature distributions, compute statistical analyses for temperatures along specified lines and recalculate the temperature by taking into account the emissivity and spectral range of thermal imaging camera.

Thermal image can be loaded using read_temperature_file. User defined matrix can be wrapped in RescaledImage which simple maps the values to [0,1] interval. After that, distinct areas (relative to their surroundings,like the most heated regions within the scene) can be fitted to regions of interest (ROIs) using fit_all_patterns. Temperature distribution along the specified lined within each ROI can be obtained, and average radial and There is also possible to evaluate the averaged statistics over the fitted ROI's using CentredObjCollectionStat and recalculate the temperature for a new emissivity value of the whole image or it's region using recalculate_with_new_emissivity!

source
Core.TypeMethod
(::Type{T})() where T<:CentredObj

Empty object constructor

source
ThermovisorImages.CentredObjType
`CentredObj` is a sort of region of interest (ROI) marker object with coordinates and dimensions.

CentredObj has centre coordinates and dimensions, object's center can be anywhere with respect to the image indices. ROI also has one or more size parameters (in pixels) coordinates of centre are equal to CartesianIndices, first element is the row index, the second element is the column index!! (y and x coordinate) This is opposite to the ImageDraw, where first Point coordinate corresponds to the column index and the second one to the row index. CentredObj can also be used as for indexing image[c] - returns all elements of image within c, image[c]=x sets all elements of image to the values of x, x firstindex should be 1. CentredObj can also be used to set all image points within the ROI to a single value. e.g. image[c] = 30

By default we have three CentredObj types in package:

CircleObj, SquareObj and RectangleObj

To impement CentredObj abstraction one needs to implement:

side - returns what is assumed to be the single number characterisation of the dimensions (by default the maximum value of dimensions(c) is taken)

area - returns the CentredObj area, used in statistics calculation and in default two objs compirason see isless(c1::CentredObj,c2::CentredObj)

perimeter - Object's perimeter

Base.size - should always return the minimum size of frame which wrappes the centred obj, e.g. for circle it is [diameter,diemater]

is_within - function to check if inds are within the CentredObj

line_within_mask - function to check if all line points are within the CentredObj

fill_x0! - function to fill the optimization starting vector during CentredObj fitting the image

convert_to_drawable fucntion to convert the CentredObj to a drawable obj for ImageDraw

parnumber function which returns the number of parameters

source
ThermovisorImages.CentredObjCollectionStatType
CentredObjCollectionStat

Calculates the following statistics on CentredObjs collection:

minimal, maximal, mean and standard deviation of sides,areas and perimeters of all CentredObj objects in the collection v.

source
ThermovisorImages.DistributionStatisticsType
DistributionStatistics

Basic descriptive statistics (mean and standard deviation) on temeprature distribution of value for a given coordinate, D is the matrix of distribution, where each column corresponds to sample, thus rows number of D should be the same as the length of coordinate, columns number is the number of samples.

source
ThermovisorImages.FilteredImageType
    Type to store image with filtered temperature region

Fields:

full - filtered rescaled image of the same size as the input with all pixels which are not the part of the pattern with label value

region_indices - cartesian indices of the pattern in the input image

reduced - image of reduced size where all not-inpatter pixels removed (the scaling of this image is the same as of the input imag.initial see RescaledImage type )

reduced_flag - bitmatrix or Matrix{Bool} version of (reduced image)

source
ThermovisorImages.MarkeredImageType
Special type to work with segmentated image, stored the matrix of indices and a vector of vectors of

patterns coordinates. Pattern can be sorted by area. And accessed by direct indexing into the MarkeredImage object.

source
ThermovisorImages.RescaledImageType
    RescaledImage - structure stores the image data mapped to region  [0,1]

Fields:

initial - initial image before rescaling

sz - size of the image

min - minimum value

max - maximum value

im - image with all values from 0 to 1

source
Base.getindexMethod
Base.getindex(img::AbstractMatrix,c::CentredObj)

CentredObj can be used for matrix indexing, image[centred_object] - returns the vector of temperatures of all points of image lying within the centred_object of CentredObj

source
Base.islessMethod
Base.isless(c1::CentredObj,c2::CentredObj)

By default centred objects are compared by their areas, thus the vector of centred objects can be sorted

source
Base.setindex!Method
Base.setindex!(img::Matrix,x::Array,c::CentredObj)

img[c]=x assignes all x elements to the elements of img with indices lying within the CentredObj c

source
Base.setindex!Method
Base.setindex!(img::Matrix{T},x::Number,c::CentredObj) where T

Setting all elements within the CentredObj to a single value

source
Base.sizeMethod
Base.size(m::MarkeredImage)

The image size in pixels

source
ThermovisorImages._inbounds_flagMethod
_inbounds_flag(L,D,max_length,min_length)

Unsafe version of check! number of rows in D should be the same as the number of elements in L Returns Bool flag of all row not containing NaN's and lying within the minlength to maxlength range

source
ThermovisorImages.along_line_distributionMethod
along_line_distribution(img::AbstractMatrix{T},x0,y0,x1,y1) where T

Function evaluates matrix values distribution along the line specified by two coordinates, img - input image returns the tuple of two vectors: coordinates and values see ImageDraw.bresenham for details of finding the points of the line

returns points - vector of coordinates along the line distrib - distribution

source
ThermovisorImages.along_line_distribution_plotMethod
along_line_distribution_plot(along_line_length,along_line_distribution;
                                    length_scaler::Float64=1.0,
                                    is_centered::Bool=true,
                                    kwargs...)

Plots temperature distribution along the line along_line_length - coordinates, along_line_distribution - values of temperature, length_scaler - length scaler (can be used to convert pixels to the actual length) is_centered - the line length is converted to the coordinates with zero value in the centre of the CentredObj

source
ThermovisorImages.along_line_distribution_xiaolin_wuMethod
along_line_distribution_xiaolin_wu(img::AbstractMatrix{T}, y0, x0, y1, x1) where T

Evaluates the value matrix content along the line with endpoint coordinates x0,y0,y1,x1, returns indices of all points. As far as Wu's algorithm returns two adjacent points the value is evaluated as an average of two point obtained with Wu's algorithm

see xiaolin_wu function from ImageDraw

source
ThermovisorImages.along_mask_line_distributionFunction
along_mask_line_distribution(imag::AbstractMatrix,c::CentredObj,direction_angle=0.0,line_length=10.0;
                                                                                        length_per_pixel=1.0,
                                                                                        use_wu::Bool=false)

The same as within_mask_line_points_distribution but returns the line length along the coordinates within the image.

line_length - the length of line in the same units as length_per_pixel. The calibration using mm_per_pixel`, returns calibrated length along the line

source
ThermovisorImages.angular_distribution_statisticsMethod
angular_distribution_statistics(angles,along_length_coordinate,distrib;
                        max_length=-1.0,min_length=-1.0)

Fills DistributionStatistics object for angular

Optional:

max_length - maximal value of alonglengthcoordinate to be includet in to the statistics evaluation

min_length - minimal value of alonglengthcoordinate to be includet in to the statistics evaluation

source
ThermovisorImages.angular_distribution_statistics_plotMethod
angular_distribution_statistics_plot(ds::DistributionStatistics;
            show_lower_bound::Bool=true,
            show_upper_bound::Bool=true,
            show_std::Bool=true,
            probability::Float64=0.95,
            is_use_student::Bool=true,
            length_scaler::Float64=1.0,
            label=nothing,
            minorgrid=true,
            gridlinewidth=2,
            title="Average temperature angular distribution",framestyle = :box,
            dpi=600,xlabel = "Angle , °", ylabel="Temperature, °C",
            kwargs...)

The same as radial_distribution_statistics_plot but plots averaged angular distribution

source
ThermovisorImages.cent_to_flagMethod
cent_to_flag(::Type{T},c::CentredObj,sz::Tuple{Int,Int};external=false) where T<:FlagMatrix

Converts centred obj to BitMatrix or the Matrix of bool see `FlagMatrix

source
ThermovisorImages.convert_temperature_to_emissivityMethod
convert_temperature_to_emissivity(image::AbstractMatrix,
                                        real_temperature::Float64,
                                        image_emissivity::Float64;
                                        λ_left::Union{Float64,Nothing}=14.0,
                        λ_right::Union{Float64,Nothing}=14.5,
                        is_in_kelvins::Bool=false)

Evaluates the emissivity, assuming the whole image to be of the same temperature real_temperature. If both λleft and λright are not equal and none of them is nothing the emissivity ϵ is calculated using:

ϵ = image_emissivity⋅∫iᵦ(λ,Tᵣ)dλ/∫iᵦ(λ,Tₘ)dλ

Here iᵦ is the Planck spectral radiance, Tₘ is the measured temperature (value of pixel temperature), Tᵣ is the real temperature of the surface. The integration is preformed over the [λ_left,λ_right] spectral range with the help of band_power function provied by the PlanckFunctions package.

If one of (but not both) λleft and λright is set to nothing or both of them are equal, than the following equaion is solved:

ϵ = image_emissivity⋅iᵦ(λ,Tᵣ)/iᵦ(λ,Tₘ)

source
ThermovisorImages.convert_to_drawableMethod
convert_to_drawable(::CentredObj)

Converts CentredObj to a drawable structure appropriate to the ImageDraw draw function, polygon,ellipse see [ImageDraw.draw] function

source
ThermovisorImages.draw!Method
draw!(rgbim::Matrix{RGB{Float64}},
                c::CentredObj;
                fill=false,
                thickness::Int=55,
                roi_color::RGB{Float64}=DefRoiColor[],
                show_cross::Bool=true,
                kwargs...)

Draws c inside the image rgbim (modified)

Keyword arguments:

fill - if true fills the object

thickness - thickness of c boundary

(this two arguments are transfered to convert_to_drawable)

roi_color - RGB color of ROI boundary and filling

show_cross - if true the cross is displayed

kwargs - Other keyword arguments are transfered directly to the ImageDraw.draw! function

source
ThermovisorImages.drawMethod
draw(image::Matrix{Float64},c::CentredObj;fill=false,thickness::Int=-1,
                                    roi_color::RGB{Float64}=DefRoiColor[], 
                                    color_scheme::Symbol=:none,
                                    show_cross=true,kwargs...)

Converts image to RGB (if not all values of the image are within [0,1] - rescales)

And draws CentreObj c inside the image (does not affect the values of image)

For kwargs see draw!

source
ThermovisorImages.drawMethod
draw(image::RescaledImage,c::Vector{T};kwargs...) where T<:CentredObj

Converts image to RGB and draws multiple objects

source
ThermovisorImages.draw_line_within_maskMethod
draw_line_within_mask(image::Matrix{Float64},c::CentredObj,
                ang,length;thickness::Int=55,
                roi_color::RGB{Float64}=DefRoiColor[], 
                color_scheme::Symbol=:none,kwargs...)

Draws straight line on the image converted to RGB, all points are located within the CentredObj. This line goes through the centre of c and oriented with the angle ang in degrees with positive direction - counterclockwise.

source
ThermovisorImages.fill_from_vect!Method
fill_from_vect!(c::CentredObj, v::AbstractVector)

Fills CentreObj parameters from the vector [centerindex1,centerindex2,dimension1,dimension2,...]

source
ThermovisorImages.fill_im!Method
fill_im!(img,c::CentredObj)

Fills bitmatrix or the matrix of Bool img in a way that all pixels which are within the CentredObj are true and false otherwise.

source
ThermovisorImages.fill_im_external!Method
fill_im_external!(img::FlagMatrix,c::CentredObj)

Fills image matrix img in a way that all pixels which are not within the CentreObj set to true. See also is_within

source
ThermovisorImages.fill_x0!Method
fill_x0!(x0,im_bin::AbstractMatrix,c::CentredObj)

Fills the optimization starting vector by seraching the centre of the image im_bin

source
ThermovisorImages.filter_imageMethod
filter_image(imag::AbstractMatrix,c::CentredObj;external=false)

Filters image according to centered object creating new image if external is true than as a filtering flag the inverse of centered object image is taken

source
ThermovisorImages.filter_imageMethod
filter_image(imag::RescaledImage,markers;label=0)

Funtion zeroes all pixels of the image, except those belonging to the specified pattern. image - rescaled image (see RescaledImage type) markers - the matrix of the same size as the input image, each element of this matrix has unique value-label associated with some pattern. Function label_components returns the markers matrix. (optional) - the value of the label to be selected as a pattern marker

Function returns FilteredImage object

source
ThermovisorImages.find_temperature_filesFunction
    `find_temperature_files(folder::AbstractString)`

Searchs the folder for thermal images files using is_temperature_file Returns dictionary Dict{String,Pair{Float64,String}} with keys parts of files matched using is_temperature_file, values - are temperature pairs of Float64 => full-file-name When file name contains "BB" it supposed to be the blackbody themperature distribution

source
ThermovisorImages.fit_all_patterns!Method
fit_all_patterns(img::RescaledImage,::Type{T}=CircleObj;
                                        level_threshold::Float64=-1.0,
                                        distance_threshold::Float64=-15.0,
                                        max_centred_objs::Int=200,
                                        sort_by_area::Bool = false,
                                        is_descend::Bool = true,
                                        optimizer::Optim.ZerothOrderOptimizer = NelderMead(),
                                        options::Optim.Options=DEFAULT_FITTING_OPTIONS[]) where T<:CentredObj

Function fits all patterns of the image img to the vector of CentredObj ROI objects. The type of ROI should be provided as a second arguments (by default it is a CircleObj)

img - input image of RescaledImage type

For other input arguments see marker_image and fit_centred_obj!

source
ThermovisorImages.fit_all_patterns!Method
fit_all_patterns!(c_vect::Vector{T},
                        markers::MarkeredImage,
                        optimizer::Optim.ZerothOrderOptimizer = NelderMead(),
                        options::Optim.Options=DEFAULT_FITTING_OPTIONS[]) where T<:CentredObj

Fits markered image pattern and fills precreated avector of Centerdobjs See fit_all_patterns!

source
ThermovisorImages.fit_all_patternsMethod
fit_all_patterns(img::RescaledImage,::Type{T}=CircleObj;
                                level_threshold::Float64=-1.0,
                                distance_threshold::Float64=-15.0,
                                max_centred_objs::Int=200,
                                sort_by_area::Bool = false,
                                is_descend::Bool = true,
                                optimizer::Optim.ZerothOrderOptimizer = NelderMead(),
                                options::Optim.Options=DEFAULT_FITTING_OPTIONS[]) where T<:CentredObj

Markers RescaledImage and fits all patterns See fit_all_patterns!

source
ThermovisorImages.fit_centred_obj!Function
fit_centred_obj!(c::CentredObj,image::FilteredImage;kwargs...)

Fits CentredObj (modified) to filtered image (not modified) fit_reduced flag (default=true) indicates what version of the image should be fitted if true - reduced otherwise - full image. For other input arguments see fit_centred_obj!

source
ThermovisorImages.fit_centred_obj!Method
fit_centred_obj!(c::CentredObj,im_bin::FlagMatrix;
                            starting_point::Union{Nothing,Vector{Float64}}=nothing,
                            optimizer::Optim.ZerothOrderOptimizer = Optim.NelderMead(), 
                            options::Optim.Options=DEFAULT_FITTING_OPTIONS[],refit::Bool = true)

Fits CentredObj to binary image pattern (region of units) by adjusting centre coordinates and dimensions using zeroth-order optimizers from Optim.jl package.

Input variables:

c - CentredObj (modified)

im_bin - binarized image (BitMatrix or Matrix{Bool})

(optional)

startingpoint - staring vector (uses [`fillx0!`](@ref) function to fill starting point by default)

optimizer - zeroth-order optimizer from Optim.jl package

options - optimization options from Optim.jl package

refit - if true the starting point of the optimization recalculated otherwise it is taken from the current state vector of ROI

source
ThermovisorImages.flag!Method
flag!(fl::FlagMatrix,m::MarkeredImage,i::Int;negate::Bool=false)

Fills the fl matrix (Bitmatrix or Matrix{Bool}) with the same size as the entire image with all elements set to zero, except the pixels of i'th pattern

source
ThermovisorImages.flagMethod
flag(m::MarkeredImage,i::Int,::Type{T}=Matrix{Bool}) where T<:FlagMatrix

Creates matrix of Bool or Bitmatrix with the same size as the whole markers matrx with trues on the location of i'th pattern

source
ThermovisorImages.full_image_flagMethod
full_image_flag(filtered_im::FilteredImage)

Returns the BitMatrix flag of filtered pattern in the whole image.

Can be used as index matrix in the full image:

filtered_image.full.initial[full_image_flag(filtered_image)] will return all elements which belong to the pattern

source
ThermovisorImages.generate_random_objsMethod
generate_random_objs(::Type{T},centers_range::NTuple{2,R},obj_number::Int,dimension_range::R) where {T<:CentredObj,R<:StepRange{Int,Int}}

Generates obj_number of objects of CentredObj subtype specified by the first argument, Second argument centers_range is a tuple of StepRange s for x and y coordinates of center to be taken from randomly, and dimension_range is the range for dimensions

source
ThermovisorImages.image_fill_discrMethod
image_fill_discr(image::FlagMatrix,c::CentredObj)

Function returns the function to evaluate the discrepancy between CentredObj and the matrix, this function is used during the fitting procedure

source
ThermovisorImages.line_within_maskMethod
line_within_mask(c::CentredObj,ang::Float64,line_length::Int)

Function returns endpoint of the line lying fully within the mask - tuple of four point which can be directly splatted to the alonglinedistribution

ang - angle in degrees

line_length - the length of line

source
ThermovisorImages.m_viewMethod
m_view(im::AbstractMatrix,m::MarkeredImage,i::Int)

Returns the view of matrix im at points corresponding to the i th pattern of MarkeredImage image m

source
ThermovisorImages.marker_imageMethod
marker_image(rescaled::RescaledImage,level_threshold::Float64,distance_threshold::Float64=1e-3)

Markers image patterns, input umage is of RescaledImage image type, levelthreshold - should be between 0.0 and 1.0 distancethreshold - criterium of image binarization after distance transform, should be less than 1

returns MarkeredImage object

source
ThermovisorImages.obj_from_vectMethod
obj_from_vect(::Type{CentredObj},v::AbstractVector)

Creates object from parameters vector, first two arguments are center point other are dimensions [center[1],center[2],dimensions[1],...]

source
ThermovisorImages.radial_distributionMethod
radial_distribution(imag::AbstractMatrix,c::CentredObj,angles_range::AbstractRange,line_length;mm_per_pixel=1.0)

Calls along_mask_line_distribution on lines oriented with some angles range and puts the resulting distribution into one matrix j'th column of this matrix corresponds to the distribution along the line oriented with ang[j] angle. The length of line is line_length, if it is less than 0.0 or greater than the smallest dimension of c, than is is set to the smallest dimentsion of c

source
ThermovisorImages.radial_distribution_statisticsMethod
radial_distribution_statistics(along_length_coordinate::AbstractVector,
    distrib::AbstractVecOrMat;
    max_length=-1.0,min_length=-1.0)

Fills DistributionStatistics object for radial distribution

Optional:

max_length - maximal value of alonglengthcoordinate to be includet in to the statistics evaluation

min_length - minimal value of alonglengthcoordinate to be includet in to the statistics evaluation

source
ThermovisorImages.radial_distribution_statistics_plotMethod
radial_distribution_statistics_plot(ds::DistributionStatistics;
            show_lower_bound::Bool=false,
            show_upper_bound::Bool=false,
            show_std::Bool=true,
            is_use_student::Bool=true,
            probability::Float64=0.95,
            length_scaler::Float64=1.0,
            is_centered::Bool=true,
            bound_color::Symbol=:red,
            # plot kwargs
            label::Union{AbstractString,Nothing} =nothing,
            minorgrid::Bool=true,
            gridlinewidth=2,
            title::Union{AbstractString,Nothing} ="Average temperature radial distribution",
            framestyle::Symbol = :box,
            dpi::Int=600,
            xlabel::Union{AbstractString,Nothing} = "Distance  across the sample ,mm", 
            ylabel::Union{AbstractString,Nothing}  =  "Temperature °C",
            kwargs...)

Recipe to plot the distribution statistics DistributionStatistics object. Returns structure of StatDataPlot type, which has attached recipe to plot radial ditribution averaged value, lower and upper confidence bounds as <d> ± std (if show_std is true) and confidence bounds multiplied by the Student's coefficient (if showlowerbound and showupperbound are true) calculated for the pobability value, if length_scaler is provied all coordinates are multiplied by this value (can be used to convert pixels to actual units) If is_centered is true coordinate goes from -L/2 to +L/2 where L is the maximum of coordinates. Other key-word arguments are the same as for the plot functions, additional keyword arguments are transfered directly to the plot function Usage example:

    using Plots
    plot(radial_distribution_statistics_plot(distribution_statistics))

This code will plot the averaged distribution statistics together with confidence bounds

source
ThermovisorImages.read_temperature_fileMethod
read_temperature_file(f_name::AbstractString;inverse_intensity::Bool=false)

Reads temeprature file f_name is a full file name, inverse_intensity is true if the intensities in the loaded file should be inverted

source
ThermovisorImages.recalculate_with_new_emissivity!Method
recalculate_with_new_emissivity!(image::AbstractArray,new_emissivity::Float64,
                                    image_emissivity::Float64;
                                    λ_left::Union{Float64,Nothing}=14.0,
                                    λ_right::Union{Float64,Nothing}=14.5,
                                    is_in_kelvins::Bool=false,
                                    rel_tol::Float64=1e-3)

Function recalculates all temperatures in image assuming the temperatures were measured for the surface with emissivity image_emissivity; new_emissivity is a new value of emissivity, λ_left and λ_right are left and right wavelength boundaries of infrared camera spectral range in μm. If is_in_kelvins is false (default) all temperatures supposed to be in Celsius and Kelvins otherwise.

To find a new temperature value T the function solves a non-linear equation. If both λleft and λright are not equal and none of them is nothing, the following equation is solved:

new_emissivity ⋅∫iᵦ(λ,T)dλ = image_emissivity ⋅∫iᵦ(λ,Tₘ)dλ

Here iᵦ is the Planck spectral radiance, Tₘ is the measured temperature (value of pixel intensity). The integration is preformed over the [λ_left,λ_right] spectral range with the help of band_power function provied by the PlanckFunctions package.

If one of (but not both) λleft and λright is set to nothing or both of them are equal, than the following equaion is solved:

new_emissivity⋅iᵦ(λ,T) = image_emissivity⋅iᵦ(λ,Tₘ)

In both cases the equation is solved by minimizing the univariate function of least-square difference of left and right parts of the equation. Second version of equation is much faster, but less precise.

source
ThermovisorImages.recalculate_with_new_emissivity!Method
recalculate_with_new_emissivity!(image::AbstractArray,marker::MarkeredImage,
    label::Int,new_emissivity::Float64,image_emissivity::Float64;
    kwargs...)

Recalculates the temperarture of each pixel within image pattern label of MarkeredImage image marker with new_emissivity assuming image_emissivity be the emissvity settled during measurements. Both marker and image should be of the same size. See recalculate_with_new_emissivity!

source
ThermovisorImages.sort_reduce!Method
sort_reduce!(m::MarkeredImage;total_number::Int = -1,descending::Bool=true)

Sorts markers by total area and reduces the total number of patterns if the maximum label is less then total_number value

Input arguments: m - MarkeredImage total_number - number of patterns retained in the image

source
ThermovisorImages.to_rgbMethod
to_rgb(image::Matrix{Float64};color_scheme::String="")

Converts matrix to rgb - martix by applyting the color scheme from ColorSchemes package.

source
ThermovisorImages.within_mask_line_points_distributionFunction
within_mask_line_points_distribution(imag::AbstractMatrix,c::CentredObj,direction_angle=0.0,line_length=10.0;use_wu::Bool=false)

Evaluates the distribution of values in imag matrix along the line with length line_length in pixels oriented with the angle direction_angle in degrees with respect to the posistive direction of oX (column index increase), this line lies within the roi (CentreObj) and goes through its center.

Function returns: a Tuple (points,distrib,linepoints)

points - vector of CartesianIndex of image's points lying on the line

distrib - distribution of values

linepoints - endpoints of line the Tupple of (leftx,leftY,rightx,righty)

source