Core.flip
Imgproc.remap
Imgproc.warpAffine
图片镜像翻转、以图片中心为原点旋转、垂直及水平拉伸。
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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
import java.awt.Color; import java.awt.event.ItemEvent; import java.awt.event.ItemListener; import java.awt.image.BufferedImage; import javax.swing.ButtonGroup; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JRadioButton; 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.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * 图片翻转、旋转、拉伸 */ public class Rotate extends JFrame { private JLabel imageView; private double leftTopX = 0; private double leftTopY = 0; private double rightTopX = 1; private double rightTopY = 0; private double leftBottomX = 0; private double leftBottomY = 1; private Mat srcMat; public Rotate(String img) { JLabel resizeLabel = new JLabel("镜像:"); resizeLabel.setBounds(15, 10, 40, 25); resizeLabel.setForeground(Color.BLUE); JRadioButton resetBtn = new JRadioButton("复位"); resetBtn.setBounds(80, 10, 65, 25); resetBtn.setForeground(Color.BLUE); resetBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { setIcon(srcMat); } }); JRadioButton updownBtn = new JRadioButton("上下"); updownBtn.setBounds(150, 10, 65, 25); updownBtn.setForeground(Color.BLUE); updownBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { remapPic(1); } }); JRadioButton leftrightBtn = new JRadioButton("左右"); leftrightBtn.setBounds(215, 10, 65, 25); leftrightBtn.setForeground(Color.BLUE); leftrightBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { remapPic(2); } }); JRadioButton updownleftrightBtn = new JRadioButton("上下左右"); updownleftrightBtn.setBounds(285, 10, 130, 25); updownleftrightBtn.setForeground(Color.BLUE); updownleftrightBtn.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { remapPic(3); } }); ButtonGroup group = new ButtonGroup(); group.add(resetBtn); group.add(updownBtn); group.add(leftrightBtn); group.add(updownleftrightBtn); JLabel angleLabel = new JLabel("角度:" + 0); angleLabel.setBounds(15, 50, 60, 25); angleLabel.setForeground(Color.GREEN); JSlider angleBar = new JSlider(0, 360); angleBar.setValue(0); angleBar.setBounds(80, 50, 220, 25); angleBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = angleBar.getValue(); angleLabel.setText("角度:" + value); anglePic(value); } }); JLabel leftTopXLabel = new JLabel("左上X:" + 0); leftTopXLabel.setBounds(15, 90, 65, 25); JSlider leftTopXBar = new JSlider(0, 10); leftTopXBar.setValue(0); leftTopXBar.setBounds(70, 90, 100, 25); leftTopXBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { leftTopX = leftTopXBar.getValue() / 10d; leftTopXLabel.setText("左上X:" + leftTopX); wrapPic(); } }); JLabel leftBottomXLabel = new JLabel("左下X:" + 0); leftBottomXLabel.setBounds(205, 90, 65, 25); JSlider leftBottomXBar = new JSlider(0, 10); leftBottomXBar.setValue(0); leftBottomXBar.setBounds(260, 90, 100, 25); leftBottomXBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { leftBottomX = leftBottomXBar.getValue() / 10d; leftBottomXLabel.setText("左下X:" + leftBottomX); wrapPic(); } }); JLabel rightTopXLabel = new JLabel("右上X:" + 0); rightTopXLabel.setBounds(400, 90, 65, 25); JSlider rightTopXBar = new JSlider(0, 10); rightTopXBar.setValue(10); rightTopXBar.setBounds(460, 90, 100, 25); rightTopXBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { rightTopX = rightTopXBar.getValue() / 10d; rightTopXLabel.setText("右上X:" + rightTopX); wrapPic(); } }); JLabel leftTopYLabel = new JLabel("左上Y:" + 0); leftTopYLabel.setBounds(15, 120, 65, 25); JSlider leftTopYBar = new JSlider(0, 10); leftTopYBar.setValue(0); leftTopYBar.setBounds(70, 120, 100, 25); leftTopYBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { leftTopY = leftTopYBar.getValue() / 10d; leftTopYLabel.setText("左上Y:" + leftTopY); wrapPic(); } }); JLabel leftBottomYLabel = new JLabel("左下Y:" + 0); leftBottomYLabel.setBounds(205, 120, 65, 25); JSlider leftBottomYBar = new JSlider(0, 10); leftBottomYBar.setValue(10); leftBottomYBar.setBounds(260, 120, 100, 25); leftBottomYBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { leftBottomY = leftBottomYBar.getValue() / 10d; leftBottomYLabel.setText("左下Y:" + leftBottomY); wrapPic(); } }); JLabel rightTopYLabel = new JLabel("右上Y:" + 0); rightTopYLabel.setBounds(400, 120, 65, 25); JSlider rightTopYBar = new JSlider(0, 10); rightTopYBar.setValue(0); rightTopYBar.setBounds(460, 120, 100, 25); rightTopYBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { rightTopY = rightTopYBar.getValue() / 10d; rightTopYLabel.setText("右上Y:" + rightTopY); wrapPic(); } }); srcMat = Imgcodecs.imread(img); int width = srcMat.width(); int height = srcMat.height(); imageView = new JLabel(""); imageView.setBounds(40, 150, width, height); this.setTitle("图片翻转、旋转、拉伸"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width + 100, height + 180); this.getContentPane().setLayout(null); this.getContentPane().add(resizeLabel); this.getContentPane().add(resetBtn); this.getContentPane().add(updownBtn); this.getContentPane().add(leftrightBtn); this.getContentPane().add(updownleftrightBtn); this.getContentPane().add(angleLabel); this.getContentPane().add(angleBar); this.getContentPane().add(leftTopXLabel); this.getContentPane().add(leftTopXBar); this.getContentPane().add(leftBottomXLabel); this.getContentPane().add(leftBottomXBar); this.getContentPane().add(rightTopXLabel); this.getContentPane().add(rightTopXBar); this.getContentPane().add(leftTopYLabel); this.getContentPane().add(leftTopYBar); this.getContentPane().add(leftBottomYLabel); this.getContentPane().add(leftBottomYBar); this.getContentPane().add(rightTopYLabel); this.getContentPane().add(rightTopYBar); this.getContentPane().add(imageView); setIcon(srcMat); } private void remapPic(int direction) { int rows = srcMat.rows(); int cols = srcMat.cols(); int type = srcMat.type(); if (1 == 1) { // 方法1 Mat destMat = new Mat(rows, cols, type); if (1 == direction) { Core.flip(srcMat, destMat, 0); // 垂直镜像 } else if (2 == direction) { Core.flip(srcMat, destMat, 1); // 水平镜像 } else { Core.flip(srcMat, destMat, -1); // 上下左右镜像 } setIcon(destMat); return; } // 方法2 Mat xMat = new Mat(rows, cols, CvType.CV_32FC1); // 可选:CV_16SC2、CV_32FC1、CV_32FC2 。 Mat yMat = new Mat(rows, cols, CvType.CV_32FC1); // 可选:CV_16UC1、CV_32FC1 。 for (int i = 0; i < rows; i++) { // 行 for (int j = 0; j < cols; j++) { // 列 if (1 == direction) { // 上下镜像 // int row, int col, double... data xMat.put(i, j, j); // 行还是原来的行 yMat.put(i, j, rows - i); // 列所在的行进行了翻转 } else if (2 == direction) { // 左右镜像 xMat.put(i, j, cols - j); // 行所在的列进行了翻转 yMat.put(i, j, i); // 列还是原来的列 } else { // 上下左右镜像 xMat.put(i, j, cols - j); yMat.put(i, j, rows - i); } } } Mat destMat = new Mat(rows, cols, type); Imgproc.remap(srcMat, destMat, xMat, yMat, 0); // 重映射。根据xMat和yMat将src拷贝到dest。插值方式Imgproc.INTER_NEAREST-0。 setIcon(destMat); } private void anglePic(int angle) { int rows = srcMat.rows(); int cols = srcMat.cols(); int type = srcMat.type(); int scale = 1; // 缩放值 Mat destMat = new Mat(rows, cols, type); Point center = new Point(cols / 2, rows / 2); Mat matrix = Imgproc.getRotationMatrix2D(center, angle, scale); Imgproc.warpAffine(srcMat, destMat, matrix, destMat.size()); setIcon(destMat); } private void wrapPic() { int cols = srcMat.cols(); int rows = srcMat.rows(); Point[] srcPoints = new Point[3]; srcPoints[0] = new Point(0, 0); // 左上角 srcPoints[1] = new Point(cols - 1, 0); // 右上角 srcPoints[2] = new Point(0, rows - 1); // 左下角 MatOfPoint2f srcTri = new MatOfPoint2f(); srcTri.fromArray(srcPoints); Point[] dstPoints = new Point[3]; dstPoints[0] = new Point(cols * leftTopX, rows * leftTopY); dstPoints[1] = new Point(cols * rightTopX, rows * rightTopY); dstPoints[2] = new Point(cols * leftBottomX, rows * leftBottomY); MatOfPoint2f dstTri = new MatOfPoint2f(); dstTri.fromArray(dstPoints); Mat wrapMat = Imgproc.getAffineTransform(srcTri, dstTri); // 根据输入图像的三点坐标计算输出图形的仿射变换矩阵 Mat dstMat = new Mat(rows, cols, srcMat.type()); Imgproc.warpAffine(srcMat, dstMat, wrapMat, dstMat.size()); // 根据仿射变换矩阵,从src计算得到dst 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/3285.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设