クラスタ情報による エッジ点群のフィルタリング

Namespace: fvalgcli
Assembly: fvalgcli (in fvalgcli.dll) Version: 3.1.0.0 (3.1.0.11)

Syntax

C#
public static int fnFIE_refilter_edge_by_clust(
	F_DEDGE_PTR src_edges,
	int src_edge_num,
	F_EDGE_CLUST_PTR edge_clust,
	int edge_clust_num,
	INT_PTR clust_idx,
	int clust_idx_num,
	ref F_DEDGE_PTR dst_edges,
	ref int dst_edge_num
)
Visual Basic
Public Shared Function fnFIE_refilter_edge_by_clust ( 
	src_edges As F_DEDGE_PTR,
	src_edge_num As Integer,
	edge_clust As F_EDGE_CLUST_PTR,
	edge_clust_num As Integer,
	clust_idx As INT_PTR,
	clust_idx_num As Integer,
	ByRef dst_edges As F_DEDGE_PTR,
	ByRef dst_edge_num As Integer
) As Integer

Parameters

src_edges
Type: fvalgcli..::..F_DEDGE_PTR
入力エッジ点 配列
src_edge_num
Type: System..::..Int32
入力エッジ点の個数 ( 1 ≦ src_edge_num )
edge_clust
Type: fvalgcli..::..F_EDGE_CLUST_PTR
エッジ点クラスタ配列
edge_clust_num
Type: System..::..Int32
エッジ点クラスタ配列 要素数
clust_idx
Type: fvalgcli..::..INT_PTR
処理対象エッジ点クラスタのインデックス配列 ( clust_idx = IntPtr.Zero とした場合は全 クラスタ を処理対象とする )
clust_idx_num
Type: System..::..Int32
処理対象エッジ点クラスタのインデックス配列 要素数
dst_edges
Type: fvalgcli..::..F_DEDGE_PTR%
出力エッジ点 配列 ( 初期値は IntPtr.Zero を指定 )
dst_edge_num
Type: System..::..Int32%
出力エッジ点の個数

Return Value

Type: Int32
以下のエラーコードを返します。

エラーコード:
f_err内容
F_ERR_NONE正常終了
F_ERR_INVALID_PARAMパラメータ異常
F_ERR_NOMEMORYメモリ不足エラー
F_ERR_NO_LICENCEライセンスエラー

Examples

C# Copy imageCopy
using System;
using System.Collections.Generic;
using System.Text;
using fvalgcli;

