001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.video.utils;
031
032import org.openimaj.image.Image;
033import org.openimaj.image.processor.SinglebandImageProcessor;
034import org.openimaj.image.renderer.ScanRasteriser;
035import org.openimaj.image.renderer.ScanRasteriser.ScanLineListener;
036import org.openimaj.math.geometry.shape.Polygon;
037import org.openimaj.math.geometry.shape.Rectangle;
038
039/**
040 * Extract a polygon from an image. The output image will just contain the
041 * polygon. The polygon is rasterised using a {@link ScanRasteriser}.
042 * 
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 * 
045 * @param <T>
046 *            The pixel type
047 * @param <S>
048 *            The {@link Image} type
049 */
050public class PolygonExtractionProcessor<T, S extends Image<T, S>> implements SinglebandImageProcessor<T, S> {
051        private Polygon polygon;
052        private T background;
053
054        /**
055         * Construct with the given polygon and background colour
056         * 
057         * @param p
058         * @param colour
059         */
060        public PolygonExtractionProcessor(Polygon p, T colour) {
061                this.polygon = p;
062                this.background = colour;
063        }
064
065        /*
066         * (non-Javadoc)
067         * 
068         * @see
069         * org.openimaj.image.processor.ImageProcessor#processImage(org.openimaj
070         * .image.Image)
071         */
072        @Override
073        public void processImage(final S image) {
074                final Polygon p = this.polygon;
075                final Rectangle r = p.calculateRegularBoundingBox();
076
077                final int dx = (int) r.x;
078                final int dy = (int) r.y;
079
080                final S output = image.newInstance((int) r.width, (int) r.height);
081                output.fill(background);
082
083                ScanRasteriser.scanFill(p.getVertices(), new ScanLineListener() {
084                        @Override
085                        public void process(int x1, int x2, int y) {
086                                for (int x = x1; x <= x2; x++) {
087                                        output.setPixel(x - dx, y - dy, image.getPixel(x, y));
088                                }
089                        }
090                });
091
092                image.internalAssign(output);
093        }
094}