Defect Pixel Correction

This got solved in an email thread… so, posting the results here:

# DEFECT PIXEL CORRECTION FOR GLOBAL SHUTTER MODULE

import sensor, image, time


def defect_pixel_correction(img, point_list):
    """
    Input is an image object 'img' and a list of points [(x,y),(x,y),etc.]
    """

    for p in point_list:
        val_list = []
        for i in range(3):
            for j in range(3):
                val = img.get_pixel(p[0]+i-1, p[1]+j-1)
                val_list.append(val)
        val_list.sort()

        # median
        try: # for the case that this pixel is on the image border
            img.set_pixel(p[0], p[1], val_list[4])
        except:
            pass


def defect_pixel_correction_2(img, point_list):
    # instead of median the value of one neighbor pixel is copied
    for p in point_list:
        try: # for the case that this pixel is on the image border
            img.set_pixel(p[0], p[1], img.get_pixel(p[0]+1, p[1]))
        except:
            pass


def find_defect_pixels():
    # if you use this, you have to cover the lens before starting this code
    thresh = 20
    img = sensor.snapshot()
    ind = []
    for posx in range(sensor.width()):
        for posy in range(sensor.height()):
            if img.get_pixel(posx, posy) > thresh:
                ind.append((posx, posy))

    return ind


sensor.reset()                      # Reset and initialize the sensor.
sensor.set_pixformat(sensor.GRAYSCALE) # Set pixel format to RGB565 (or GRAYSCALE)
sensor.set_framesize(sensor.WVGA2)   # Set frame size to QVGA (320x240)
sensor.skip_frames(time = 2000)     # Wait for settings take effect.
clock = time.clock()                # Create a clock object to track the FPS.

correction = 2     # 0 = no correction
                   # 1 = median correction (lens must be covered at the beginning)
                   #     recommended for low resolution (e.g. QQVGA)
                   # 2 = copy one neighbor pixel (lens must be covered at the beginning)
                   #     recommended for high resolution (e.g. WVGA2)

if correction > 0:
    ind = find_defect_pixels()

while(True):
    clock.tick()                    # Update the FPS clock.
    img = sensor.snapshot()         # Take a picture and return the image.

    if correction == 1:
        defect_pixel_correction(img, ind)
    elif correction == 2:
        defect_pixel_correction_2(img, ind)

    print(clock.fps())              # Note: OpenMV Cam runs about half as fast when connected
                                    # to the IDE. The FPS should increase once disconnected.

How to implement this for FIR sensors?

I got such picture from closed MLX90640 objective.
There are three bad pixels. As a result I got wrong Tmin and Tmax values.
So, are there a techniques to delete the results from these pixels from calculations?
BadPixels

Official Melexis library include such technique

Hi, you just use the median filter. img.median() As for Tmin/Tmax. You’ll need to use get_stats() to get the color min and maxes and then recompute the min/max manually.

Thanks! I’ll try