namespace TC
{
    public partial class FIE
    {
        [FvPluginExecute]
        public void fnFIE_refilter_edge_by_clust()
        {
            string filepath = TestImageDir + "/testdata/key_UC8_M.png";

            int status = (int)f_err.F_ERR_NONE;
            FHANDLE hsrc = FHANDLE.Zero;
            FHANDLE hmag = FHANDLE.Zero;

            F_DEDGE_PTR src_edges = IntPtr.Zero;
            F_DEDGE_PTR dst_edges = IntPtr.Zero;
            int src_edge_num = 0;
            int dst_edge_num = 0;

            F_EDGE_CLUST_PTR clust = IntPtr.Zero;
            int clust_num = 0;

            INT_PTR indexes = IntPtr.Zero;
            int index_num = 0;

            try
            {
                // 入力画像.
                api.fnFIE_load_img_file(filepath, ref hsrc, f_color_img_type.F_COLOR_IMG_TYPE_UC8);

                F_EDGE_SOBEL_PARAMS param;
                param.mag_threshold = 120;
                param.nms_length = 1;

                DPNT_T offset;
                offset.x = 0;
                offset.y = 0;

                var border_mode = f_border_mode.F_BORDER_NONE;
                var feat_mode = f_edge_feature_mode.F_EDGE_FEAT_DIRECT;

                // エッジ検出実行.
                status = api.fnFIE_edge_sobel_subpix(hsrc, IntPtr.Zero, ref param, feat_mode, border_mode, offset, ref src_edges, ref src_edge_num);
                Assert.IsTrue(status == (int)f_err.F_ERR_NONE, "fnFIE_edge_sobel_subpix: エラーが発生しました。({0})", (f_err)status);

                // エッジ点群の連結用パラメータの設定.
                double angle_range = System.Math.PI / 2;
                int distance_range = 5;
                int min_clust_elem = 3;
                int max_clust_elem = -1;

                // エッジ点群の連結.
                status = api.fnFIE_edge_connecting2(
                    src_edges, src_edge_num,
                    angle_range, distance_range, min_clust_elem, max_clust_elem,
                    ref clust, ref clust_num
                    );
                Assert.IsTrue(status == (int)f_err.F_ERR_NONE, "エラーが発生しました。({0})", (f_err)status);

                // インデックス配列.
                index_num = clust_num;
                indexes = INT_PTR.alloc(index_num);
                for (int i = 0; i < index_num; i++)
                    indexes[i] = i;

                // クラスタ情報によるエッジ点群のフィルタリング.
                status = api.fnFIE_refilter_edge_by_clust(
                    src_edges, src_edge_num,
                    clust, clust_num,
                    indexes, index_num,
                    ref dst_edges, ref dst_edge_num);
                Assert.IsTrue(status == (int)f_err.F_ERR_NONE, "エラーが発生しました。({0})", (f_err)status);

                // 確認用.
                {
                    var hdraw_image = fnPRV_draw_edges(hsrc, src_edges, src_edge_num);
                    api.fnFIE_save_png(ResultDir + "/fnFIE_refilter_edge_by_clust_1.png", hdraw_image, -1);
                    hdraw_image.Dispose();
                }

                // 確認用.
                {
                    var hdraw_image = fnPRV_draw_edges(hsrc, dst_edges, dst_edge_num);
                    api.fnFIE_save_png(ResultDir + "/fnFIE_refilter_edge_by_clust_2.png", hdraw_image, -1);
                    hdraw_image.Dispose();
                }

                // 確認用.
                {
                    var hdraw_image = fnPRV_draw_edge_clust(hsrc, src_edges, clust, clust_num);
                    api.fnFIE_save_png(ResultDir + "/fnFIE_refilter_edge_by_clust_3.png", hdraw_image, -1);
                    hdraw_image.Dispose();
                }
            }
            finally
            {
                hsrc.Dispose();
                hmag.Dispose();
                src_edges.Dispose();
                dst_edges.Dispose();
                api.fnFIE_free_edge_clust(clust, clust_num);
                indexes.Dispose();
            }
        }
    }
}

確認用の描画処理: (エッジ点群)
C# Copy imageCopy
using System;
using System.Collections.Generic;
using System.Text;
using fvalgcli;

namespace TC
{
    public partial class FIE
    {
        /// <summary>
        /// エッジ点群の描画.
        /// </summary>
        /// <param name="hsrc">元画像</param>
        /// <param name="edges">エッジ点群</param>
        /// <param name="edge_num">エッジ点数</param>
        /// <returns>
        ///        描画結果の画像。解放必要。
        /// </returns>
        private FHANDLE fnPRV_draw_edges(FHANDLE hsrc, F_DEDGE_PTR edges, int edge_num)
        {
            FHANDLE hdst = FHANDLE.Zero;
            DOUBLE_PTR color = DOUBLE_PTR.Zero;
            DPNT_T_PTR points = DPNT_T_PTR.Zero;

            try
            {
                // 入力画像の情報取得.
                int width = api.fnFIE_img_get_width(hsrc);
                int height = api.fnFIE_img_get_height(hsrc);

                // 出力画像の生成.
                hdst = api.fnFIE_img_root_alloc((int)f_imgtype.F_IMG_UC8, 3, width, height);

                FHANDLE hdst0 = FHANDLE.Zero;
                FHANDLE hdst1 = FHANDLE.Zero;
                FHANDLE hdst2 = FHANDLE.Zero;
                try
                {
                    hdst0 = api.fnFIE_img_child_alloc_single_ch(hdst, 0, 0, 0, width, height);    // CH0
                    hdst1 = api.fnFIE_img_child_alloc_single_ch(hdst, 1, 0, 0, width, height);    // CH1
                    hdst2 = api.fnFIE_img_child_alloc_single_ch(hdst, 2, 0, 0, width, height);    // CH2
                    api.fnFIE_img_copy_ex(hsrc, hdst0, 1, 0, 0);
                    api.fnFIE_img_copy_ex(hsrc, hdst1, 1, 0, 0);
                    api.fnFIE_img_copy_ex(hsrc, hdst2, 1, 0, 0);
                }
                finally
                {
                    hdst0.Dispose();
                    hdst1.Dispose();
                    hdst2.Dispose();
                }

                // 描画色.
                color = DOUBLE_PTR.alloc(3);    // R,G,B
                color[0] = 255;
                color[1] = 0;
                color[2] = 0;

                // 描画.
                for (int i = 0; i < edge_num; i++)
                {
                    DPNT_T point = DPNT_T.init(edges[i].x, edges[i].y);
                    api.fnFIE_draw_point(hdst, color, point);
                }
            }
            finally
            {
                // オブジェクトの開放.
                color.Dispose();
                points.Dispose();
            }

            return hdst;
        }
    }
}

