update: 2013/12/27
reference:
1. Amazon.com: Practical OpenCV eBook
2. Welcome to opencv documentation!
3. I touchs: Using OpenCV on Mac OS X
A. 前置作業:
1. 先依照 I touchs: Using OpenCV on Mac OS X 的說明, 建置好開發環境.
2. 需要加入到專案的 OpenCV 函式庫爲: (Add Files to Project...)
libopencv_core.dylib
libopencv_highgui.dylib
3. 將 main.cpp 的 main function 更名.(不作為程式執行的進入點)
//int main(int argc, const char * argv[])
int main_main(int argc, const char * argv[])
4. 爲專案新增 C++ 檔案:
點選專案 > New File... > OS X > C and C++ > C++ Class > Next >
Save as: detectHorizontalEdges.cpp > Create
5. 說明:
a. 使用 OpenCV 的 filter2D() 函式, 來作 kernel-based filtering.
b. 在 filter matrix(or kernel) 與被 kernel 涵蓋住的像素間, 會作
Element-wise multiplication(矩陣元素對應相乘)之和的運算.
c. filter2D() 函式使用的演算法:
DFT(Discrete Fourier transform)-based algorithm for large kernels;
direct algorithm for small kernels.
----------------------------------------------------------------------------------------
B. 撰寫程式:
1. 開啓 detectHorizontalEdges.h 檔案, 修改如下:
#ifndef __HelloOpenCV__detectHorizontalEdges__
#define __HelloOpenCV__detectHorizontalEdges__
#include <iostream>
//@add
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#endif /* defined(__HelloOpenCV__detectHorizontalEdges__) */
---------------------------------------------------------------------------------
2. 開啓 detectHorizontalEdges.cpp 檔案, 修改如下:
#include "detectHorizontalEdges.h"
using namespace std;
using namespace cv;
int main()
{
Mat img = imread("/Lanli/RD/Projects/OpenCV_Mac/HelloOpenCV/officeRoom.png", CV_LOAD_IMAGE_GRAYSCALE);
Mat img_filtered;
// Filter kernel for detecting vertical edges
//float vertical_fk[5][5] = {{0,0,0,0,0}, {0,0,0,0,0}, {-1,-2,6,-2,-1}, {0,0,0,0,0}, {0,0,0,0,0}};
/*
0 0 -1 0 0
0 0 -2 0 0
0 0 6 0 0
0 0 -2 0 0
0 0 -1 0 0
*/
//Mat filter_kernel = Mat(5, 5, CV_32FC1, vertical_fk);
// Filter kernel for detecting horizontal edges
float horizontal_fk[5][5] = {{0,0,-1,0,0}, {0,0,-2,0,0}, {0,0,6,0,0}, {0,0,-2,0,0}, {0,0,-1,0,0}};
/*
0 0 0 0 0
0 0 0 0 0
-1 -2 6 -2 -1
0 0 0 0 0
0 0 0 0 0
*/
Mat filter_kernel = Mat(5, 5, CV_32FC1, horizontal_fk); // for float 32 bits, 1 channel
// Apply filter
// -1: the output image will have the same depth as the source.
filter2D(img, img_filtered, -1, filter_kernel);
namedWindow("Image");
imshow("Image", img);
namedWindow("Filtered image");
imshow("Filtered image", img_filtered);
imwrite("/Lanli/RD/Projects/OpenCV_Mac/HelloOpenCV/officeRoom_filtered.png", img_filtered);
while (char(waitKey(1)) != 'q') {
}
return 0;
}
----------------------------------------------------------------------------------------
C. 執行結果:
原始圖片:
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。