正規化相関サーチクラス

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

Syntax

C#
[SerializableAttribute]
public class CFviGSearch : CFviImageParser
Visual Basic
<SerializableAttribute>
Public Class CFviGSearch
	Inherits CFviImageParser

Remarks

指定されたパタンを対象画像からサーチするクラスです。 指定されたパタン画像と、サーチ対象画像の部分画像との相互相関係数(マッチングの度合を示す情報) を求めて、その値がパラメータ指定値を超えた部分の座標値を出力します。

サーチ対象画像のサイズやサーチ結果個数によっては、サーチライブラリをオープンしなおす必要があります。 既定では、原画像の最大を 2048x2048、サーチ結果個数の最大を 1024 としてオープンしています。 これより大きい画像サイズやサーチ結果個数が必要な場合は、 Open(Int32, Int32, Int32, Int32) を実行してください。
現在の設定値は、下記のプロパティから取得できます。


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

処理対象画像の条件:

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

  • 画像種別が FVIL.ImageType.UC8 である事 -
  • チャネル数が 1 である事 -
  • 画像の縦サイズが 42 画素以上である事 -

初期値と範囲:
プロパティ初期値範囲
MaxOrgSizeX2048 1~ (Open(Int32, Int32, Int32, Int32) で決定します。)
MaxOrgSizeY2048 1~ (Open(Int32, Int32, Int32, Int32) で決定します。)
MaxObjNum1024 1~ (Open(Int32, Int32, Int32, Int32) で決定します。)
CashPwNum-1 -1,0~15 (Open(Int32, Int32, Int32, Int32) で決定します。)
ContinueOptionFVIL.GSearch.ContinueOption.DisagreePatternContinueOption に定義された定数
ParamCFviGSearchParam の初期値※インスタンスの差し替えはできません。
ResultCFviGSearchResult の初期値CFviGSearchResult のインスタンス

Examples

下記は、最も基本的な処理例を示します。
サーチパタン(FVIL.Data.CFviPattern)は、下図から MakePattern(CFviImage, CFviRectangle, CFviPoint) を行って保存していたものを使用しています。 サーチパタンの生成やファイル保存については、 CFviPattern のサンプルコードを参考にしてください。
処理結果の画像下に記述している処理時間は、Pentium4 3.2GHz 1.0GB RAM で実行した時のものです。 画像サイズは 320x240 です。処理対象画像の状態やパラメータの設定内容によって速度は変化します。


【処理結果】


【パタン画像】


【関数の出力】
User.SampleCode.GSearch.Search
execute. 2.54 msec, count=2 

