Home Schedule
Examples of image boundary checking and basic edge detection approaches using media. Download Complete Example
    

    import media


    # Implementation of the first edge detection algorithm in the book
    def detect_edge_diff(picture):
        source = media.duplicatePicture(picture)
        for px in media.getPixels(source):
            x = media.getX(px)
            y = media.getY(px)
            
            # This is a boundary check to make sure we do not try to get a
            #   bottom-right pixel (botrt) adjacent to our pixel (px)
            if y < media.getHeight(source) - 1 and x < media.getWidth(source) - 1:
                # Access the bottom right pixel in the image based on the x,y of our
                #   current pixel (px)
                botrt = media.getPixel(source, x + 1, y + 1)
                
                # Sum the color channels of each pixel to determine overall luminance
                sum1 = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
                sum2 = media.getRed(botrt) + media.getGreen(botrt) + media.getBlue(botrt)
                
                # Get the absolute value of the difference between the luminance values
                diff = abs(sum2 - sum1)
                
                # Change the pixel color to be the luminance difference between the pixel and the
                #   bottom-rigth pixel
                # The more similar the luminance, the closer to black, the greater the difference
                #   the colors will be lighter grey or white
                newcolor = media.makeColor(diff, diff, diff)
                media.setColor(px, newcolor)

        return source

    # Helper function to calculate the average luminance of a pixel
    #   used by the detect_edge_luminance function
    def calc_luminance(pixel):
        r = media.getRed(pixel)
        g = media.getGreen(pixel)
        b = media.getBlue(pixel)
        
        return (r + b + g) / 3

    # Implementation of the second edge detection algorithm in the book
    def detect_edge_luminance(picture):
        source = media.duplicatePicture(picture)
        for px in media.getPixels(source):
            x = media.getX(px)
            y = media.getY(px)
            
            # This is a boundary check to make sure we do not try to get a
            #   bottom-right pixel (botrt) adjacent to our pixel (px)
            if y < media.getHeight(source) - 1 and x < media.getWidth(source) - 1:
                # Access the bottom right pixel in the image based on the x,y of our
                #   current pixel (px)
                botrt = media.getPixel(source, x + 1, y + 1)
                
                # Calculate the lumunance of the current pixel and the bottom-right pixel
                current_lum = calc_luminance(px)
                botrt_lum = calc_luminance(botrt)
                    
                # Get the absolute value of the difference and compare to a threshold
                #   Higher threshold values means more white pixels
                if abs(botrt_lum - current_lum) > 10:
                    media.setColor(px, media.black)
                else:
                    media.setColor(px, media.white)

        return source

    # Variation on the implementation of the second edge detection algorithm in the book
    #   using the distance between colors
    def detect_edge_distance(picture):
        source = media.duplicatePicture(picture)
        for px in media.getPixels(source):
            x = media.getX(px)
            y = media.getY(px)
            
            # This is a boundary check to make sure we do not try to get a
            #   bottom-right pixel (botrt) adjacent to our pixel (px)
            if y < media.getHeight(source) - 1 and x < media.getWidth(source) - 1:
                # Access the bottom right pixel in the image based on the x,y of our
                #   current pixel (px)
                botrt = media.getPixel(source, x + 1, y + 1)
                
                # Calculate the color distance between the two pixels and compare
                #    that distance to a threshold value where a 
                #    higher threshold values means more white pixels
                if media.distance(media.getColor(px), media.getColor(botrt)) > 25:
                    media.setColor(px, media.black)
                else:
                    media.setColor(px, media.white)

        return source

    # Variation on the implementation of the first edge detection algorithm in the book
    #   that allows for the color to be preserved
    def detect_edge_color(picture):
        source = media.duplicatePicture(picture)
        for px in media.getPixels(source):
            x = media.getX(px)
            y = media.getY(px)
            if y < media.getHeight(source) - 1 and x < media.getWidth(source) - 1:
                # Access the bottom right pixel in the image based on the x,y of our
                #   current pixel (px)
                botrt = media.getPixel(source, x + 1, y + 1)
                
                # Sum the color channels of each pixel to determine overall luminance
                sum1 = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
                sum2 = media.getRed(botrt) + media.getGreen(botrt) + media.getBlue(botrt)
                
                # If the absolute value of the difference in luminance is greater
                #   than the threshold set the pixel to be black otherwise,
                #   leave the pixel alone and keep the color.
                if abs(sum2 - sum1) > 60:
                    newcolor = media.makeColor(0, 0, 0)
                    media.setColor(px, newcolor)

        return source


    pic = media.makePicture('car.jpg')

    media.show(pic)
    media.show(detect_edge_diff(pic))
    media.show(detect_edge_luminance(pic))
    media.show(detect_edge_distance(pic))

    media.show(pic)
    media.show(detect_edge_color(pic))

    media.quit()