Skip to content

Instantly share code, notes, and snippets.

@YHaruoka
Created May 9, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YHaruoka/d224a9e552530b11469bd20d81f0609f to your computer and use it in GitHub Desktop.
Save YHaruoka/d224a9e552530b11469bd20d81f0609f to your computer and use it in GitHub Desktop.
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "引数不足(入力画像名, 閾値)" << std::endl;
return -1;
}
// 画像読込
std::string filename = argv[1];
std::cout << "入力画像 : " << filename.c_str() << std::endl;
cv::Mat input_img = cv::imread(filename.c_str(), -1);
if (input_img.empty() == true) {
std::cerr << "画像読込失敗 : " << filename.c_str() << std::endl;
return -1;
}
// 閾値読込
int th = std::stoi(argv[2]);
// チャネル数の確認
std::cout << "チャネル数 : " << input_img.channels() << std::endl;
// 閾値以上のAlpha値の部分を255、閾値以下のAlpha値の部分を0にするマスクの生成
int width = input_img.cols;
int height = input_img.rows;
cv::Mat1b alpha_msk = cv::Mat::zeros(height, width, CV_8UC1);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int alpha_value = input_img.at<cv::Vec4b>(y, x)[3];
if (alpha_value > th) {
alpha_msk.at<unsigned char>(y, x) = 255;
}
}
}
cv::imwrite("alpha_msk.jpg", alpha_msk);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment