shimat / opencvsharp

OpenCV wrapper for .NET
Apache License 2.0
5.22k stars 1.13k forks source link

Setup #1652

Open NBarile opened 4 months ago

NBarile commented 4 months ago

Summary of your issue

Hi ! I try to devellop an app with OpenCvsharp, in wpf with community toolkit mvvm.

When I try to launch a basic windows with only an image for stream acquisition, the windows doesn't display...

I think it's when I set my VideoCapture, because when I remove it, the windows display normaly.

So, if someone can help me, or show me an exemple code who work ...

Thanks !

Environment

I Use .net 8.0 Visual Studio community 2022 image

Example code:

main_view.xaml

<Window x:Class="Stock_IA.Views.Main_View"
        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:Stock_IA.Views"
        mc:Ignorable="d"
        Title="Main_View">
    <Grid Height="700" Width="1000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

        <Image Source="{Binding Camera}" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2"/>
    </Grid>
</Window>

main_view.xaml.cs

public partial class Main_View
{
    public Main_View()
    {
        DataContext = new Main_ViewModel();
        InitializeComponent();

    }
}

Main_ViewModel.cs

public partial class Main_ViewModel : ObservableObject
{
    private OpenCv_Model _model;

    [ObservableProperty]
    private Mat _camera;

    public Main_ViewModel()
    {
        _model = new(0);
        GetCamera();
    }

    void GetCamera()
    {
        while (true)
        {
            Camera = _model.GetFrame();
            Cv2.WaitKey(50);
            Camera.Dispose();
        }
    }
}

OpenCv_Model.cs

internal class OpenCv_Model
{
    VideoCapture capture;

    public OpenCv_Model(int nb_device)
    {
        capture = new VideoCapture(nb_device);
    }

    public Mat GetFrame()
    {
        Mat image = new Mat();
        capture.Read(image);
        return image;
    }
}

Output:

Nothing .... Just want a basic windows with my webcam ....

Thanks again !!

lwqwag commented 3 months ago

Mat can't directly bind to wpf ImageSource