Defect Pixel Correction

Discussion related to "under the hood" OpenMV topics.
User avatar
kwagyeman
Posts: 4658
Joined: Sun May 24, 2015 2:10 pm

Defect Pixel Correction

Postby kwagyeman » Tue Sep 01, 2020 7:20 pm

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

Code: Select all

# 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.
Nyamekye,

Return to “Technical Discussion”

Who is online

Users browsing this forum: No registered users and 1 guest