Imgproc.filter2D
Imgproc.Laplacian
Imgproc.Sobel
卷积运算。
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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
import java.awt.image.BufferedImage; import javax.swing.ImageIcon; import javax.swing.JCheckBox; 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.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * 图片滤镜、平滑、浮雕、边缘检测 */ public class Filter extends JFrame { private JLabel imageView; private int smooth = 1; private int sobelX = 1; private int sobelY = 1; private Mat srcMat; public Filter(String img) { JLabel smoothLabel = new JLabel("平滑:" + smooth); smoothLabel.setBounds(15, 10, 50, 25); JSlider smoothBar = new JSlider(1, 10); smoothBar.setValue(smooth); smoothBar.setBounds(65, 10, 150, 25); smoothBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { smooth = smoothBar.getValue(); smoothLabel.setText("平滑:" + smooth); smoothPic(); } }); JCheckBox prewittHBtn = new JCheckBox("水平浮雕"); prewittHBtn.setBounds(10, 40, 90, 25); prewittHBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { prewittHPic(prewittHBtn.isSelected()); } }); JCheckBox prewittVBtn = new JCheckBox("垂直浮雕"); prewittVBtn.setBounds(130, 40, 90, 25); prewittVBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { prewittVPic(prewittVBtn.isSelected()); } }); JCheckBox laplacianBtn = new JCheckBox("拉普拉斯边缘检测"); laplacianBtn.setBounds(10, 70, 160, 25); laplacianBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { laplacianPic(laplacianBtn.isSelected()); } }); JLabel sobelXLabel = new JLabel("索贝尔边缘检测: x=" + sobelX); sobelXLabel.setBounds(10, 100, 140, 25); JSlider sobelXBar = new JSlider(1, 2); sobelXBar.setValue(1); sobelXBar.setBounds(140, 100, 150, 25); sobelXBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { sobelX = sobelXBar.getValue(); sobelXLabel.setText("索贝尔边缘检测: x=" + sobelX); sobelPic(); } }); JLabel sobelYLabel = new JLabel("y=" + sobelY); sobelYLabel.setBounds(300, 100, 40, 25); JSlider sobelYBar = new JSlider(1, 2); sobelYBar.setValue(1); sobelYBar.setBounds(320, 100, 150, 25); sobelYBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { sobelY = sobelYBar.getValue(); sobelYLabel.setText("y=" + sobelY); sobelPic(); } }); JLabel freichennLabel = new JLabel("FreiChenn、Scharr、Robinson、Kirsch、Emboss"); freichennLabel.setBounds(10, 130, 320, 25); JLabel sqrboxLabel = new JLabel("Imgproc.sqrBoxFilter(src, dst, CvType.CV_8U, new Size(alpha,alpha));"); sqrboxLabel.setBounds(10, 160, 440, 25); srcMat = Imgcodecs.imread(img); int width = srcMat.width(); int height = srcMat.height(); imageView = new JLabel(""); imageView.setBounds(40, 120, width + 50, height + 50); this.setTitle("图片滤镜、平滑、浮雕、边缘检测"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width + 100, height + 250); this.getContentPane().setLayout(null); this.getContentPane().add(smoothLabel); this.getContentPane().add(smoothBar); this.getContentPane().add(prewittHBtn); this.getContentPane().add(prewittVBtn); this.getContentPane().add(laplacianBtn); this.getContentPane().add(sobelXLabel); this.getContentPane().add(sobelXBar); this.getContentPane().add(sobelYLabel); this.getContentPane().add(sobelYBar); this.getContentPane().add(freichennLabel); this.getContentPane().add(sqrboxLabel); this.getContentPane().add(imageView); setIcon(srcMat); } private void smoothPic() { Mat kernel = Mat.ones(smooth, smooth, CvType.CV_32F); // 卷积核。创建smooth*smooth像素的32位mat,通道默认1:灰度图片-单通道图像,值都是1。核有锚点一般是数组中心。 for (int i = 0; i < kernel.rows(); i++) { for (int j = 0; j < kernel.cols(); j++) { double[] pixel = kernel.get(i, j); // 单通道,数组里只有1个元素,即值"1"。 double b = pixel[0] / (smooth * smooth); // smooth值越大,则运算后的值越小 pixel[0] = b; kernel.put(i, j, pixel); } } Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.filter2D(srcMat, dstMat, srcMat.depth(), kernel); // 卷积运算 // 卷积操作: // <1>将核的锚点放在该特定位置的像素上,同时,核内的其他值与该像素邻域的各像素重合; // <2>将核内各值与相应像素值相乘,并将乘积相加; // <3>将所得结果放到与锚点对应的像素上; // <4>对图像所有像素重复上述过程。 setIcon(dstMat); } private void prewittHPic(boolean isPrewittPic) { if (isPrewittPic) { Mat kernel = new Mat(3, 3, CvType.CV_32F); kernel.put(0, 0, new float[]{-1, 0, 1}); kernel.put(1, 0, new float[]{-1, 0, 1}); kernel.put(2, 0, new float[]{-1, 0, 1}); prewittPic(kernel); } else { setIcon(srcMat); } } private void prewittVPic(boolean isPrewittPic) { if (isPrewittPic) { Mat kernel = new Mat(3, 3, CvType.CV_32F); kernel.put(0, 0, new float[]{-1, -1, -1}); kernel.put(1, 0, new float[]{0, 0, 0}); kernel.put(2, 0, new float[]{1, 1, 1}); prewittPic(kernel); } else { setIcon(srcMat); } } private void prewittPic(Mat kernel) { Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.filter2D(srcMat, dstMat, srcMat.depth(), kernel); // 卷积运算 setIcon(dstMat); } private void laplacianPic(boolean isLaplacian) { if (isLaplacian) { Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.Laplacian(srcMat, dstMat, srcMat.depth()); // Laplacian(Mat src, Mat& dst, int ddepth, int ksize = 1, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT) // Imgproc.Laplacian(Mat src, Mat dst, int ddepth, int ksize); // ksize为奇数不可大于31。 // Imgproc.Laplacian(Mat src, Mat dst, int ddepth, int ksize, double scale); // scale影响轮廓明显程度 // Imgproc.Laplacian(Mat src, Mat dst, int ddepth, int ksize, double scale, double delta); // delta影响亮度 // Imgproc.Laplacian(Mat src, Mat dst, int ddepth, int ksize, double scale, double delta, int borderType); // borderType取值:0、1、2。 setIcon(dstMat); } else { setIcon(srcMat); } } private void sobelPic( ) { if (0 == sobelX && 0 == sobelY){ sobelX = 1; } // x或y大于2,error: (-215:Assertion failed) ksize > order in function 'getSobelKernels' Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.Sobel(srcMat, dstMat, srcMat.depth(), sobelX, sobelY); // Sobel(Mat src, Mat& dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0, int borderType = BORDER_DEFAULT) // Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy, int ksize); // ksize为奇数。 // Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy, int ksize, double scale); // Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy, int ksize, double scale, double delta); // Imgproc.Sobel(Mat src, Mat dst, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType); setIcon(dstMat); } private void setIcon(Mat mat) { BufferedImage image = CVUtil.matToBufferedImage(mat); // CVUtil 见 http://www.gaohaiyan.com/3229.html imageView.setIcon(new ImageIcon(image)); } } |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3296.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设