Created
April 25, 2013 15:05
-
-
Save ramonesteban/5460416 to your computer and use it in GitHub Desktop.
Fragmento de código para el preprocesamiento de la detección de agujeros
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def minimums_in_histogram(self, hist): | |
minimums = list() | |
for i, value in enumerate(hist): | |
try: | |
if hist[i-2] > value and hist[i-1] > value and value <= hist[i+1]: | |
minimums.append(i) | |
except: | |
pass | |
return minimums | |
def draw_histograms(self, image, width, height, hist_horiz, hist_verti): | |
pixels = image.load() | |
draw = ImageDraw.Draw(image) | |
last_x = 0 | |
last_y = hist_verti[0]/self.norm | |
for x in range(width): | |
y = hist_verti[0]/self.norm | |
del hist_verti[0] | |
pixels[x, y] = (255, 0, 0) | |
draw.line((last_x, last_y, x, y), fill=(255, 0, 255)) | |
last_x = x | |
last_y = y | |
last_x = hist_horiz[0]/self.norm | |
last_y = 0 | |
for y in range(height): | |
x = hist_horiz[0]/self.norm | |
del hist_horiz[0] | |
pixels[x, y] = (255, 255, 0) | |
draw.line((last_x, last_y, x, y), fill=(0, 255, 255)) | |
last_x = x | |
last_y = y | |
return image | |
def action(self): | |
image = grayscale(self.image) | |
image = binarization(self.image, 60) | |
width, height = get_image_size(image) | |
pixels = image.load() | |
hist_verti = list() | |
for x in range(width): | |
pixels_sum = 0 | |
for y in range(height): | |
pixels_sum += pixels[x, y][0] | |
hist_verti.append(pixels_sum) | |
hist_horiz = list() | |
for y in range(height): | |
pixels_sum = 0 | |
for x in range(width): | |
pixels_sum += pixels[x, y][0] | |
hist_horiz.append(pixels_sum) | |
min_x = self.minimums_in_histogram(hist_verti) | |
min_y = self.minimums_in_histogram(hist_horiz) | |
holes = list() | |
counter = 1 | |
for x in min_x: | |
for y in min_y: | |
this_pixel_color = pixels[x, y][0] | |
if this_pixel_color < self.dark_color: | |
r = random.randint(120, 140) | |
g = random.randint(75, 95) | |
b = random.randint(140, 160) | |
image, count, pixels_with_color = self.bfs(image, (x, y), (r, g, b)) | |
percentage = (count * 100.0)/(width*height) | |
holes.append((counter, percentage, (x, y), (r, g, b), pixels_with_color)) | |
counter += 1 | |
# draw the histograms over the image | |
image = Image.open(self.image_file_path) | |
hist_image = self.draw_histograms(image, width, height, hist_horiz, hist_verti) | |
hist_image.save('image-histogram.png', 'png') | |
# draw intersecting lines in a hole | |
image = Image.open(self.image_file_path) | |
draw = ImageDraw.Draw(image) | |
pixels = image.load() | |
for hole in holes: | |
counter, percentage, center, color, pixels_with_color = hole | |
x, y = center | |
draw.ellipse((x-2, y-2, x+2, y+2), fill=(255, 255, 0), outline=(255, 255, 0)) | |
draw.line((x, 0, x, height), fill=(0, 255, 0)) | |
draw.line((0, y, width, y), fill=(255, 0, 0)) | |
image.save('image-lines.png', 'png') | |
# draw the holes detected | |
image = Image.open(self.image_file_path) | |
draw = ImageDraw.Draw(image) | |
pixels = image.load() | |
for hole in holes: | |
counter, percentage, center, color, pixels_with_color = hole | |
for x, y in pixels_with_color: | |
pixels[x, y] = color | |
x, y = center | |
print 'Hole %d detected at (%d, %d)' % (counter, x, y) | |
print '> %.4f%% of all the image' % percentage | |
draw.ellipse((x-2, y-2, x+2, y+2), fill=(255, 255, 0), outline=(255, 255, 0)) | |
draw.text((x+4, y-4), 'H'+str(counter), fill=(0, 255, 255)) | |
image.save('image-holes.png', 'png') | |
self.update_image(image) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment