2013년 1월 26일 토요일

OpenCV #6-3 Example (에지를 감시하기 위한 방향성 필터 적용)

 - 반대 변환을 수행함으로써 영상 내 고주파 내용을 증폭시킴.
 - 고주파 필터를 이용해서 에지 감지를 수행.

 - 소벨(Sobel) 필터는 방향성 필터이다.
 - 사용하는 필터의 커널에 따라 수직 영상 주파수나 수평 영상 주파수에 영향을 미침.

  • Example
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

int main()
{
cv::Mat image= cv::imread("boldt.jpg", 0);
cv::namedWindow("Original Image");
cv::imshow("Original Image",image);

cv::Mat sobelX, sobelY;

cv::Sobel(image, sobelX, CV_8U, 1, 0, 3, 0.4, 128); // 수평 필터 호출
cv::Sobel(image, sobelY, CV_8U, 0, 1, 3, 0.4, 128); // 수직 필터 호출
// 0값은 128인 그레이레벨에 해당
// 음수 값은 어두운 화소로 표현, 양수 값은 밝은 값을 표현

cv::namedWindow("Sobel X image");
cv::imshow("Sobel X image",sobelX);
cv::namedWindow("Sobel Y image");
cv::imshow("Sobel Y image",sobelY);

// 소벨 커널은 양수와 음수 값을 가짐
// 소벨 필터의 결과는 일반적으로 16비트 부호 있는 정수영상(CV_16S)에서 계산
// 소벨 놈 계산 (수직과 수평 두 결과의 소벨 필터 놈을 얻기 위해 조합)
cv::Sobel(image, sobelX, CV_16S, 1, 0);
cv::Sobel(image, sobelY, CV_16S, 0, 1);
cv::Mat sobel;
sobel = abs(sobelX)+abs(sobelY); // L1 놈 계산

// 소벨 놈은 0인 값을 흰색으로, 높은 값을 어두운 회색 음영으로 할당한 영상을 얻기 위해
// converTo 메소드의 부가적인 재스케일링 파라미터를 사용
double sobmin, sobmax;
cv::minMaxLoc(sobel, &sobmin, &sobmax); // 소벨 최댓값 찾기
// sobelImage = -alpha*sobel + 255 // 8비트 영상 변환
cv::Mat sobelImage;
sobel.convertTo(sobelImage, CV_8U, -255./sobmax, 255);

cv::namedWindow("Sobel Image");
cv::imshow("Sobel Image",sobelImage);

// 영상의 경계값을 처리
cv::Mat sobelThresholded;
cv::threshold(sobelImage, sobelThresholded, 225, 255, cv::THRESH_BINARY);
// 소벨 놈에 대한 경계값 적용(낮은 경계값)

cv::namedWindow("Binary Sobel Image (low)");
cv::imshow("Binary Sobel Image (low)",sobelThresholded);

cv::threshold(sobelImage, sobelThresholded, 190, 255, cv::THRESH_BINARY);
// 소벨 놈에 대한 경계값 적용(높은 경계값)

cv::namedWindow("Binary Sobel Image (high)");
cv::imshow("Binary Sobel Image (high)",sobelThresholded);

cv::waitKey(0);

return 0;
}


  • Result

  • 참고문헌 : OpenCV 2 Computer Vision Application Programming Cookbook