Skip to content

Commit

Permalink
Working #301: added rectangle detection in pixel segmentation
Browse files Browse the repository at this point in the history
The algorithm for processing user-defined geometries only works on rectangles.  Polygons are ignored.

(This commit was done after a previous revert.  Had to recommit because I deleted some code.)
  • Loading branch information
mrbannon committed May 15, 2014
1 parent fa7dfe7 commit 3753bb0
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions rodan/jobs/gamera/custom/pixel_segment/celery_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
import sys
from gamera.core import init_gamera, load_image
from gamera.plugins.pil_io import from_pil
#import Image
#import ImageDraw
from rodan.jobs.gamera.custom.gamera_custom_base import GameraCustomTask


class PixelSegmentTask(GameraCustomTask):
max_retries = None
name = 'gamera.custom.lyric_extraction.pixel_segment'

settings = [{'visibility': False, 'default': 0, 'has_default': True, 'rng': (-1048576, 1048576), 'name': 'image_width', 'type': 'int'},
{'visibility': False, 'default': None, 'has_default': True, 'name': 'geometries', 'type': 'json'}]

# Preconfiguration setup.
def preconfigure_settings(self, page_url, settings):

init_gamera()
task_image = load_image(page_url)
miyao_settings = settings.copy()
del miyao_settings['image_width']
return {'image_width': task_image.ncols}

# Given an image and coloured geometries from the associated interactive
# job, converts non-white pixels in each geometry in the image to the
# specified colour.
def process_image(self, task_image, settings):

# Get the returned data.
try:
geometries = json.loads(settings['geometries'])
Expand All @@ -33,7 +36,8 @@ def process_image(self, task_image, settings):

# For each of the geometries provided, we have to apply the pixel colour to non-white pixels.
for geometry in geometries:
imageOriginal = self._apply_geometry(imageOriginal, geometry)
if self._is_rectangle(geometry['points']):
imageOriginal = self._apply_geometry(imageOriginal, geometry)

# Convert red to white and black to green.
colour_swap = {(255, 0, 0): (255, 255, 255), (0, 255, 0): (0, 0, 0)}
Expand All @@ -44,6 +48,22 @@ def process_image(self, task_image, settings):
finalImage = from_pil(imageOriginal)
return finalImage

# Returns true iff provided geometry points form a rectangle.
# It assumes at least 3 points.
def _is_rectangle(self, geometryPoints):

if len(geometryPoints) != 4:
return False

return ((geometryPoints[0]['y'] == geometryPoints[1]['y'] and
geometryPoints[1]['x'] == geometryPoints[2]['x'] and
geometryPoints[2]['y'] == geometryPoints[3]['y'] and
geometryPoints[3]['x'] == geometryPoints[0]['x']) or
(geometryPoints[0]['x'] == geometryPoints[1]['x'] and
geometryPoints[1]['y'] == geometryPoints[2]['y'] and
geometryPoints[2]['x'] == geometryPoints[3]['x'] and
geometryPoints[3]['y'] == geometryPoints[0]['y']))

# Given image and geometry (with colour and working vs original dimensions), apply it to the image.
def _apply_geometry(self, image, geometry):

Expand Down

0 comments on commit 3753bb0

Please sign in to comment.