JAVA와 OpenCV를 이용하여 이미지 평활화(Equalization)와 이진화(Binarization)를 해보겠습니다.
평활화(Equalization)
public static void showEqualizedGrayLenna() {
try {
String locGrayLenna = "경로생략/LennaGray.png";
Mat imageGrayLenna = Imgcodecs.imread(locGrayLenna);
Imgproc.cvtColor(imageGrayLenna, imageGrayLenna, Imgproc.COLOR_BGR2GRAY);
Mat imageEqualizedGrayLenna = new Mat();
// Equalize Start
Imgproc.equalizeHist(imageGrayLenna, imageEqualizedGrayLenna);
// Equalize End
// Show
HighGui.imshow("LennaEqualizedGray", imageEqualizedGrayLenna);
} catch (Exception e) {
e.printStackTrace();
}
}
이진화(Binarization)
public static void showBinarizationGrayLenna() {
try {
String locGrayLenna = "경로생략/LennaGray.png";
Mat imageGrayLenna = Imgcodecs.imread(locGrayLenna);
Mat imageBinarizationGrayLenna = new Mat();
// Binarization Start
Imgproc.threshold(imageGrayLenna, imageBinarizationGrayLenna
, 127, 255, Imgproc.THRESH_BINARY);
// Binarization End
// Show
HighGui.imshow("LennaBinarizationGray", imageBinarizationGrayLenna);
} catch (Exception e) {
e.printStackTrace();
}
}
이진화(Binarization) - 보너스 GUI
이진화에 트랙바를 추가하여 임계값(Threshold)을 조절하는 프로그램입니다.
public static void showBinarizationGrayLennaWithTrackbar() {
try {
String locGrayLenna = "경로생략/LennaGray.png";
Mat imageGrayLenna = Imgcodecs.imread(locGrayLenna);
Mat imageBinarizationGrayLenna = new Mat();
// Binarization Start
Imgproc.threshold(imageGrayLenna, imageBinarizationGrayLenna
, 127, 255, Imgproc.THRESH_BINARY);
// Binarization End
// Show
JFrame trackbarWindow = new JFrame("BinarizationGrayLennaWithTrackbar");
trackbarWindow.setBounds(100, 100, 528, 582);
trackbarWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
trackbarWindow.getContentPane().setLayout(null);
// 이미지
Image img = HighGui.toBufferedImage(imageBinarizationGrayLenna);
JLabel imgLabel = new JLabel(new ImageIcon(img));
imgLabel.setBounds(0, 31, 512, 512);
trackbarWindow.getContentPane().add(imgLabel);
// 임계값 텍스트
JLabel levelLabel = new JLabel("임계값 : " + 127);
levelLabel.setBounds(10, 10, 75, 15);
trackbarWindow.getContentPane().add(levelLabel);
// 트랙바
JSlider slider = new JSlider(JSlider.HORIZONTAL, 0, 255, 127);
slider.setBounds(86, 5, 410, 26);
slider.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
int level = (int)source.getValue();
//System.out.println(level);
// Binarization Start
Imgproc.threshold(imageGrayLenna, imageBinarizationGrayLenna
,level, 255, Imgproc.THRESH_BINARY);
// Binarization End
Image img = HighGui.toBufferedImage(imageBinarizationGrayLenna);
levelLabel.setText("임계값 : " + level);
imgLabel.setIcon(new ImageIcon(img));
trackbarWindow.repaint();
}
});
trackbarWindow.getContentPane().add(slider, BorderLayout.NORTH);
trackbarWindow.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
'프로그래밍 > OpenCV.' 카테고리의 다른 글
[OpenCV] 06.. SIFT Key Point, 기술자 추출(feat.JAVA) (0) | 2020.11.02 |
---|---|
[OpenCV] 05.. Canny Edge와 Sobel Edge(feat.JAVA) (2) | 2020.11.02 |
[OpenCV] 03.. OpenCV 히스토그램(feat.JAVA) (0) | 2020.10.17 |
[OpenCV] 02.. Gray Scale과 Resize (2) | 2020.09.20 |
[OpenCV] 01.. JAVA로 OpenCV 시작하기 (3) | 2020.09.20 |
댓글