8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OpenCVでアルファマスキング

Posted at

画像をある形状で切り抜きたいときなどにアルファマスキングと呼ばれる手法が使われます。OpenCVのmixChannelsを使ってこれを実現するためのメモです。

// 入力画像(3チャンネル)
cv::Mat src = cv::imread("input.jpg");

// マスク画像(グレースケール)
cv::Mat mask(300, 300, CV_8UC1);

// マスク領域を作成
cv::Point points[] = {
    cv::Point(10, 100),
    cv::Point(150, 20),
    cv::Point(240, 200),
    cv::Point(30, 280)
};
cv::fillConvexPoly(mask, points, 4, cv::Scalar(255), CV_AA);

// 出力画像(※先に領域の確保が必要)
cv::Mat dat(300, 300, CV_8UC4);

// 入力する画像の配列
cv::Mat srcArray[] = { cvSrc, mask };
// チャンネルの変換方法の指定
int from_to[] = { 0,0, 1,1, 2,2, 3,3 };
cv::mixChannels(srcArray, 2, &dst, 1, from_to, 4);

Screen Shot 2015-11-30 at 09.47.24.png

ふつうにcv::copyToなどでマスクを指定すると、マスク画像の黒でない領域を単純にコピーしてしまうのでジャギーが残ってしまうのですが、この方法でやればアンチエイリアスも保持されてきれいな画像になります。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?