linearPolar,线性极坐标映射,把数据从笛卡尔坐标系转到极坐标坐标系。见下图
logPolar,对数极坐标,是把数据从笛卡尔转到对数极坐标系。见下图
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 |
import java.awt.Color; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; 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.Mat; import org.opencv.core.Point; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class Polar extends JFrame { private final JLabel imageView; private Mat srcMat; private Point mousePoint; public Polar() { srcMat = Imgcodecs.imread("./linearPolar_test.jpg"); JLabel linearPolarLabel = new JLabel("LinearPolar:" + 1); linearPolarLabel.setBounds(15, 15, 100, 15); JRadioButton linearPolar8Btn = new JRadioButton("8"); linearPolar8Btn.setSelected(true); linearPolar8Btn.setBounds(280, 10, 50, 25); linearPolar8Btn.setForeground(Color.BLUE); JRadioButton linearPolar16Btn = new JRadioButton("16"); linearPolar16Btn.setBounds(330, 10, 50, 25); linearPolar16Btn.setForeground(Color.BLUE); ButtonGroup group = new ButtonGroup(); group.add(linearPolar8Btn); group.add(linearPolar16Btn); JSlider linearPolarBar = new JSlider(1, 360); linearPolarBar.setValue(1); linearPolarBar.setBounds(110, 10, 120, 25); linearPolarBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = linearPolarBar.getValue(); linearPolarLabel.setText("LinearPolar:" + value); int type = linearPolar8Btn.isSelected() ? 8 : 16; linearPolarPic(mousePoint, value, type); } }); imageView = new JLabel(); imageView.setBounds(15, 40, 299, 302); imageView.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { mousePoint = new Point(e.getX(), e.getY()); int maxRadius = linearPolarBar.getValue(); int type = linearPolar8Btn.isSelected() ? 8 : 16; linearPolarPic(mousePoint, maxRadius, type); } }); this.setTitle("OpenCV和Java,图片形变"); this.setSize(550, 580); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.getContentPane().setLayout(null); this.getContentPane().add(linearPolarLabel); this.getContentPane().add(linearPolarBar); this.getContentPane().add(linearPolar8Btn); this.getContentPane().add(linearPolar16Btn); this.getContentPane().add(imageView); setIcon(srcMat); } private void linearPolarPic(Point point, int maxRadius, int flags) { double x, y; if (null == point) { x = srcMat.cols() / 2d; y = srcMat.rows() / 2d; } else { x = point.x; y = point.y; } Point center = new Point(x, y); Mat dstMat = new Mat(); // flags = Imgproc.CV_WARP_FILL_OUTLIERS = 8; 填充输出图像所有像素,如果这些点有和外点对应的,则置零。 // flags = Imgproc.CV_WARP_INVERSE_MAP = 16; 表示矩阵由输出图像到输入图像的逆变换,并且因此可以直接用于像素插值。否则,函数从map_matrix中寻找逆变换。 // flags = 8、9、10、16、17、18、28 if (1 == 0) { // Imgproc.linearPolar(srcMat, dstMat, center, value, flags); // Deprecated Imgproc.warpPolar(srcMat, dstMat, srcMat.size(), center, maxRadius, flags); // 本例 maxRadius取0-360 } else { Imgproc.logPolar(srcMat, dstMat, center, maxRadius, flags); // Deprecated //Imgproc.warpPolar(srcMat, dstMat, srcMat.size(), center, maxRadius, flags + Imgproc.WARP_POLAR_LOG); ? 没效果 } 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/3488.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设