確認用の描画処理: (エッジ点群のクラスタ)
C# Copy imageCopy
using System;
using System.Collections.Generic;
using System.Text;
using fvalgcli;

namespace TC
{
    public partial class FIE
    {
        /// <summary>
        /// 連結された曲線を色を変えて入力画像上に描画する.
        /// </summary>
        /// <param name="hsrc">元画像</param>
        /// <param name="edges">エッジ点群</param>
        /// <param name="clust">クラスタ</param>
        /// <param name="clust_num">クラスタ数</param>
        /// <returns>
        ///        描画結果の画像。解放必要。
        /// </returns>
        private FHANDLE fnPRV_draw_edge_clust(FHANDLE hsrc, F_DEDGE_PTR edges, F_EDGE_CLUST_PTR clust, int clust_num)
        {
            FHANDLE hdst = FHANDLE.Zero;

            // 連結して生成された曲線描画用.
            double[,] colors =
            {
                {   0.0,   0.0, 255.0 },
                {   0.0,   0.0, 128.0 },
                {   0.0, 255.0,   0.0 },
                { 255.0, 255.0,   0.0 },
                { 255.0, 165.0,   0.0 },
                { 255.0,   0.0,   0.0 },
                { 255.0, 192.0, 203.0 },
                { 176.0,  48.0,  96.0 },
                {   0.0, 255.0, 255.0 },
                { 160.0,  32.0, 240.0 },
                { 255.0,   0.0, 255.0 },
                {   0.0, 100.0,   0.0 },
            };

            FHANDLE hdst0 = FHANDLE.Zero;
            FHANDLE hdst1 = FHANDLE.Zero;
            FHANDLE hdst2 = FHANDLE.Zero;
            DOUBLE_PTR color = DOUBLE_PTR.Zero;
            DPNT_T_PTR points = DPNT_T_PTR.Zero;

            try
            {
                // 入力画像の情報取得.
                int width = api.fnFIE_img_get_width(hsrc);
                int height = api.fnFIE_img_get_height(hsrc);

                // 出力画像の生成.
                hdst = api.fnFIE_img_root_alloc((int)f_imgtype.F_IMG_UC8, 3, width, height);

                hdst0 = api.fnFIE_img_child_alloc_single_ch(hdst, 0, 0, 0, width, height);    // CH0
                hdst1 = api.fnFIE_img_child_alloc_single_ch(hdst, 1, 0, 0, width, height);    // CH1
                hdst2 = api.fnFIE_img_child_alloc_single_ch(hdst, 2, 0, 0, width, height);    // CH2
                api.fnFIE_img_copy_ex(hsrc, hdst0, 1, 0, 0);
                api.fnFIE_img_copy_ex(hsrc, hdst1, 1, 0, 0);
                api.fnFIE_img_copy_ex(hsrc, hdst2, 1, 0, 0);

                // 描画色.
                color = DOUBLE_PTR.alloc(3);    // R,G,B

                for (int i = 0; i < clust_num; i++)
                {
                    color[0] = colors[i % 4, 0];
                    color[1] = colors[i % 4, 1];
                    color[2] = colors[i % 4, 2];

                    int num = clust[i].num - 1;
                    for (int j = 0; j < num; j++)
                    {
                        int index0 = clust[i].pindex[j + 0];
                        int index1 = clust[i].pindex[j + 1];
                        DPNT_T st = DPNT_T.init(edges[index0].x, edges[index0].y);
                        DPNT_T ed = DPNT_T.init(edges[index1].x, edges[index1].y);

                        // 描画.
                        api.fnFIE_draw_line_seg(hdst, color, st, ed);
                    }
                }
            }
            finally
            {
                // オブジェクトの開放.
                hdst0.Dispose();
                hdst1.Dispose();
                hdst2.Dispose();
                color.Dispose();
                points.Dispose();
            }

            return hdst;
        }
    }
}


