LoginSignup
0
0

More than 3 years have passed since last update.

OpenCVforUnity 画像フリップ処理について

Posted at

はじめに

この記事ではOpenCVforUnity(有償)を使用しています。
iOSアーカイブ目的の際はこちらをお勧めします。(Appleのbitcode変換も問題なく通るのでアップ後のアーキテクチャ対応の心配ありません)

Unityで画像を保存する際にフリップ処理したいなんて時があると思います。RendereTextreで工夫したり、ピクセル処理することもできるかと思いますが。今回はOpenCVを使います。

今回はこの結果を目指します。Texture2DをX軸,Y軸,XY軸に対してフリップ処理します。
スクリーンショット 2020-12-06 13.20.05.png
といっても、することはPythonで使用する時と変わりません。
Texture2DをMatへ変換してFlip変換して再びTexture2Dへ返しているだけです。

実装方法

OpenCVManager.cs
using UnityEngine;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnity.ImgprocModule;

public class OpenCVManager
{
    /// <summary>
    /// Texture2Dをフリップ回転処理 FlipNum = -1(XY), 0(X), 1(Y)
    /// </summary>
    public static Texture2D FlipToTex2D(Texture2D tex, int flipNum)
    {
        Mat origin = new Mat(tex.height, tex.width, CvType.CV_8UC4);
        Utils.texture2DToMat(tex, origin);
        Core.flip(origin, origin, flipNum);
        Texture2D texture = new Texture2D(origin.cols(), origin.rows(), TextureFormat.RGBA32, false);
        Utils.matToTexture2D(origin, texture);
        return texture;
    }
}

まとめ

名前空間上のどのクラスに必要な関数があるのか若干わかりにくい感じはします。
後発系のOpenCVplusUnity(無料)の方が使用感的には使いやすい印象です。
癖があるだけで慣れてしまえばどちらも同じです。
アーカイブ目的であればOpenCVforUnityをおすすめします。

0
0
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
0
0