001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 * image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause).
006 * Copyright (c) 2006-2023 Wilhelm Burger, Mark J. Burge. All rights reserved.
007 * Visit https://imagingbook.com for additional details.
008 ******************************************************************************/
009package imagingbook.common.morphology;
010
011import ij.process.Blitter;
012import ij.process.ByteProcessor;
013import imagingbook.common.geometry.basic.NeighborhoodType2D;
014
015/**
016 * <p>
017 * Implements a binary morphological dilation operation. See Sec. 7.2.7 of [1] for additional details.
018 * </p>
019 * <p>
020 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing &ndash; An Algorithmic Introduction</em>, 3rd ed, Springer
021 * (2022).
022 * </p>
023 *
024 * @author WB
025 * @version 2022/09/18
026 */
027public class BinaryOutline implements BinaryMorphologyOperator {
028        
029        private static final byte[][] H4 = { 
030                        { 0, 1, 0 }, 
031                        { 1, 1, 1 }, 
032                        { 0, 1, 0 }};
033        
034        private static final byte[][] H8 = { 
035                        { 1, 1, 1 }, 
036                        { 1, 1, 1 }, 
037                        { 1, 1, 1 }};
038        
039        private final NeighborhoodType2D nh;
040        
041        /**
042         * Constructor, creates a {@link BinaryOutline} operator using
043         * a 4-neighborhood by default ({@link NeighborhoodType2D#N4}).
044         */
045        public BinaryOutline() {
046                this(NeighborhoodType2D.N4);
047        }
048        
049        /**
050         * Constructor, creates a {@link BinaryOutline} operator using
051         * a the specified neighborhood type ({@link NeighborhoodType2D}).
052         * 
053         * @param nh neighborhood type ({@link NeighborhoodType2D})
054         */
055        public BinaryOutline(NeighborhoodType2D nh) {
056                this.nh = nh;
057        }
058        
059        @Override
060        public void applyTo(ByteProcessor ip) {
061                ByteProcessor fg = (ByteProcessor) ip.duplicate();
062                byte[][] H = (nh == NeighborhoodType2D.N4) ? H4 : H8;
063                new BinaryErosion(H).applyTo(fg);       //erode(fg, H);
064                ip.copyBits(fg, 0, 0, Blitter.DIFFERENCE);
065        }
066
067}