#include <opencv2/improc.hpp>
#include <opencv/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main()
{
	Mat src, dst1, dst2, dst3, dst4, 
    src = imread("disney.jpg", IMREAD_GRAYSCALE);
    
    threshold(src, dst1, 127, 255, THRESH_BINARY);
    
    Mat saltpepper_noise = Mat::zeros(src.rows, src.cols, CV_8U);
    randu(saltpepper_noise, 0, 255);
    
    Mat black = saltpepper_noise < 30;
    Mat white = saltpepper_noise > 225;
    
    Mat saltpepper_img = dst1.clone();
    saltpepper_img.setTo(255, white);
    saltpepper_img.setTo(0, black);
    
    imshow("1단계: 소금후추 노이즈 적용", saltpepper_img);
    
    Mat element = getStructuringElement(MORPH_ELLIPSE, Size(3,3));
    morphologyEx(saltpepper_img, dst2, MORPH_CLOSE, element);
    imshow("2단계: 모폴로지 닫힘 연산 적용", dst3);
    
    morphologyEx(dst2, dst3, MORPH_OPEN, element);
    imshow("3단계: 모폴로지 열림 적용", dst4);
    
    erode(dst3, dst4, element);
    imshow("4단계: 침식 적용", dst5);
    
    waitKey(0);
}

 

+ Recent posts