Examples of image boundary checking and basic edge detection approaches using media.
Download Complete Example
import media
def detect_edge_diff(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:
botrt = media.getPixel(source, x + 1, y + 1)
sum1 = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
sum2 = media.getRed(botrt) + media.getGreen(botrt) + media.getBlue(botrt)
diff = abs(sum2 - sum1)
newcolor = media.makeColor(diff, diff, diff)
media.setColor(px, newcolor)
return source
def calc_luminance(pixel):
r = media.getRed(pixel)
g = media.getGreen(pixel)
b = media.getBlue(pixel)
return (r + b + g) / 3
def detect_edge_luminance(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:
botrt = media.getPixel(source, x + 1, y + 1)
current_lum = calc_luminance(px)
botrt_lum = calc_luminance(botrt)
if abs(botrt_lum - current_lum) > 10:
media.setColor(px, media.black)
else:
media.setColor(px, media.white)
return source
def detect_edge_distance(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:
botrt = media.getPixel(source, x + 1, y + 1)
if media.distance(media.getColor(px), media.getColor(botrt)) > 25:
media.setColor(px, media.black)
else:
media.setColor(px, media.white)
return source
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:
botrt = media.getPixel(source, x + 1, y + 1)
sum1 = media.getRed(px) + media.getGreen(px) + media.getBlue(px)
sum2 = media.getRed(botrt) + media.getGreen(botrt) + media.getBlue(botrt)
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()