001/*
002 * Copyright (C) Photon Vision.
003 *
004 * This program is free software: you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License as published by
006 * the Free Software Foundation, either version 3 of the License, or
007 * (at your option) any later version.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
016 */
017
018package org.photonvision.vision.pipe.impl;
019
020import org.opencv.core.Mat;
021import org.opencv.core.Size;
022import org.opencv.imgproc.Imgproc;
023import org.photonvision.vision.pipe.MutatingPipe;
024
025public class ErodeDilatePipe extends MutatingPipe<Mat, ErodeDilatePipe.ErodeDilateParams> {
026    @Override
027    protected Void process(Mat in) {
028        if (params.shouldErode()) {
029            Imgproc.erode(in, in, params.getKernel());
030        }
031        if (params.shouldDilate()) {
032            Imgproc.dilate(in, in, params.getKernel());
033        }
034        return null;
035    }
036
037    public static class ErodeDilateParams {
038        private final boolean m_erode;
039        private final boolean m_dilate;
040        private final Mat m_kernel;
041
042        public ErodeDilateParams(boolean erode, boolean dilate, int kernelSize) {
043            m_erode = erode;
044            m_dilate = dilate;
045            m_kernel =
046                    Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(kernelSize, kernelSize));
047        }
048
049        public boolean shouldErode() {
050            return m_erode;
051        }
052
053        public boolean shouldDilate() {
054            return m_dilate;
055        }
056
057        public Mat getKernel() {
058            return m_kernel;
059        }
060    }
061}