Visual Basic Copy imageCopy
Imports System.Collections.Generic
Imports System.Text
Imports fvalgcli

Public Partial Class FIE
    <FvPluginExecute> _
    Public Sub fnFIE_refilter_edge_by_clust()
        Dim filepath As String = TestImageDir & "/testdata/key_UC8_M.png"

        Dim status As Integer = CInt(f_err.F_ERR_NONE)
        Dim hsrc As FHANDLE = FHANDLE.Zero
        Dim hmag As FHANDLE = FHANDLE.Zero

        Dim src_edges As F_DEDGE_PTR = IntPtr.Zero
        Dim dst_edges As F_DEDGE_PTR = IntPtr.Zero
        Dim src_edge_num As Integer = 0
        Dim dst_edge_num As Integer = 0

        Dim clust As F_EDGE_CLUST_PTR = IntPtr.Zero
        Dim clust_num As Integer = 0

        Dim indexes As INT_PTR = IntPtr.Zero
        Dim index_num As Integer = 0

        Try
            ' 入力画像.
            api.fnFIE_load_img_file(filepath, hsrc, f_color_img_type.F_COLOR_IMG_TYPE_UC8)

            Dim param As F_EDGE_SOBEL_PARAMS
            param.mag_threshold = 120
            param.nms_length = 1

            Dim offset As DPNT_T
            offset.x = 0
            offset.y = 0

            Dim border_mode As f_border_mode = f_border_mode.F_BORDER_NONE
            Dim feat_mode As f_edge_feature_mode = f_edge_feature_mode.F_EDGE_FEAT_DIRECT

            ' エッジ検出実行.
            status = api.fnFIE_edge_sobel_subpix(hsrc, IntPtr.Zero, param, feat_mode, border_mode, offset, _
                src_edges, src_edge_num)
            Assert.IsTrue(status = CInt(f_err.F_ERR_NONE), "fnFIE_edge_sobel_subpix: エラーが発生しました。({0})", CType(status, f_err))

            ' エッジ点群の連結用パラメータの設定.
            Dim angle_range As Double = System.Math.PI / 2
            Dim distance_range As Integer = 5
            Dim min_clust_elem As Integer = 3
            Dim max_clust_elem As Integer = -1

            ' エッジ点群の連結.
            status = api.fnFIE_edge_connecting2(src_edges, src_edge_num, angle_range, distance_range, min_clust_elem, max_clust_elem, _
                clust, clust_num)
            Assert.IsTrue(status = CInt(f_err.F_ERR_NONE), "エラーが発生しました。({0})", CType(status, f_err))

            ' インデックス配列.
            index_num = clust_num
            indexes = INT_PTR.alloc(index_num)
            For i As Integer = 0 To index_num - 1
                indexes(i) = i
            Next

            ' クラスタ情報によるエッジ点群のフィルタリング.
            status = api.fnFIE_refilter_edge_by_clust(src_edges, src_edge_num, clust, clust_num, indexes, index_num, _
                dst_edges, dst_edge_num)
            Assert.IsTrue(status = CInt(f_err.F_ERR_NONE), "エラーが発生しました。({0})", CType(status, f_err))

            ' 確認用.
            If True Then
                Dim hdraw_image As FHANDLE = fnPRV_draw_edges(hsrc, src_edges, src_edge_num)
                api.fnFIE_save_png(ResultDir & "/fnFIE_refilter_edge_by_clust_1.png", hdraw_image, -1)
                hdraw_image.Dispose()
            End If

            ' 確認用.
            If True Then
                Dim hdraw_image As FHANDLE = fnPRV_draw_edges(hsrc, dst_edges, dst_edge_num)
                api.fnFIE_save_png(ResultDir & "/fnFIE_refilter_edge_by_clust_2.png", hdraw_image, -1)
                hdraw_image.Dispose()
            End If

            ' 確認用.
            If True Then
                Dim hdraw_image As FHANDLE = fnPRV_draw_edge_clust(hsrc, src_edges, clust, clust_num)
                api.fnFIE_save_png(ResultDir & "/fnFIE_refilter_edge_by_clust_3.png", hdraw_image, -1)
                hdraw_image.Dispose()
            End If
        Finally
            hsrc.Dispose()
            hmag.Dispose()
            src_edges.Dispose()
            dst_edges.Dispose()
            api.fnFIE_free_edge_clust(clust, clust_num)
            indexes.Dispose()
        End Try
    End Sub