ソースコード:
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 GSearch
    {
        /// <summary>
        /// サーチ実行.
        /// </summary>
        [FvPluginExecute]
        public void Search()
        {
            System.Reflection.MethodBase method = System.Reflection.MethodBase.GetCurrentMethod();
            Console.WriteLine("{0}.{1}", method.DeclaringType.FullName, method.Name);

            // 1) インスタンスの準備.
            FVIL.GSearch.CFviGSearch parser = new FVIL.GSearch.CFviGSearch();
            FVIL.Data.CFviImage src = new FVIL.Data.CFviImage();
            FVIL.Data.CFviPattern pattern = new FVIL.Data.CFviPattern();

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

            // 2-2) パタン画像の読み込み.
            pattern.Load(Defs.TestImageDir + "/clip.pat");

            // 3) 処理対象画像の有効性検査.
            if( FVIL.ErrorCode._SUCCESS != FVIL.GSearch.CFviGSearch.CheckValidity( src ) ) return;
            if( FVIL.ErrorCode._SUCCESS != FVIL.GSearch.CFviGSearch.CheckValidity( pattern ) ) return;

            // 4) 画像処理準備.
            // --- ライブラリのオープン.
            parser.Open(src.HorzSize, src.VertSize, 1024, -1);

            // --- 処理対象画像と結果格納用オブジェクトの設定.
            parser.SrcImages[0] = src;

            // 
            // ※パラメータを既定値から変更した部分は、コメントに [*] を記述しています.
            // 

            // 5-1) パラメータ設定.
            FVIL.GSearch.CFviGSearchParam    param = parser.Param;
            param.Count = 10;            // [*] サーチ個数.
                                        // [*] 精度: SuperHigh(超高精度)
            param.Precision = FVIL.GSearch.Precision.SuperHigh;
            param.Complexity = 1;        // [ ] 複雑度: 1~9
            param.Score1st = 4000;        // [*] 途中相関値閾値.
            param.Score2nd = 5000;        // [*] 最終相関値閾値.
            param.EdgeDetect = false;    // [ ] 処理範囲接触の許可 (false:非)

            // 5-E) サーチパタンのオープン.
            parser.OpenPattern( pattern, FVIL.GSearch.FilterMode.NormalPattern );

            // 6) 画像処理実行.
            FVIL.CFviTimeCounter timer = new FVIL.CFviTimeCounter();
            timer.Start();
            parser.Execute();
            double msec = timer.Stop();
            Console.WriteLine("execute. {0} msec, count={1} ", msec.ToString("0.###"), parser.Result.Count);

            // E) 確認用.
            {
                // 画像表示の準備.
                FVIL.GDI.CFviDisplay display = new FVIL.GDI.CFviDisplay();
                display.Image = src;
                display.DisplayRect =  src.Window;

                // オーバレイの生成.
                FVIL.GDI.CFviOverlay pOverlay0 = new FVIL.GDI.CFviOverlay();
                pOverlay0.Scaling = true;
                display.Overlays.Add(pOverlay0);

                FVIL.Data.CFviPoint        pat_mark = pattern.CenterMark;    // パタン登録時の基準点.
                FVIL.Data.CFviRectangle    pat_rect = pattern.RegistRect;    // パタン登録時の矩形.

                for( int i=0 ; i<parser.Result.Count ; i++ )
                {
                    FVIL.GSearch.CFviGSearchData data = parser.Result[i];

                    // サーチ結果の取得.
                    // --- 検出位置.
                    FVIL.Data.CFviPoint mark = data.Position;

                    // --- 検出位置とパタン情報から矩形を復元.
                    FVIL.Data.CFviRectangle    rect = new FVIL.Data.CFviRectangle();
                    rect.St = new FVIL.Data.CFviPoint(mark.X - pat_mark.X, mark.Y - pat_mark.Y);
                    rect.Ed = new FVIL.Data.CFviPoint(rect.X + pat_rect.Width, rect.Y + pat_rect.Height);

                    // --- 相関値.
                    System.String score = data.Score.ToString();

                    // 描画用.
                    // --- 矩形(塗り潰し)
                    FVIL.GDI.CFviGdiImage _fill = new FVIL.GDI.CFviGdiImage();
                    _fill.SetSize( 1, 1, 32 );
                    _fill.Position = rect.St;
                    _fill.Alpha = 0x3F;
                    _fill.StretchHorzSize = (int)(rect.Width);
                    _fill.StretchVertSize = (int)(rect.Height);
                    _fill.SetPixelRGB( 0, 0, Color.FromArgb(0xFF,0x00,0x00,0xFF) );    // A,R,G,B

                    // --- 矩形(枠)
                    FVIL.GDI.CFviGdiRectangle _rect = new FVIL.GDI.CFviGdiRectangle(rect);
                    _rect.Pen.Color = Color.FromArgb(0x00,0x00,0xFF);

                    // --- 基準点.
                    FVIL.GDI.CFviGdiPoint _mark = new FVIL.GDI.CFviGdiPoint(mark);
                    _mark.Size = new Size( 5, 5 );
                    _mark.Pen.Color = Color.FromArgb(0xFF,0x00,0x00);

                    // --- 相関値.
                    FVIL.GDI.CFviGdiString _score = new FVIL.GDI.CFviGdiString();
                    _score.Text = score.ToString();
                    _score.Position = mark;
                    _score.Align = FVIL.GDI.TextAlign.Right | FVIL.GDI.TextAlign.Bottom;
                    _score.BkMode = FVIL.GDI.BkMode.Transparent;
                    _score.Color = Color.FromArgb(0xFF,0xFF,0x00);

                    // 追加.
                    pOverlay0.Figures.Add(_fill);
                    pOverlay0.Figures.Add(_rect);
                    pOverlay0.Figures.Add(_mark);
                    pOverlay0.Figures.Add(_score);
                }

                // 保存.
                FVIL.Data.CFviImage canvas = new FVIL.Data.CFviImage();
                display.SaveImage( canvas, display.DisplayRect, 1.0 );
                FVIL.File.Function.SaveImageFile(Defs.ResultDir + "/GSearch.Search.png", canvas);
            }
        }
    };
}


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 GSearch
        ''' <summary>
        ''' サーチ実行.
        ''' </summary>
        <FvPluginExecute> _
        Public Sub Search()
            Dim method As System.Reflection.MethodBase = System.Reflection.MethodBase.GetCurrentMethod()
            Console.WriteLine("{0}.{1}", method.DeclaringType.FullName, method.Name)

            ' 1) インスタンスの準備.
            Dim parser As New FVIL.GSearch.CFviGSearch()
            Dim src As New FVIL.Data.CFviImage()
            Dim pattern As New FVIL.Data.CFviPattern()

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

            ' 2-2) パタン画像の読み込み.
            pattern.Load(Defs.TestImageDir & "/clip.pat")

            ' 3) 処理対象画像の有効性検査.
            If FVIL.ErrorCode._SUCCESS <> FVIL.GSearch.CFviGSearch.CheckValidity(src) Then
                Return
            End If
            If FVIL.ErrorCode._SUCCESS <> FVIL.GSearch.CFviGSearch.CheckValidity(pattern) Then
                Return
            End If

            ' 4) 画像処理準備.
            ' --- ライブラリのオープン.
            parser.Open(src.HorzSize, src.VertSize, 1024, -1)

            ' --- 処理対象画像と結果格納用オブジェクトの設定.
            parser.SrcImages(0) = src

            ' 
            ' ※パラメータを既定値から変更した部分は、コメントに [*] を記述しています.
            ' 

            ' 5-1) パラメータ設定.
            Dim param As FVIL.GSearch.CFviGSearchParam = parser.Param
            param.Count = 10
            ' [*] サーチ個数.
            ' [*] 精度: SuperHigh(超高精度)
            param.Precision = FVIL.GSearch.Precision.SuperHigh
            param.Complexity = 1
            ' [ ] 複雑度: 1~9
            param.Score1st = 4000
            ' [*] 途中相関値閾値.
            param.Score2nd = 5000
            ' [*] 最終相関値閾値.
            param.EdgeDetect = False
            ' [ ] 処理範囲接触の許可 (false:非)
            ' 5-E) サーチパタンのオープン.
            parser.OpenPattern(pattern, FVIL.GSearch.FilterMode.NormalPattern)

            ' 6) 画像処理実行.
            Dim timer As New FVIL.CFviTimeCounter()
            timer.Start()
            parser.Execute()
            Dim msec As Double = timer.[Stop]()
            Console.WriteLine("execute. {0} msec, count={1} ", msec.ToString("0.###"), parser.Result.Count)

            ' E) 確認用.
            If True Then
                ' 画像表示の準備.
                Dim display As New FVIL.GDI.CFviDisplay()
                display.Image = src
                display.DisplayRect = src.Window

                ' オーバレイの生成.
                Dim pOverlay0 As New FVIL.GDI.CFviOverlay()
                pOverlay0.Scaling = True
                display.Overlays.Add(pOverlay0)

                Dim pat_mark As FVIL.Data.CFviPoint = pattern.CenterMark
                ' パタン登録時の基準点.
                Dim pat_rect As FVIL.Data.CFviRectangle = pattern.RegistRect
                ' パタン登録時の矩形.
                For i As Integer = 0 To parser.Result.Count - 1
                    Dim data As FVIL.GSearch.CFviGSearchData = parser.Result(i)

                    ' サーチ結果の取得.
                    ' --- 検出位置.
                    Dim mark As FVIL.Data.CFviPoint = data.Position

                    ' --- 検出位置とパタン情報から矩形を復元.
                    Dim rect As New FVIL.Data.CFviRectangle()
                    rect.St = New FVIL.Data.CFviPoint(mark.X - pat_mark.X, mark.Y - pat_mark.Y)
                    rect.Ed = New FVIL.Data.CFviPoint(rect.X + pat_rect.Width, rect.Y + pat_rect.Height)

                    ' --- 相関値.
                    Dim score As System.String = data.Score.ToString()

                    ' 描画用.
                    ' --- 矩形(塗り潰し)
                    Dim _fill As New FVIL.GDI.CFviGdiImage()
                    _fill.SetSize(1, 1, 32)
                    _fill.Position = rect.St
                    _fill.Alpha = &H3f
                    _fill.StretchHorzSize = CInt(Math.Truncate(rect.Width))
                    _fill.StretchVertSize = CInt(Math.Truncate(rect.Height))
                    _fill.SetPixelRGB(0, 0, Color.FromArgb(&Hff, &H0, &H0, &Hff))
                    ' A,R,G,B
                    ' --- 矩形(枠)
                    Dim _rect As New FVIL.GDI.CFviGdiRectangle(rect)
                    _rect.Pen.Color = Color.FromArgb(&H0, &H0, &Hff)

                    ' --- 基準点.
                    Dim _mark As New FVIL.GDI.CFviGdiPoint(mark)
                    _mark.Size = New Size(5, 5)
                    _mark.Pen.Color = Color.FromArgb(&Hff, &H0, &H0)

                    ' --- 相関値.
                    Dim _score As New FVIL.GDI.CFviGdiString()
                    _score.Text = score.ToString()
                    _score.Position = mark
                    _score.Align = FVIL.GDI.TextAlign.Right Or FVIL.GDI.TextAlign.Bottom
                    _score.BkMode = FVIL.GDI.BkMode.Transparent
                    _score.Color = Color.FromArgb(&Hff, &Hff, &H0)

                    ' 追加.
                    pOverlay0.Figures.Add(_fill)
                    pOverlay0.Figures.Add(_rect)
                    pOverlay0.Figures.Add(_mark)
                    pOverlay0.Figures.Add(_score)
                Next

                ' 保存.
                Dim canvas As New FVIL.Data.CFviImage()
                display.SaveImage(canvas, display.DisplayRect, 1.0)
                FVIL.File.[Function].SaveImageFile(Defs.ResultDir & "/GSearch.Search.png", canvas)
            End If
        End Sub
    End Class
End Namespace

Inheritance Hierarchy

System..::..Object
FVIL..::..CFviObject
FVIL..::..CFviImageParser
FVIL.GSearch..::..CFviGSearch

See Also