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.IJ;
012import ij.process.Blitter;
013import ij.process.ByteProcessor;
014
015/**
016 * <p>
017 * Implements a binary morphological dilation operation. See Sec. 7.2.3 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 BinaryDilation extends BinaryMorphologyFilter {
028        
029        /**
030         * Constructor, creates a {@link BinaryDilation} with a 3x3 box structuring
031         * element by default.
032         */
033        public BinaryDilation() {
034                super();
035        }
036        
037        /**
038         * Constructor, creates a {@link BinaryDilation} with the specified structuring
039         * element.
040         * 
041         * @param H the structuring element
042         */
043        public BinaryDilation(byte[][] H) {
044                super(H);
045        }
046        
047        @Override
048        public void applyTo(ByteProcessor bp) {
049                //assume that the hot spot of H is at its center (ic,jc)
050                int xc = (H[0].length - 1) / 2;
051                int yc = (H.length - 1) / 2;
052                int N = H.length * H[0].length;
053                
054                ByteProcessor tmp = (ByteProcessor) bp.createProcessor(bp.getWidth(), bp.getHeight());
055                
056                int k = 0;
057                IJ.showProgress(k, N);
058                for (int j = 0; j < H.length; j++) {
059                        for (int i = 0; i < H[j].length; i++) {
060                                if (H[j][i] > 0) { // this element is set
061                                        // copy image into position (u-ch,v-cv)
062                                        tmp.copyBits(bp, i - xc, j - yc, Blitter.MAX);
063                                }
064                                IJ.showProgress(k++, N);
065                        }
066                }
067                bp.copyBits(tmp, 0, 0, Blitter.COPY);
068        }
069
070}