End Class

確認用の描画処理: (エッジ点群)
Visual Basic Copy imageCopy
Imports System.Collections.Generic
Imports System.Text
Imports fvalgcli

Public Partial Class FIE
    ''' <summary>
    ''' エッジ点群の描画.
    ''' </summary>
    ''' <param name="hsrc">元画像</param>
    ''' <param name="edges">エッジ点群</param>
    ''' <param name="edge_num">エッジ点数</param>
    ''' <returns>
    '''        描画結果の画像。解放必要。
    ''' </returns>
    Private Function fnPRV_draw_edges(hsrc As FHANDLE, edges As F_DEDGE_PTR, edge_num As Integer) As FHANDLE
        Dim hdst As FHANDLE = FHANDLE.Zero
        Dim color As DOUBLE_PTR = DOUBLE_PTR.Zero
        Dim points As DPNT_T_PTR = DPNT_T_PTR.Zero

        Try
            ' 入力画像の情報取得.
            Dim width As Integer = api.fnFIE_img_get_width(hsrc)
            Dim height As Integer = api.fnFIE_img_get_height(hsrc)

            ' 出力画像の生成.
            hdst = api.fnFIE_img_root_alloc(CInt(f_imgtype.F_IMG_UC8), 3, width, height)

            Dim hdst0 As FHANDLE = FHANDLE.Zero
            Dim hdst1 As FHANDLE = FHANDLE.Zero
            Dim hdst2 As FHANDLE = FHANDLE.Zero
            Try
                hdst0 = api.fnFIE_img_child_alloc_single_ch(hdst, 0, 0, 0, width, height)
                ' CH0
                hdst1 = api.fnFIE_img_child_alloc_single_ch(hdst, 1, 0, 0, width, height)
                ' CH1
                hdst2 = api.fnFIE_img_child_alloc_single_ch(hdst, 2, 0, 0, width, height)
                ' CH2
                api.fnFIE_img_copy_ex(hsrc, hdst0, 1, 0, 0)
                api.fnFIE_img_copy_ex(hsrc, hdst1, 1, 0, 0)
                api.fnFIE_img_copy_ex(hsrc, hdst2, 1, 0, 0)
            Finally
                hdst0.Dispose()
                hdst1.Dispose()
                hdst2.Dispose()
            End Try

            ' 描画色.
            color = DOUBLE_PTR.alloc(3)
            ' R,G,B
            color(0) = 255
            color(1) = 0
            color(2) = 0

            ' 描画.
            For i As Integer = 0 To edge_num - 1
                Dim point As DPNT_T = DPNT_T.init(edges(i).x, edges(i).y)
                api.fnFIE_draw_point(hdst, color, point)
            Next
        Finally
            ' オブジェクトの開放.
            color.Dispose()
            points.Dispose()
        End Try

        Return hdst
    End Function
End Class

確認用の描画処理: (エッジ点群のクラスタ)
Visual Basic Copy imageCopy
Imports System.Collections.Generic
Imports System.Text
Imports fvalgcli

Public Partial Class FIE
    ''' <summary>
    ''' 連結された曲線を色を変えて入力画像上に描画する.
    ''' </summary>
    ''' <param name="hsrc">元画像</param>
    ''' <param name="edges">エッジ点群</param>
    ''' <param name="clust">クラスタ</param>
    ''' <param name="clust_num">クラスタ数</param>
    ''' <returns>
    '''        描画結果の画像。解放必要。
    ''' </returns>
    Private Function fnPRV_draw_edge_clust(hsrc As FHANDLE, edges As F_DEDGE_PTR, clust As F_EDGE_CLUST_PTR, clust_num As Integer) As FHANDLE
        Dim hdst As FHANDLE = FHANDLE.Zero

        ' 連結して生成された曲線描画用.
        Dim colors As Double(,) = {{0.0, 0.0, 255.0}, {0.0, 0.0, 128.0}, {0.0, 255.0, 0.0}, {255.0, 255.0, 0.0}, {255.0, 165.0, 0.0}, {255.0, 0.0, 0.0}, _
            {255.0, 192.0, 203.0}, {176.0, 48.0, 96.0}, {0.0, 255.0, 255.0}, {160.0, 32.0, 240.0}, {255.0, 0.0, 255.0}, {0.0, 100.0, 0.0}}

        Dim hdst0 As FHANDLE = FHANDLE.Zero
        Dim hdst1 As FHANDLE = FHANDLE.Zero
        Dim hdst2 As FHANDLE = FHANDLE.Zero
        Dim color As DOUBLE_PTR = DOUBLE_PTR.Zero
        Dim points As DPNT_T_PTR = DPNT_T_PTR.Zero

        Try
            ' 入力画像の情報取得.
            Dim width As Integer = api.fnFIE_img_get_width(hsrc)
            Dim height As Integer = api.fnFIE_img_get_height(hsrc)

            ' 出力画像の生成.
            hdst = api.fnFIE_img_root_alloc(CInt(f_imgtype.F_IMG_UC8), 3, width, height)

            hdst0 = api.fnFIE_img_child_alloc_single_ch(hdst, 0, 0, 0, width, height)
            ' CH0
            hdst1 = api.fnFIE_img_child_alloc_single_ch(hdst, 1, 0, 0, width, height)
            ' CH1
            hdst2 = api.fnFIE_img_child_alloc_single_ch(hdst, 2, 0, 0, width, height)
            ' CH2
            api.fnFIE_img_copy_ex(hsrc, hdst0, 1, 0, 0)
            api.fnFIE_img_copy_ex(hsrc, hdst1, 1, 0, 0)
            api.fnFIE_img_copy_ex(hsrc, hdst2, 1, 0, 0)

            ' 描画色.
            color = DOUBLE_PTR.alloc(3)
            ' R,G,B
            For i As Integer = 0 To clust_num - 1
                color(0) = colors(i Mod 4, 0)
                color(1) = colors(i Mod 4, 1)
                color(2) = colors(i Mod 4, 2)

                Dim num As Integer = clust(i).num - 1
                For j As Integer = 0 To num - 1
                    Dim index0 As Integer = clust(i).pindex(j + 0)
                    Dim index1 As Integer = clust(i).pindex(j + 1)
                    Dim st As DPNT_T = DPNT_T.init(edges(index0).x, edges(index0).y)
                    Dim ed As DPNT_T = DPNT_T.init(edges(index1).x, edges(index1).y)

                    ' 描画.
                    api.fnFIE_draw_line_seg(hdst, color, st, ed)
                Next
            Next
        Finally
            ' オブジェクトの開放.
            hdst0.Dispose()
            hdst1.Dispose()
            hdst2.Dispose()
            color.Dispose()
            points.Dispose()
        End Try

        Return hdst
    End Function
End Class

See Also