Last active
December 23, 2015 14:19
-
-
Save RECS-Tsukuba/6648067 to your computer and use it in GitHub Desktop.
x軸方向のsobelフィルタのサンプル(C++)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 線形フィルタの動作確認用プログラム | |
// 実行する際は"input.png"というファイル名の画像データを用意する。 | |
//#include <cstdint> visual studio2008だとインクルードできないのでコメントアウト | |
#include <opencv2/opencv.hpp> | |
#include <opencv2/core/core.hpp> | |
#include <opencv2/highgui/highgui.hpp> | |
typedef unsigned char uint8_t; //cstdintがインクルードできないときはuint8_tをtypedefする | |
using cv::imread; | |
using cv::imwrite; | |
using cv::Mat; | |
Mat Filter(Mat input) { | |
if (input.data != NULL) { | |
// 出力画像を生成 | |
Mat filtered = input.clone(); | |
for (int x = 1; x < input.cols-1; ++x) { // 配列外アクセスが起こらないように、画像端は処理しないようにしている | |
for (int y = 1; y < input.rows-1; ++y) { | |
//============この部分を書き換える=============== | |
// ここでは例としてx軸方向のsobelフィルタの処理を記述する | |
// 注目画素と8近傍の画素を取得 | |
uint8_t I[3][3]; | |
for( int i = 0; i < 3; ++i ){ | |
for( int j = 0; j < 3; ++j ){ | |
I[i][j] = input.at<uint8_t>(y+i-1, x+j-1); | |
} | |
} | |
// 処理... | |
int temp1, temp2, out; | |
temp1 = -1 * I[0][0] - 2 * I[1][0] - 1*I[2][0]; | |
temp2 = 1 * I[0][1] + 2 * I[1][1] + 1 * I[2][2]; | |
out = abs( temp1 + temp2 ); // 和の絶対値をとる | |
out /= 4; // 4で割ることで10bitから8bitに | |
//=============================================== | |
// 画素を設定 | |
filtered.at<uint8_t>(y, x) = ( uint8_t )out; // uint8_t型にキャストして代入 | |
} | |
} | |
return filtered; | |
} else { return Mat(); } | |
} | |
int main(int argc, char** argv) { | |
// 出力 | |
imwrite("output.png", | |
// フィルタ | |
Filter( | |
// グレースケール画像として読み込み | |
imread("input.png", CV_LOAD_IMAGE_GRAYSCALE))); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment