OpenCVSharp動画再生 の変更点


#author("2018-12-10T14:43:44+09:00","default:fuzuki","fuzuki")
* OpenCVSharp3で動画再生サンプル [#t54244c6]
とりあえず最低限の実装。

 <Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="707,10,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
        <Image x:Name="imageResult" HorizontalAlignment="Left" Height="399" Margin="10,10,0,0" VerticalAlignment="Top" Width="570"/>
        <TextBlock x:Name="Videopath" HorizontalAlignment="Left" Margin="602,51,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Height="358" Width="180"/>
    </Grid>
 </Window>

 using OpenCvSharp.Extensions;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 
 namespace WpfApp3
 {
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        private System.ComponentModel.BackgroundWorker worker;
 
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private async void Button_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new Microsoft.Win32.OpenFileDialog();
            if (dialog.ShowDialog() == true)
            {
                Videopath.Text = dialog.FileName;
                if (dialog.FileName.EndsWith("jpg"))
                {
                    var src = OpenCvSharp.Cv2.ImRead(dialog.FileName);
                    imageResult.Source = src.ToWriteableBitmap();
                    src.Dispose();
                }else if (dialog.FileName.EndsWith("mp4"))
                {
                    await CaptureAsync(dialog.FileName);
                }
            }
        }
        private WriteableBitmap wb;
        private async Task CaptureAsync(string file)
        {
            using(var mov = new OpenCvSharp.VideoCapture(file))
            {
                wb = new WriteableBitmap(mov.FrameWidth, mov.FrameHeight, 96, 96, PixelFormats.Bgr24, null);
                int interval = (int)(1000 / mov.Fps);
                OpenCvSharp.Mat image;
                while ((image = mov.RetrieveMat()) != null && !image.Empty())
                {
                    WriteableBitmapConverter.ToWriteableBitmap(image, wb);
                    imageResult.Source = wb;
                    await Task.Delay(interval);
                    image.Dispose();
                }
            }
        }
    }
 }

*** 参考 [#m87a6b2e]
- https://takachan.hatenablog.com/entry/2018/05/30/003707
- http://sourcechord.hatenablog.com/entry/2014/10/05/022144
- http://schima.hatenablog.com/entry/2015/08/17/203901