Code Monkey home page Code Monkey logo

Comments (1)

dmerinodel avatar dmerinodel commented on July 21, 2024

Hi,

I kept studying that error and after some 'debugging' in the raw2temp module, I discovered that my thermal_np_array had some 0 values. I'm assuming that these are some hardware bad detections and I'm replacing those 0s with some non zero value from the same row (e.g, the first non zero value). I'm including this piece of code

for row in thermal_np:
    if 0 in row:
        non_zeros = row[row != 0]
        row[row == 0] = non_zeros[0]

Update 18-06-21. That method led to bad coloured images. I ended up adding the following module.

def zeros_filter(matrix, row, col):
    """
    Input: bidimensional np.array, row and column where the 0 lies.
    Output: average of the surrounding values. 
    """
    shape = np.shape(matrix)
    rows = shape[0]
    cols = shape[1]
    
    # Surroundings depends on the position of the element (corners, edges or inner 
    # points). Each 'if/elif' is for each possible position in the matrix. Surrounding
    # non-zero elements are selected for the interpolation and if there isn't any, a
    # non-zero value from the row is taken.
    
    if (row not in {0,rows-1}) and (col not in {0,cols-1}): # inner points
        surr = np.array([matrix[row-1][col-1],matrix[row-1][col],matrix[row][col+1],
                         matrix[row][col-1],matrix[row][col+1],
                         matrix[row+1][col-1],matrix[row+1][col],matrix[row+1][col+1]])
        surr = surr[surr != 0]
        if len(surr) == 0:
            new_value = matrix[row][matrix[row] != 0][0]
        else:
            mean = np.mean(surr)
            new_value = mean

    elif (row == 0): # top row
        if (col == 0):
            surr = np.array([matrix[row][col+1],matrix[row+1][col],
                             matrix[row+1][col+1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean

        elif (col == cols-1):
            surr = np.array([matrix[row][col-1],matrix[row+1][col],
                             matrix[row+1][col-1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean

        else:
            surr = np.array([matrix[row][col-1],matrix[row][col+1],
                             matrix[row+1][col-1],matrix[row+1][col],matrix[row+1][col+1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean
    elif (row == rows-1): # bottom row
        if (col == 0):
            surr = np.array([matrix[row][col+1],matrix[row-1][col],
                             matrix[row][col+1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean
        elif (col == cols-1):
            surr = np.array([matrix[row][col-1],matrix[row-1][col],
                             matrix[row-1][col-1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean
        else:
            surr = np.array([matrix[row-1][col-1],matrix[row-1][col],matrix[row][col+1],
                             matrix[row][col-1],matrix[row][col+1]])
            surr = surr[surr != 0]
            if len(surr) == 0:
                new_value = matrix[row][matrix[row] != 0][0]
            else:
                mean = np.mean(surr)
                new_value = mean
    elif (col == 0): # first column
        surr = np.array([matrix[row-1][col],matrix[row][col+1],
                         matrix[row][col+1],
                         matrix[row+1][col],matrix[row+1][col+1]])
        surr = surr[surr != 0]
        if len(surr) == 0:
            new_value = matrix[row][matrix[row] != 0][0]
        else:
            mean = np.mean(surr)
            new_value = mean
    elif (col == cols-1):  # last column
        surr = np.array([matrix[row-1][col],matrix[row][col-1],
                         matrix[row][col-1],
                         matrix[row+1][col],matrix[row+1][col-1]])
        surr = surr[surr != 0]
        
        if len(surr) == 0:
            new_value = matrix[row][matrix[row] != 0][0]
        else:
            mean = np.mean(surr)
            new_value = mean

    return new_value

I think variable names and comments are clear enough. It is some kind of "convolution". If someone knows how to do it in a easier way I'd appreciate it :)

from read_thermal.py.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.