ウィーナフィルタによる画像復元

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

Syntax

C#
public static int fnFIE_wiener(
	FHANDLE hsrc,
	FHANDLE hdst,
	F_FILTER_KERNEL_T_PTR kernel,
	double gamma
)
Visual Basic
Public Shared Function fnFIE_wiener ( 
	hsrc As FHANDLE,
	hdst As FHANDLE,
	kernel As F_FILTER_KERNEL_T_PTR,
	gamma As Double
) As Integer

Parameters

hsrc
Type: fvalgcli..::..FHANDLE
入力画像 (type: uc8, s16, us16, double)
hdst
Type: fvalgcli..::..FHANDLE
出力画像 (type: uc8, s16, us16, double)
kernel
Type: fvalgcli..::..F_FILTER_KERNEL_T_PTR
カーネル指定構造体のポインタ
gamma
Type: System..::..Double
復元力とノイズ量のバランスを取るための係数(0以上)

Return Value

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

エラーコード:
f_err内容
F_ERR_NONE正常終了
F_ERR_INVALID_PARAMパラメータエラー
F_ERR_INVALID_IMAGE不正な画像が渡された(入力と出力が合わない等)
F_ERR_NOMEMORYメモリ不足により異常終了
F_ERR_NO_LICENCEライセンスエラー、または未初期化エラー

Examples

C# Copy imageCopy
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using fvalgcli;    // FvPluginXXXX attribute requires fvalgcli

namespace TC
{
    public partial class FIE
    {
        // ============================================================
        /// <summary>
        /// ウィーナフィルタによる画像復元.
        /// </summary>
        [FvPluginExecute]
        public void fnFIE_wiener()
        {
            int status = (int)f_err.F_ERR_NONE;

            FHANDLE hsrc = FHANDLE.Zero;                // 入力画像.
            FHANDLE hsrc_degraded = FHANDLE.Zero;        // 劣化画像(人工的に作成)
            FHANDLE hdst = FHANDLE.Zero;                // 出力画像.
            F_FILTER_KERNEL_T_PTR kernel = IntPtr.Zero;    // フィルタカーネル.

            try
            {
                // 画像パラメータ.
                int type = 0;
                int channels = 0;
                int width = 0;
                int height = 0;
                SIZE_T step = 0;

                // 画像ぶれを人工的に発生させるフィルタカーネル.
                var kernel_core = new double[5, 5]
                {
                    {1, 0, 0, 0, 0},
                    {0, 1, 0, 0, 0},
                    {0, 0, 1, 0, 0},
                    {0, 0, 0, 1, 0},
                    {0, 0, 0, 0, 1},
                };
                kernel = F_FILTER_KERNEL_T_PTR.alloc(5, 5);
                for (int r = 0; r < 5; r++)
                    for (int c = 0; c < 5; c++)
                        kernel[r, c] = kernel_core[r, c];
                kernel.anchor_x = 2;    // フィルタ中心位置.
                kernel.anchor_y = 2;    // フィルタ中心位置.
                kernel.denom = 5;        // フィルタ分母.

                // 入力画像読み込み.
                api.fnFIE_load_img_file(TestImageDir + "/testdata/gray_04.bmp", ref hsrc, f_color_img_type.F_COLOR_IMG_TYPE_UC8);

                // 入力画像のパラメータ取得.
                api.fnFIE_img_get_params(hsrc, ref channels, ref type, ref step, ref width, ref height);

                // 劣化画像と出力画像のメモリ確保.
                hsrc_degraded = api.fnFIE_img_root_alloc(type, channels, width, height);
                hdst = api.fnFIE_img_root_alloc(type, channels, width, height);

                // 劣化画像の作成 (ぶれ+ノイズ)
                api.fnFIE_kernel_filter(hsrc, hsrc_degraded, kernel, f_border_mode.F_BORDER_REPEAT, 0);
                api.fnFIE_add_noise_gauss(hsrc_degraded, hsrc_degraded, 0.0, 5.0, 0);

                // ウィーナフィルタによる画像復元.
                status = api.fnFIE_wiener(hsrc_degraded, hdst, kernel, 0.01);

                // 結果画像保存.
                api.fnFIE_save_png(ResultDir + "/fnFIE_wiener.degraded.png", hsrc_degraded, -1);
                api.fnFIE_save_png(ResultDir + "/fnFIE_wiener.restored.png", hdst, -1);
            }
            finally
            {
                // 画像の解放.
                hsrc.Dispose();
                hsrc_degraded.Dispose();
                hdst.Dispose();
                kernel.Dispose();
            }
        }
    }
}


