Skip to content

Instantly share code, notes, and snippets.

@YHaruoka
Created Mar 21, 2019
Embed
What would you like to do?
#include <opencv2/opencv.hpp>
#include <ctime>
std::string input_image_name = "test.jpg";
int lbp_weights[8] = {128, 64, 32, 16, 8, 4, 2, 1};
int main(int argc, char* argv[]) {
// 入力画像の取得
cv::Mat input_image;
input_image = cv::imread(input_image_name, 0);
if (input_image.empty() == true) {
std::cerr << input_image_name << "が見つからない" << std::endl;
return -1;
}
std::cout << "Size(input_image) : " << input_image.cols << "×" << input_image.rows << std::endl;
cv::imshow("Input_image", input_image);
cv::waitKey(0);
// 出力用画像の用意
cv::Mat1b lbp_image(input_image.rows, input_image.cols);
// 入力画像の上下左右に1pixel分のPaddingを追加する
cv::Mat input_pad_image;
copyMakeBorder(input_image, input_pad_image, 1, 1, 1, 1, cv::BORDER_REPLICATE);
std::cout << "Size(input_pad_image) : " << input_pad_image.cols << "×" << input_pad_image.rows << std::endl;
// 計算時間測定用
int start = clock();
// LBP特徴量の計算
for (int y = 1; y < input_pad_image.rows - 1; y++) {
for (int x = 1; x < input_pad_image.cols - 1; x++) {
// 周辺画素入力用配列
int pixel_value[8] = { 0 };
int thresholded_value[8] = { 0 };
// 注目画素
int center_value = input_pad_image.at<unsigned char>(y, x);
// 周辺画素の値を取得
pixel_value[0] = input_pad_image.at<unsigned char>(y - 1, x - 1);
pixel_value[1] = input_pad_image.at<unsigned char>(y - 1, x);
pixel_value[2] = input_pad_image.at<unsigned char>(y - 1, x + 1);
pixel_value[3] = input_pad_image.at<unsigned char>(y, x + 1);
pixel_value[4] = input_pad_image.at<unsigned char>(y + 1, x + 1);
pixel_value[5] = input_pad_image.at<unsigned char>(y + 1, x);
pixel_value[6] = input_pad_image.at<unsigned char>(y + 1, x - 1);
pixel_value[7] = input_pad_image.at<unsigned char>(y, x - 1);
int lbp_result = 0;
// LBP値を計算(0~255)
for (int i = 0; i < 8; i++) {
if (pixel_value[i] >= center_value){
lbp_result += lbp_weights[i];
}
}
// 結果の取得
lbp_image.at<unsigned char>(y - 1, x - 1) = lbp_result;
}
}
// 計算時間測定用
int end = clock();
std::cout << "Processing time : " << end - start << "[ms]" << std::endl;
// 結果の保存と表示
imwrite("output.png", lbp_image);
cv::imshow("LBP_result_image", lbp_image);
cv::waitKey(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment