Imgproc.pyrUp
Imgproc.pyrDown
Imgproc.resize
Imgproc.getRotationMatrix2D
Imgproc.warpAffine
工具类见 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 |
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.Point; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * 图片缩放 */ public class Transform extends JFrame { private JLabel imageView; private double pyramid = 1; private double resize = 1; private double matrix = 1; private Mat srcMat; public Transform(String img) { JLabel pyramidLabel = new JLabel("高斯金字塔缩放:复位"); pyramidLabel.setBounds(15, 10, 120, 15); JSlider pyramidBar = new JSlider(0, 2); pyramidBar.setValue(2); pyramidBar.setBounds(140, 5, 220, 25); pyramidBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = pyramidBar.getValue(); if (0 == value) { pyramid = 0.5; pyramidLabel.setText("高斯金字塔缩放:" + pyramid); } else if (1 == value) { pyramid = 2; pyramidLabel.setText("高斯金字塔缩放:" + pyramid); } else { pyramid = 1; pyramidLabel.setText("高斯金字塔缩放:复原"); } gaussianPic(); } }); JLabel resizeLabel = new JLabel("resize缩放:原始"); resizeLabel.setBounds(15, 40, 120, 15); JSlider resizeBar = new JSlider(1, 20); resizeBar.setValue(10); resizeBar.setBounds(140, 35, 220, 25); resizeBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = resizeBar.getValue(); if (10 == value || 11 == value) { resize = 1; resizeLabel.setText("resize缩放:原始"); } else if (value > 11) { resize = value - 11; resizeLabel.setText("resize放大:" + resize); // 1 - 9 } else { resize = value / 10d; resizeLabel.setText("resize缩小:" + resize); // 0.1 - 0.9 } resizePic(); } }); JLabel matrixLabel = new JLabel("matrix缩放:" + matrix); matrixLabel.setBounds(15, 70, 120, 15); JSlider matrixBar = new JSlider(1, 20); matrixBar.setValue(10); matrixBar.setBounds(140, 65, 220, 25); matrixBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = matrixBar.getValue(); if (10 == value || 11 == value) { matrix = 1; } else if (value > 11) { matrix = value - 11; } else { matrix = value / 10d; } matrixLabel.setText("matrix缩放:" + matrix); matrixPic(); } }); srcMat = Imgcodecs.imread(img); int width = srcMat.width(); int height = srcMat.height(); imageView = new JLabel(""); imageView.setBounds(40, 100, width, height); this.setTitle("图片缩放"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width + 100, height + 140); this.getContentPane().setLayout(null); this.getContentPane().add(pyramidLabel); this.getContentPane().add(pyramidBar); this.getContentPane().add(resizeLabel); this.getContentPane().add(resizeBar); this.getContentPane().add(matrixLabel); this.getContentPane().add(matrixBar); this.getContentPane().add(imageView); setIcon(srcMat); } private void gaussianPic() { int rows = srcMat.rows(); int cols = srcMat.cols(); Mat mat = new Mat(rows, cols, srcMat.type()); if (pyramid == 2) { // 放大 Imgproc.pyrUp(srcMat, mat, new Size(rows * pyramid, cols * pyramid)); // 图像金字塔所选用的图像必须是正方形。std::abs(dsize.width - ssize.width*2) == dsize.width % 2 && std::abs(dsize.height - ssize.height*2) == dsize.height % 2 in function 'pyrUp_' } else if (pyramid == 0.5) { // 缩小 Imgproc.pyrDown(srcMat, mat, new Size(rows * pyramid, cols * pyramid)); // 图像金字塔所选用的图像必须是正方形。每次都是原基础上的2或0.5倍。 } else { mat = srcMat; } setIcon(mat); } private void resizePic() { int rows = srcMat.rows(); int cols = srcMat.cols(); int ir = (int) (rows * resize); int ic = (int) (cols * resize); Mat mat = new Mat(ir, ic, srcMat.type()); // 目标mat即原mat的resize倍 // Mat src, Mat dst, Size dsize, double fx水平方向, double fy垂直方向, int interpolation插值方式:0临近点,1临近2*2范围的点,2临近4*4范围点,3覆盖区平均值 Imgproc.resize(srcMat, mat, mat.size(), resize, resize, 0); // 按resize倍放大 setIcon(mat); } private void matrixPic() { int rows = srcMat.rows(); int cols = srcMat.cols(); int ir = (int) (rows * matrix); int ic = (int) (cols * matrix); Mat mat = new Mat(ir, ic, srcMat.type()); Point center = new Point(rows / 2, cols / 2); double angle = 0d; Mat rotMat = Imgproc.getRotationMatrix2D(center, angle, matrix); // 生成中心放大矩阵。(Point center, double angle, double scale) Imgproc.warpAffine(srcMat, mat, rotMat, mat.size()); // 根据矩阵转换到目标mat (Mat src, Mat dst, Mat M, Size dsize) setIcon(mat); } 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/3251.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设