Visual Basic Copy imageCopy
Imports System.Collections.Generic
Imports System.Text
Imports fvalgcli
' FvPluginXXXX attribute requires fvalgcli
Partial Public Class FIE
    ' ============================================================
    ''' <summary>
    ''' ウィーナフィルタによる画像復元.
    ''' </summary>
    <FvPluginExecute> _
    Public Sub fnFIE_wiener()
        Dim status As Integer = CInt(f_err.F_ERR_NONE)

        ' 入力画像.
        Dim hsrc As FHANDLE = FHANDLE.Zero
        ' 劣化画像(人工的に作成)
        Dim hsrc_degraded As FHANDLE = FHANDLE.Zero
        ' 出力画像.
        Dim hdst As FHANDLE = FHANDLE.Zero
        ' フィルタカーネル.
        Dim kernel As F_FILTER_KERNEL_T_PTR = IntPtr.Zero

        Try
            ' 画像パラメータ.
            Dim type As Integer = 0
            Dim channels As Integer = 0
            Dim width As Integer = 0
            Dim height As Integer = 0
            Dim [step] As SIZE_T = 0

            ' 画像ぶれを人工的に発生させるフィルタカーネル.
            Dim kernel_core As Double(,) = New Double(4, 4) { _
                {1, 0, 0, 0, 0}, _
                {0, 1, 0, 0, 0}, _
                {0, 0, 1, 0, 0}, _
                {0, 0, 0, 1, 0}, _
                {0, 0, 0, 0, 1} _
            }
            kernel = F_FILTER_KERNEL_T_PTR.alloc(5, 5)
            For r As Integer = 0 To 4
                For c As Integer = 0 To 4
                    kernel(r, c) = kernel_core(r, c)
                Next
            Next
            kernel.anchor_x = 2
            ' フィルタ中心位置.
            kernel.anchor_y = 2
            ' フィルタ中心位置.
            kernel.denom = 5
            ' フィルタ分母.
            ' 入力画像読み込み.
            api.fnFIE_load_img_file(TestImageDir & "/testdata/gray_04.bmp", hsrc, f_color_img_type.F_COLOR_IMG_TYPE_UC8)

            ' 入力画像のパラメータ取得.
            api.fnFIE_img_get_params(hsrc, channels, type, [step], width, height)

            ' 劣化画像と出力画像のメモリ確保.
            hsrc_degraded = api.fnFIE_img_root_alloc(type, channels, width, height)
            hdst = api.fnFIE_img_root_alloc(type, channels, width, height)

            ' 劣化画像の作成 (ぶれ+ノイズ)
            api.fnFIE_kernel_filter(hsrc, hsrc_degraded, kernel, f_border_mode.F_BORDER_REPEAT, 0)
            api.fnFIE_add_noise_gauss(hsrc_degraded, hsrc_degraded, 0.0, 5.0, 0)

            ' ウィーナフィルタによる画像復元.
            status = api.fnFIE_wiener(hsrc_degraded, hdst, kernel, 0.01)

            ' 結果画像保存.
            api.fnFIE_save_png(ResultDir & "/fnFIE_wiener.degraded.png", hsrc_degraded, -1)
            api.fnFIE_save_png(ResultDir & "/fnFIE_wiener.restored.png", hdst, -1)
        Finally
            ' 画像の解放.
            hsrc.Dispose()
            hsrc_degraded.Dispose()
            hdst.Dispose()
            kernel.Dispose()
        End Try
    End Sub
End Class

See Also