Imgproc.GaussianBlur
Imgproc.medianBlur
Imgproc.boxFilter
Imgproc.blur
Imgproc.bilateralFilter
工具类见 http://www.gaohaiyan.com/3229.html
本例完整代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * 模糊 */ public class Blur extends JFrame { private JLabel imageView; private double gaussian = 1; private int median = 1; private double box = 1; private double normalized = 1; private int bilateral = 1; private Mat srcMat; private int pattern = -1; public Blur(String path) { JLabel gaussianLabel = new JLabel("高斯模糊:" + gaussian); gaussianLabel.setBounds(15, 10, 90, 15); JSlider gaussianBar = new JSlider(1, 100); gaussianBar.setValue((int) gaussian); gaussianBar.setBounds(110, 5, 180, 25); gaussianBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = gaussianBar.getValue(); if (value % 2 == 0) { value = value + 1; } gaussian = value * 1.0d; gaussianLabel.setText("高斯模糊:" + gaussian); pattern = 0; setPic(); } }); JLabel medianLabel = new JLabel("中值模糊:" + median); medianLabel.setBounds(15, 40, 80, 15); JSlider medianBar = new JSlider(1, 100); medianBar.setValue((int) median); medianBar.setBounds(110, 35, 180, 25); medianBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = medianBar.getValue(); if (value % 2 == 0) { value = value + 1; } median = value; medianLabel.setText("中值模糊:" + median); pattern = 1; setPic(); } }); JLabel boxLabel = new JLabel("方框模糊:" + box); boxLabel.setBounds(15, 70, 80, 15); JSlider boxBar = new JSlider(0, 100); boxBar.setValue((int) box); boxBar.setBounds(110, 65, 180, 25); boxBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { box = boxBar.getValue() * 1.0d; boxLabel.setText("方框模糊:" + box); pattern = 2; setPic(); } }); JLabel normalizedLabel = new JLabel("归一模糊:" + normalized); normalizedLabel.setBounds(15, 100, 80, 15); JSlider normalizedBar = new JSlider(0, 100); normalizedBar.setValue((int) normalized); normalizedBar.setBounds(110, 95, 180, 25); normalizedBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { normalized = normalizedBar.getValue() * 1.0d; normalizedLabel.setText("归一模糊:" + normalized); pattern = 3; setPic(); } }); JLabel bilateralLabel = new JLabel("双向模糊:" + bilateral); bilateralLabel.setBounds(15, 130, 80, 15); JSlider bilateralBar = new JSlider(0, 100); bilateralBar.setValue((int) bilateral); bilateralBar.setBounds(110, 125, 180, 25); bilateralBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { bilateralLabel.setText("双向模糊:" + bilateral); bilateral = bilateralBar.getValue(); pattern = 4; setPic(); } }); srcMat = Imgcodecs.imread(path); int width = srcMat.width(); int height = srcMat.height(); imageView = new JLabel(""); imageView.setBounds(0, 160, width, height); this.setTitle("图片模糊处理"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width + 100, height + 170); this.getContentPane().setLayout(null); this.getContentPane().add(gaussianLabel); this.getContentPane().add(gaussianBar); this.getContentPane().add(medianLabel); this.getContentPane().add(medianBar); this.getContentPane().add(boxLabel); this.getContentPane().add(boxBar); this.getContentPane().add(normalizedLabel); this.getContentPane().add(normalizedBar); this.getContentPane().add(bilateralLabel); this.getContentPane().add(bilateralBar); this.getContentPane().add(imageView); setPic(); } private void setPic() { int rowSize = srcMat.rows(); int colSize = srcMat.cols(); int type = srcMat.type(); if (0 == pattern) { // 高斯 Mat mat = new Mat(rowSize, colSize, type); Imgproc.GaussianBlur(srcMat, mat, new Size(gaussian, gaussian), 0); setIcon(mat); return; } if (1 == pattern) { // 中值 Mat mat = new Mat(rowSize, colSize, type); Imgproc.medianBlur(srcMat, mat, median); setIcon(mat); return; } if (2 == pattern) { // 方框 Mat mat = new Mat(rowSize, colSize, type); Imgproc.boxFilter(srcMat, mat, srcMat.depth(), new Size(box, box)); // 未解决filter.dispatch.cpp:146: error: (-215:Assertion failed) 0 <= anchor.x && anchor.x < ksize.width && 0 <= anchor.y && anchor.y < ksize.height in function 'init' setIcon(mat); return; } if (3 == pattern) { // 归一化 Mat mat = new Mat(rowSize, colSize, type); Imgproc.blur(srcMat, mat, new Size(normalized, normalized)); // 未解决CvException: cv::Exception: OpenCV(3.4.16) filter.dispatch.cpp:146: error: (-215:Assertion failed) 0 <= anchor.x && anchor.x < ksize.width && 0 <= anchor.y && anchor.y < ksize.height in function 'init' setIcon(mat); return; } if (4 == pattern) { // 双边 Mat mat = new Mat(rowSize, colSize, type); Imgproc.bilateralFilter(srcMat, mat, bilateral, 120, 120); setIcon(mat); return; } setIcon(srcMat); } private void setIcon(Mat mat) { BufferedImage image = CVUtil.matToBufferedImage(mat); // CVUtil 见 http://www.gaohaiyan.com/3229.html imageView.setIcon(new ImageIcon(image)); } } |
均值漂移,油画效果 MeanShiftFiltering
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int sp = 17; int sr = 30; int max = 1; Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); // src,输入图像,8位,三通道的彩色图像,并不要求必须是RGB格式,HSV、YUV等Opencv中的彩色图像格式均可; // dst,输出图像,跟输入src有同样的大小和数据格式; // sp,定义的漂移物理空间半径大小; // sr,定义的漂移色彩空间半径大小; // maxLevel,定义金字塔的最大层数; // termcrit,定义的漂移迭代终止条件,可以设置为迭代次数满足终止,迭代目标与中心点偏差满足终止,或者两者的结合; Imgproc.pyrMeanShiftFiltering(srcMat, dstMat, sp, sr, max, new TermCriteria(TermCriteria.MAX_ITER | TermCriteria.EPS, 50, 0.001)); |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3234.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设