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 java.util.ArrayList; 021import java.util.List; 022import org.photonvision.vision.opencv.Contour; 023import org.photonvision.vision.pipe.CVPipe; 024 025public class SpeckleRejectPipe 026 extends CVPipe<List<Contour>, List<Contour>, SpeckleRejectPipe.SpeckleRejectParams> { 027 private final List<Contour> m_despeckledContours = new ArrayList<>(); 028 029 @Override 030 protected List<Contour> process(List<Contour> in) { 031 for (var c : m_despeckledContours) { 032 c.mat.release(); 033 } 034 m_despeckledContours.clear(); 035 036 if (!in.isEmpty()) { 037 double averageArea = 0.0; 038 for (Contour c : in) { 039 averageArea += c.getArea(); 040 } 041 averageArea /= in.size(); 042 043 double minAllowedArea = params.getMinPercentOfAvg() / 100.0 * averageArea; 044 for (Contour c : in) { 045 if (c.getArea() >= minAllowedArea) { 046 m_despeckledContours.add(c); 047 } else { 048 c.release(); 049 } 050 } 051 } 052 053 return m_despeckledContours; 054 } 055 056 public static class SpeckleRejectParams { 057 private final double m_minPercentOfAvg; 058 059 public SpeckleRejectParams(double minPercentOfAvg) { 060 m_minPercentOfAvg = minPercentOfAvg; 061 } 062 063 public double getMinPercentOfAvg() { 064 return m_minPercentOfAvg; 065 } 066 } 067}