Imgproc.equalizeHist
Imgproc.GaussianBlur
Core.addWeighted
Core.merge
Core.split
灰度图、直方图均衡化、YUV、权重。
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 |
import java.awt.Color; import java.awt.image.BufferedImage; import java.util.ArrayList; 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.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; /** * 图片对比度调整 */ public class Equalize extends JFrame { private JLabel imageView; private boolean isAddGaussian = false; private boolean isGrayHist = false; private boolean isYUV = false; private double alpha = 1; private double beta = 1; private double gamma = 0; private double gaussian = 0; private Mat srcMat; public Equalize(String img) { JCheckBox grayHistBtn = new JCheckBox("灰阶强化"); grayHistBtn.setBounds(15, 10, 90, 25); grayHistBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { isGrayHist = !isGrayHist; grayHistPic(); } }); JCheckBox yuvBtn = new JCheckBox("YUV强化"); yuvBtn.setBounds(115, 10, 90, 25); yuvBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { isYUV = !isYUV; yuvPic(); } }); JLabel alphaLabel = new JLabel("alpha:" + alpha); alphaLabel.setBounds(15, 50, 80, 25); JSlider alphaBar = new JSlider(1, 20); alphaBar.setValue(10); alphaBar.setBounds(95, 50, 220, 25); alphaBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = alphaBar.getValue(); if (10 == value || 11 == value) { alpha = 1.0d; } else if (value > 10) { alpha = (value - 10) * 1.0d; } else if (value < 10) { alpha = value / 10d; } alphaLabel.setText("alpha:" + alpha); bgrPic(); } }); JLabel betaLabel = new JLabel("beta:" + beta); betaLabel.setBounds(15, 75, 80, 25); JSlider betaBar = new JSlider(1, 20); betaBar.setValue(10); betaBar.setBounds(95, 75, 220, 25); betaBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = betaBar.getValue(); if (10 == value || 11 == value) { beta = 1.0d; } else if (value > 10) { beta = (value - 10) * 1.0d; } else if (value < 10) { beta = value / 10d; } betaLabel.setText("beta:" + beta); bgrPic(); } }); JLabel gammaLabel = new JLabel("gamma:" + gamma); gammaLabel.setBounds(15, 100, 80, 25); JSlider gammaBar = new JSlider(1, 100); gammaBar.setValue(50); gammaBar.setBounds(95, 100, 220, 25); gammaBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = gammaBar.getValue(); gamma = (value - 10) * 1d; gammaLabel.setText("gamma:" + gamma); bgrPic(); } }); JLabel gaussianLabel = new JLabel("高斯:" + gaussian); gaussianLabel.setBounds(350, 70, 70, 25); JSlider gaussianBar = new JSlider(0, 200); gaussianBar.setValue(0); gaussianBar.setBounds(420, 70, 160, 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); bgrPic(); } }); JCheckBox addGaussianBtn = new JCheckBox("开启模糊"); addGaussianBtn.setBounds(320, 50, 90, 25); addGaussianBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { isAddGaussian = !isAddGaussian; if (isAddGaussian) { alphaLabel.setForeground(Color.BLUE); betaLabel.setForeground(Color.BLUE); gammaLabel.setForeground(Color.BLUE); gaussianLabel.setForeground(Color.BLUE); } else { alphaLabel.setForeground(Color.BLACK); betaLabel.setForeground(Color.BLACK); gammaLabel.setForeground(Color.BLACK); gaussianLabel.setForeground(Color.BLACK); } } }); 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(grayHistBtn); this.getContentPane().add(yuvBtn); this.getContentPane().add(alphaLabel); this.getContentPane().add(alphaBar); this.getContentPane().add(betaLabel); this.getContentPane().add(betaBar); this.getContentPane().add(gammaLabel); this.getContentPane().add(gammaBar); this.getContentPane().add(addGaussianBtn); this.getContentPane().add(gaussianLabel); this.getContentPane().add(gaussianBar); this.getContentPane().add(imageView); setIcon(srcMat); } private void grayHistPic() { if (isGrayHist) { Mat grayMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.cvtColor(srcMat, grayMat, Imgproc.COLOR_BGR2GRAY); // 须先得到灰度图 Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.equalizeHist(grayMat, dstMat); // 直方图均衡化 setIcon(dstMat); } else { setIcon(srcMat); } } private void bgrPic() { Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); if (isAddGaussian) { Mat blurMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.GaussianBlur(srcMat, blurMat, new Size(gaussian, gaussian), 0, 0); // 高斯模糊 Core.addWeighted(blurMat, alpha, srcMat, beta, gamma, dstMat); // 权重 } else { ArrayList<Mat> bgrList = new ArrayList<>(3); Core.split(srcMat, bgrList); // 将 BGR 3个通道拆分 for (int i = 0; i < 3; i++) { Imgproc.equalizeHist(bgrList.get(i), bgrList.get(i)); // 对每个通道直方图均衡化 } Mat bgrMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Core.merge(bgrList, bgrMat); // 合并通道 Core.addWeighted(srcMat, alpha, bgrMat, beta, gamma, dstMat); // 添加权重 } setIcon(dstMat); } private void yuvPic() { if (isYUV) { Mat yuvMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Imgproc.cvtColor(srcMat, yuvMat, Imgproc.COLOR_RGB2YUV); // 转换为YUV色彩格式 ArrayList<Mat> yuvList = new ArrayList<>(3); Core.split(yuvMat, yuvList); // 将YUV按3个通道拆分。Y-亮度,只显示Y的话,图像看起来会是一张黑白照。U-色度分量:是照片蓝色部分去掉亮度。V-色度分量:是照片红色部分去掉亮度。 Imgproc.equalizeHist(yuvList.get(0), yuvList.get(0)); // 对Y通道直方图均衡化。 Mat dstMat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); Core.merge(yuvList, dstMat); // 合并通道 Imgproc.cvtColor(dstMat, dstMat, Imgproc.COLOR_YUV2RGB); // 转换为RGB色彩格式 setIcon(dstMat); } else { setIcon(srcMat); } } 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/3288.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设