Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup #1652

Open
NBarile opened this issue Feb 13, 2024 · 1 comment
Open

Setup #1652

NBarile opened this issue Feb 13, 2024 · 1 comment

Comments

@NBarile
Copy link

NBarile commented Feb 13, 2024

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
Copy link
Contributor

lwqwag commented Mar 20, 2024

Mat can't directly bind to wpf ImageSource

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants