画像アフィン変換クラス

Namespace: FVIL.GeoTrans
Assembly: FVILbasic (in FVILbasic.dll) Version: 3.1.0.0 (3.1.0.17)

Syntax

C#
[SerializableAttribute]
public class CFviAffine : CFviGeoTrans
Visual Basic
<SerializableAttribute>
Public Class CFviAffine
	Inherits CFviGeoTrans

Remarks

マルチコア対応:

このクラスの Execute()()()() メソッドは、 SetParallelNum(Int32) によって設定されたスレッド数に従って、 自動的に処理を並列化します。 マルチコア環境においては、処理を並列化する事によりレーテンシを短縮できる場合があります。 詳しくは、 SetParallelNum(Int32) の説明をご参照ください。


画像データのアフィン変換を行うクラスです。

本クラスはユーザが設定した行列(m)に従って下記の座標変換を行います。
ここで、行例(m)は Matrix を意味します。
出力座標X = (入力座標X * m[0][0]) + (入力座標Y * m[0][1]) + m[0][2]
出力座標Y = (入力座標X * m[1][0]) + (入力座標Y * m[1][1]) + m[1][2]

座標変換に使用する行列は 3x3 の正方行列である必要が有ります。 また、下記の条件を満たしている必要が有ります。

  • 逆行列が計算できること。
  • m[2][0]= 0
  • m[2][1]= 0
  • m[2][2]= 1

行列の初期値は下記の通りです。
---------------
|0.0|0.0|0.0|
---------------
|0.0|0.0|0.0|
---------------
|0.0|0.0|1.0|
---------------


濃度補間方法:

濃度補間方法は、以下の5種類から選択できます。


処理対象画像の画像種別が ImageType.BIN の場合は、 濃度補間方法の設定は無視され SamplingMode.NearestNeighbor で 処理した場合と等価になります。


要求する入出力画像数:
項目定数
入力画像1SrcImageCount フィールド
出力画像2DstImageCount フィールド

処理対象画像の条件:

処理対象の画像は下記の条件を満たしている必要が有ります。

  • 入力画像と出力画像の画像種別、チャネル数が等しい事
  • 処理対象の画像種別 -
  • マスク画像(2つ目の出力画像)は、以下の条件を満たしている必要があります
    • 出力画像の処理範囲サイズと一致している事
    • 画像種別が BIN である事
    • チャネル数が 1 である事
入力画像と出力画像の処理範囲サイズは異なっていても構いません。
マスク画像は nullptr を指定することで省略できます。
処理対象画像の有効性検査と有効化:

入力画像の有効性検査 このクラスの CheckValidity(CFviImage) 関数で入力画像の有効性を検査できます。 この関数はスタティックメンバですので、このクラスのインスタンスを生成せずに直接呼び出す事ができます。
入出力画像の整合性検査 このクラスの IsValid()()()() メソッドで入出力画像の整合性を検査できます。 このメソッドが false を返す場合は、前述の処理対象画像の条件を満たしていない為、 実行できない事を意味します。
出力画像の有効化 このクラスの Validate(Int32) メソッドで出力画像を有効化する事ができます。 このメソッドを実行すると、出力画像の領域サイズ、画像種別、チャネル数を適切なものに設定します。 領域サイズは、このメソッドの引数によって以下のように調整できます。
  • Validate(0) : 入力画像の処理範囲サイズに応じて、出力画像の画像領域を調整します。
  • Validate(1) : 変換行列に応じて、出力画像の画像領域を調整します。


初期値と範囲:
プロパティ初期値範囲
ClearBackModetruetrue/false
Matrix※クラスの説明を参照※クラスの説明を参照
SamplingModeSamplingMode.NearestNeighborSamplingModeの定数

関連する FIE 関数:

fnFIE_geotrans_affine

Examples

【入力画像】【出力画像】

ソースコード:
C# Copy imageCopy
//    $Revision: 1.3 $

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using fvalgcli;    // FvPluginXXXX attribute requires fvalgcli

namespace User.SampleCode
{
    public partial class GeoTrans
    {
        // ============================================================
        /// <summary>
        /// 画像アフィン変換クラス.
        /// </summary>
        [FvPluginExecute]
        public void Affine()
        {
            // 1) インスタンスの準備.
            FVIL.Data.CFviImage src = new FVIL.Data.CFviImage();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();

            // 2) 処理対象画像の取り込み.
            FVIL.File.Function.LoadImageFile(Defs.TestImageDir + "/fvc06_UC8.jpg", src, FVIL.PixelMode.Unpacking);

            // 3) 画像処理準備 (共通)
            FVIL.GeoTrans.CFviAffine parser = new FVIL.GeoTrans.CFviAffine();
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;

            // 4) パラメータ設定 (固有)
            {
                // --- 回転角.
                FVIL.Data.CFviAngle angle = new FVIL.Data.CFviAngle(90);

                // --- 回転後の各頂点.
                FVIL.Data.CFviRectangle clip = new FVIL.Data.CFviRectangle(0, 0, src.Window.Width, src.Window.Height);
                clip.Angle = angle;
                FVIL.Data.CFviPolyline vertex = clip.ToCFviPolyline();

                FVIL.Data.CFviPoint st = vertex[0];
                FVIL.Data.CFviPoint ed = vertex[0];
                for (int i = 0; i < vertex.Count; i++)
                {
                    if (st.X > vertex[i].X) st.X = vertex[i].X;
                    if (st.Y > vertex[i].Y) st.Y = vertex[i].Y;
                    if (ed.X < vertex[i].X) ed.X = vertex[i].X;
                    if (ed.Y < vertex[i].Y) ed.Y = vertex[i].Y;
                }

                // --- 回転後の外接矩形サイズ.
                int horz = (int)(Math.Abs(ed.X - st.X) + 0.9);    // 幅.
                int vert = (int)(Math.Abs(ed.Y - st.Y) + 0.9);    // 高.

                double R = angle.Radian;
                double em11 = +Math.Cos(R);
                double em12 = -Math.Sin(R);
                double em21 = +Math.Sin(R);
                double em22 = +Math.Cos(R);
                double axis_x = -0.5;
                double axis_y = -0.5;

                // --- 行列. (回転)
                FVIL.Data.CFviMatrix mr = new FVIL.Data.CFviMatrix(3, 3);
                mr[0, 0] = +Math.Cos(R); mr[0, 1] = -Math.Sin(R); mr[0, 2] = 0.0;
                mr[1, 0] = +Math.Sin(R); mr[1, 1] = +Math.Cos(R); mr[1, 2] = 0.0;
                mr[2, 0] = 0.0;          mr[2, 1] = 0.0;          mr[2, 2] = 1.0;

                // --- 行列. (平行移動)
                FVIL.Data.CFviMatrix ms = new FVIL.Data.CFviMatrix(3, 3);
                ms[0, 0] = 1.0; ms[0, 1] = 0.0; ms[0, 2] = Math.Abs(Math.Min(st.X, 0.0));
                ms[1, 0] = 0.0; ms[1, 1] = 1.0; ms[1, 2] = Math.Abs(Math.Min(st.Y, 0.0));
                ms[2, 0] = 0.0; ms[2, 1] = 0.0; ms[2, 2] = 1.0;

                // --- 行列. (画素中心を画素左上へ移動)
                FVIL.Data.CFviMatrix mp = new FVIL.Data.CFviMatrix(3, 3);
                mp[0, 0] = 1.0; mp[0, 1] = 0.0; mp[0, 2] = (axis_x - axis_x * em11 - axis_y * em12);
                mp[1, 0] = 0.0; mp[1, 1] = 1.0; mp[1, 2] = (axis_y - axis_x * em21 - axis_y * em22);
                mp[2, 0] = 0.0; mp[2, 1] = 0.0; mp[2, 2] = 1.0;

                // --- 行列.
                FVIL.Data.CFviMatrix mm = mp * ms * mr;
                parser.Matrix.CopyFrom(mm);
            }

            // -) 有効化.
            parser.Validate(1);    // 1:行列の内容に応じる.

            // 5) 画像処理実行.
            parser.Execute();

            // E) 処理結果画像の保存.
            FVIL.File.Function.SaveImageFile(Defs.ResultDir + "/GeoTrans.Affine.png", dst);
        }
    }
}


Visual Basic Copy imageCopy
'    $Revision: 1.1 $

Imports System.Collections.Generic
Imports System.Text
Imports System.Drawing
Imports fvalgcli
' FvPluginXXXX attribute requires fvalgcli
Namespace SampleCode
    Public Partial Class GeoTrans
        ' ============================================================
        ''' <summary>
        ''' 画像アフィン変換クラス.
        ''' </summary>
        <FvPluginExecute> _
        Public Sub Affine()
            ' 1) インスタンスの準備.
            Dim src As New FVIL.Data.CFviImage()
            Dim dst As New FVIL.Data.CFviImage()

            ' 2) 処理対象画像の取り込み.
            FVIL.File.[Function].LoadImageFile(Defs.TestImageDir & "/fvc06_UC8.jpg", src, FVIL.PixelMode.Unpacking)

            ' 3) 画像処理準備 (共通)
            Dim parser As New FVIL.GeoTrans.CFviAffine()
            parser.SrcImages(0) = src
            parser.DstImages(0) = dst

            ' 4) パラメータ設定 (固有)
            If True Then
                ' --- 回転角.
                Dim angle As New FVIL.Data.CFviAngle(90)

                ' --- 回転後の各頂点.
                Dim clip As New FVIL.Data.CFviRectangle(0, 0, src.Window.Width, src.Window.Height)
                clip.Angle = angle
                Dim vertex As FVIL.Data.CFviPolyline = clip.ToCFviPolyline()

                Dim st As FVIL.Data.CFviPoint = vertex(0)
                Dim ed As FVIL.Data.CFviPoint = vertex(0)
                For i As Integer = 0 To vertex.Count - 1
                    If st.X > vertex(i).X Then
                        st.X = vertex(i).X
                    End If
                    If st.Y > vertex(i).Y Then
                        st.Y = vertex(i).Y
                    End If
                    If ed.X < vertex(i).X Then
                        ed.X = vertex(i).X
                    End If
                    If ed.Y < vertex(i).Y Then
                        ed.Y = vertex(i).Y
                    End If
                Next

                ' --- 回転後の外接矩形サイズ.
                Dim horz As Integer = CInt(Math.Truncate(Math.Abs(ed.X - st.X) + 0.9))
                ' 幅.
                Dim vert As Integer = CInt(Math.Truncate(Math.Abs(ed.Y - st.Y) + 0.9))
                ' 高.
                Dim R As Double = angle.Radian
                Dim em11 As Double = +Math.Cos(R)
                Dim em12 As Double = -Math.Sin(R)
                Dim em21 As Double = +Math.Sin(R)
                Dim em22 As Double = +Math.Cos(R)
                Dim axis_x As Double = -0.5
                Dim axis_y As Double = -0.5

                ' --- 行列. (回転)
                Dim mr As New FVIL.Data.CFviMatrix(3, 3)
                mr(0, 0) = +Math.Cos(R)
                mr(0, 1) = -Math.Sin(R)
                mr(0, 2) = 0.0
                mr(1, 0) = +Math.Sin(R)
                mr(1, 1) = +Math.Cos(R)
                mr(1, 2) = 0.0
                mr(2, 0) = 0.0
                mr(2, 1) = 0.0
                mr(2, 2) = 1.0

                ' --- 行列. (平行移動)
                Dim ms As New FVIL.Data.CFviMatrix(3, 3)
                ms(0, 0) = 1.0
                ms(0, 1) = 0.0
                ms(0, 2) = Math.Abs(Math.Min(st.X, 0.0))
                ms(1, 0) = 0.0
                ms(1, 1) = 1.0
                ms(1, 2) = Math.Abs(Math.Min(st.Y, 0.0))
                ms(2, 0) = 0.0
                ms(2, 1) = 0.0
                ms(2, 2) = 1.0

                ' --- 行列. (画素中心を画素左上へ移動)
                Dim mp As New FVIL.Data.CFviMatrix(3, 3)
                mp(0, 0) = 1.0
                mp(0, 1) = 0.0
                mp(0, 2) = (axis_x - axis_x * em11 - axis_y * em12)
                mp(1, 0) = 0.0
                mp(1, 1) = 1.0
                mp(1, 2) = (axis_y - axis_x * em21 - axis_y * em22)
                mp(2, 0) = 0.0
                mp(2, 1) = 0.0
                mp(2, 2) = 1.0

                ' --- 行列.
                Dim mm As FVIL.Data.CFviMatrix = mp * ms * mr
                parser.Matrix.CopyFrom(mm)
            End If

            ' -) 有効化.
            parser.Validate(1)
            ' 1:行列の内容に応じる.
            ' 5) 画像処理実行.
            parser.Execute()

            ' E) 処理結果画像の保存.
            FVIL.File.[Function].SaveImageFile(Defs.ResultDir & "/GeoTrans.Affine.png", dst)
        End Sub
    End Class
End Namespace

Inheritance Hierarchy

See Also