Imgproc.cvtColor
Imgproc.threshold
Imgproc.adaptiveThreshold
Imgproc.medianBlur
Core.addWeighted
工具类见 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 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.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 Transcolor extends JFrame { private JLabel imageView; private double threshold = 0; private double maxval = 0; private int type = 0; private int adaptiveMethod = 0; private int thresholdType = 0; private int blockSize = 3; private double C = 0; private boolean isAdaptiveMethod = false; private int sharpness = 0; private boolean isSharpness = false; private Mat srcMat; public Transcolor(String path) { JLabel thresholdLabel = new JLabel("阈值:" + threshold); thresholdLabel.setBounds(15, 10, 120, 15); JSlider thresholdBar = new JSlider(1, 600); thresholdBar.setValue((int) threshold); thresholdBar.setBounds(140, 5, 220, 25); thresholdBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { threshold = thresholdBar.getValue() * 1.0d; thresholdLabel.setText("阈值:" + threshold); setPic(); } }); JLabel maxvalLabel = new JLabel("最大值:" + maxval); maxvalLabel.setBounds(15, 40, 120, 15); JSlider maxvalBar = new JSlider(1, 600); maxvalBar.setValue((int) maxval); maxvalBar.setBounds(140, 35, 220, 25); maxvalBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { maxval = maxvalBar.getValue() * 1.0d; maxvalLabel.setText("最大值:" + maxval); setPic(); } }); JLabel typeLabel = new JLabel("类型:CV_8UC1," + type); typeLabel.setBounds(15, 70, 120, 15); JSlider typeBar = new JSlider(0, 6); typeBar.setValue((int) type); typeBar.setBounds(140, 65, 220, 25); typeBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { type = typeBar.getValue(); // 数据类型:U(unsigned integer)无符号整数,S(signed integer)有符号整数,F(float)浮点数 // 通道数:1-4,(5)-(8)。 // 位数:8/16/32/64。8和16位只匹配数据类型U和S,32位只匹配S和F,64位只匹配F // 类型:CV_+位数+数据类型+C通道数 // org.opencv.core.CvType switch (type) { case 0: { typeLabel.setText("类型:CV_8UC1," + CvType.CV_8UC1); break; } case 1: { typeLabel.setText("类型:CV_8SC1," + CvType.CV_8SC1); break; } case 2: { typeLabel.setText("类型:CV_16UC1," + CvType.CV_16UC1); break; } case 3: { typeLabel.setText("类型:CV_16SC1," + CvType.CV_16SC1); break; } case 4: { typeLabel.setText("类型:CV_32SC1," + CvType.CV_32SC1); break; } case 5: { typeLabel.setText("类型:CV_32FC1," + CvType.CV_32FC1); break; } case 6: { typeLabel.setText("类型:CV_64FC1," + CvType.CV_64FC1); break; } } setPic(); } }); JLabel adaptiveMethodLabel = new JLabel("临界值算法:Imgproc.ADAPTIVE_THRESH_MEAN_C," + adaptiveMethod); adaptiveMethodLabel.setBounds(15, 125, 360, 15); JSlider adaptiveMethodBar = new JSlider(0, 1); adaptiveMethodBar.setValue(adaptiveMethod); adaptiveMethodBar.setBounds(380, 120, 120, 25); adaptiveMethodBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { adaptiveMethod = adaptiveMethodBar.getValue(); // ADAPTIVE_THRESH_MEAN_C 局部邻域块的平均值,先求出块中的均值,再减去常数C // ADAPTIVE_THRESH_GAUSSIAN_C 该算法是在区域中(x, y)周围的像素根据高斯函数按照它们离中心点的距离进行加权计算,再减去常数C。 switch (adaptiveMethod) { case 0: { adaptiveMethodLabel.setText("临界值算法:Imgproc.ADAPTIVE_THRESH_MEAN_C," + adaptiveMethod); break; } case 1: { adaptiveMethodLabel.setText("临界值算法:Imgproc.ADAPTIVE_THRESH_GAUSSIAN_C," + adaptiveMethod); break; } } if (isAdaptiveMethod) setPic(); } }); JLabel thresholdTypeLabel = new JLabel("临界值类型:Imgproc.THRESH_BINARY," + thresholdType); thresholdTypeLabel.setBounds(15, 155, 360, 15); JSlider thresholdTypeBar = new JSlider(0, 1); thresholdTypeBar.setValue(thresholdType); thresholdTypeBar.setBounds(380, 150, 120, 25); thresholdTypeBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { thresholdType = thresholdTypeBar.getValue(); // THRESH_BINARY 表示阈值的二值化操作,大于阈值使用maxval表示,小于阈值使用0表示 // THRESH_BINARY_INV 表示阈值的二值化翻转操作,大于阈值的使用0表示,小于阈值的使用最大值表示 switch (thresholdType) { case 0: { thresholdTypeLabel.setText("临界值类型:Imgproc.THRESH_BINARY," + thresholdType); break; } case 1: { thresholdTypeLabel.setText("临界值类型:Imgproc.CV_THRESH_BINARY_INV," + thresholdType); break; } } if (isAdaptiveMethod) setPic(); } }); JLabel blockSizeLabel = new JLabel("像素临界范围:" + blockSize); blockSizeLabel.setBounds(15, 185, 120, 15); JSlider blockSizeBar = new JSlider(3, 300); blockSizeBar.setValue(blockSize); blockSizeBar.setBounds(140, 180, 220, 25); blockSizeBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = blockSizeBar.getValue(); if (value % 2 == 0) { value = value + 1; } blockSize = value; // 必须是大于1的奇数 blockSizeLabel.setText("像素临界范围:" + blockSize); if (isAdaptiveMethod) setPic(); } }); JCheckBox adaptiveMethodBtn = new JCheckBox("高级(灰度图自动临界算法)"); adaptiveMethodBtn.setBounds(40, 95, 200, 25); adaptiveMethodBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { isAdaptiveMethod = !isAdaptiveMethod; if (isAdaptiveMethod) { maxvalLabel.setForeground(Color.RED); adaptiveMethodLabel.setForeground(Color.RED); thresholdTypeLabel.setForeground(Color.RED); blockSizeLabel.setForeground(Color.RED); } else { maxvalLabel.setForeground(Color.BLACK); adaptiveMethodLabel.setForeground(Color.BLACK); thresholdTypeLabel.setForeground(Color.BLACK); blockSizeLabel.setForeground(Color.BLACK); } } }); JCheckBox sharpnessBtn = new JCheckBox("锐化"); sharpnessBtn.setBounds(40, 210, 200, 25); sharpnessBtn.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { isSharpness = !isSharpness; } }); JLabel sharpnessLabel = new JLabel("锐化:" + sharpness); sharpnessLabel.setBounds(15, 235, 60, 15); JSlider sharpnessBar = new JSlider(1, 90); // 81 sharpnessBar.setValue(sharpness); sharpnessBar.setBounds(80, 235, 220, 25); sharpnessBar.addChangeListener(new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { int value = sharpnessBar.getValue(); if (value % 2 == 0) { value = value + 1; } sharpness = value; // 必须是奇数 sharpnessLabel.setText("锐化:" + sharpness); if (isSharpness) setPic(); } }); srcMat = Imgcodecs.imread(path); int width = srcMat.width(); int height = srcMat.height(); imageView = new JLabel(""); imageView.setBounds(40, 270, width, height); this.setTitle("图片二值化、锐化"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(width + 100, height + 320); this.getContentPane().setLayout(null); this.getContentPane().add(thresholdLabel); this.getContentPane().add(thresholdBar); this.getContentPane().add(maxvalLabel); this.getContentPane().add(maxvalBar); this.getContentPane().add(typeLabel); this.getContentPane().add(typeBar); this.getContentPane().add(adaptiveMethodBtn); this.getContentPane().add(adaptiveMethodLabel); this.getContentPane().add(adaptiveMethodBar); this.getContentPane().add(blockSizeLabel); this.getContentPane().add(blockSizeBar); this.getContentPane().add(thresholdTypeLabel); this.getContentPane().add(thresholdTypeBar); this.getContentPane().add(sharpnessBtn); this.getContentPane().add(sharpnessLabel); this.getContentPane().add(sharpnessBar); this.getContentPane().add(imageView); setIcon(srcMat); } private void setPic() { Mat mat = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); if (isAdaptiveMethod) { Mat tmp = new Mat(srcMat.rows(), srcMat.cols(), srcMat.type()); // 须先得到灰度图 Imgproc.cvtColor(srcMat, tmp, Imgproc.COLOR_BGR2GRAY); Imgproc.adaptiveThreshold(tmp, mat, maxval, adaptiveMethod, thresholdType, blockSize, C); setIcon(mat); return; } if (isSharpness) { Imgproc.medianBlur(srcMat, mat, sharpness); // 中间值模糊 // Mat src1, double alpha, Mat src2, double beta, double gamma, Mat dst Core.addWeighted(srcMat, 2.1, mat, -1.1, 0, mat); // 图片叠加 src1*alpha+src2*beta+gamma setIcon(mat); return; } Imgproc.threshold(srcMat, mat, threshold, maxval, type); 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/3239.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设