OpenCVの画像フィルターを並べて実行するWPFアプリを作る「高さ指定リサイズ」

コンピュータ

拡大の場合Cubic、縮小の場合Area


ResizeByHeightFilter.cs

using OpenCvSharp;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
using System.Reactive.Linq;
using Cv = OpenCvSharp;

namespace OpenCvFilterMaker2;

public class ResizeByHeightFilter : CvFilterBase
{
    public ReactivePropertySlim<int> TargetHeight { get; set; }
        = new ReactivePropertySlim<int>(1600);

    public ResizeByHeightFilter()
    {
        MenuHeader = "高さ指定リサイズ";
        IsEnabled.Value = true;

        TargetHeight.Subscribe(_ =>
        {
            UpdateName();
        })
        .AddTo(Disposable);

        UpdateName();
    }

    private void UpdateName()
    {
        Name.Value = $"Resize(Height={TargetHeight.Value}";

    }
    protected override Cv.Mat Apply(Cv.Mat input)
    {
        if (TargetHeight.Value <= 0)
            throw new InvalidOperationException("TargetHeight must be > 0");


        int originalHeight = input.Rows;
        int originalWidth = input.Cols;

        double scale = (double)TargetHeight.Value / originalHeight;
        int newWidth = (int)(originalWidth * scale);

        var dst = new Mat();

        var flag = scale < 1.0 ? InterpolationFlags.Area : InterpolationFlags.Cubic;

        Cv2.Resize(
            input,
            dst,
            new Size(newWidth, TargetHeight.Value),
            0,
            0,
            flag
        );

        return dst;
    }
}

ResizeByHeightFilter.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:OpenCvFilterMaker2">

    <!-- 高さ基準リサイズテンプレート -->
    <DataTemplate DataType="{x:Type local:ResizeByHeightFilter}">
        <StackPanel Margin="10">
            <TextBlock Text="ResizeByHeightFilter Settings"
                       FontWeight="Bold"
                       Margin="0,0,0,10"/>

            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Height:"
                            VerticalAlignment="Center"
                            Margin="0,0,5,0"/>
                <TextBox Width="60"
                            Text="{Binding TargetHeight.Value}"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

</ResourceDictionary>a

MainWindow.xaml

<ResourceDictionary Source="/Resources/ResizeByHeightFilter.xaml"/>

追記


コメント