Integrating ShapeFinder with WPF Application

Hi!

Just started working on creating a shapefinder application in a WPF app and I’m stuck at the first step!

I’m attempting to allow the user select a rectangle containing the part of the image that they would like to train to recognise. What’s the correct way to do this using WPF?

Currently I have a DisplayCanvas and RectSelectOverlay in the same grid

<cvb:RectSelectOverlay x:Name="SelectRect" Grid.ColumnSpan="4" SelectedRect="{Binding SelectedRect}" ></cvb:RectSelectOverlay>
<cvb:DisplayCanvas x:Name="CVBDisplayImage" Image="{Binding Image.DeviceImage}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Grid.ColumnSpan="4"/>

Currently this just creates an exception of Object Reference not set to an instance of an Object"

Could anyone point me in the right direction to get this working?

Thanks!

Hi!

The type DisplayCanvas is itself a container for the overlays that operate on image coordinates, therefore the RectSelectOverlay in your example goes inside the DisplayCanvas (which makes the Grid.ColumnSpan obsolete):

<cvb:DisplayCanvas x:Name="CVBDisplayImage" Image="{Binding Image.DeviceImage}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="0" Grid.ColumnSpan="4">
    <cvb:RectSelectOverlay x:Name="SelectRect" SelectedRect="{Binding SelectedRect}" />
</cvb:DisplayCanvas>

Thanks for the quick reply!

Is that all that is required to get the selected rectangle data bound to ‘SelectedRect’? Are there any other steps that are required? I currently don’t have any hardware to capture an image but currently no rectangle is shown on the screen. I have set the stroke/strokethickness values on the RectSelectOverlay. a rough breakdown of the steps would be great if possible.

Thank you!

Best Regards

One thing to keep in mind is that the Display and DisplayCanvas use the information about the currently displayed (:cvb:-)image to scale the overlays correctly (the overlays are displayed in image coordinates, see also How to use Overlay and mouse click with WPF display (c#)). Conversely this means that if there is no image currently set on the Display.Image or DisplayCanvas.Image you won’t see anything because the display/display canvas does not actually know how to scale your overlay.

I’ve put together a very quick and even more dirty demo that shows the concept. Note that it’ll need TwoWay binding on the selected rectangle to work properly. SimpleDemo.zip (466.4 KB)

Thanks for the demo, you have been a great help.

1 Like

Hi Illusive,

Really sorry, I keep on failing on what i presume is basic stuff, is there any other documentation I can look at for using shapefinder other than looking for other forum posts?

internal bool TrainFinder(Stemmer.Cvb.Rect rect)
{
    Image image = CurrDevice.DeviceImage;
    Point2D ptCenter = new Point2D(rect.Left + rect.Width / 2, rect.Top + rect.Height / 2);
    shapefinder.Classifier classifier = shapefinder.ClassifierFactory.Learn(image.Planes[0], ptCenter, rect, 50, shapefinder.GradientType.Roberts);
    if (classifier != null)
    {
        return true;
    }
    else
        return false;
}

This gives an error of

Any guidance on this issue?

Thanks!

Best Regards

Joe

Please don’t be!!! First of all we don’t have anything yet in terms of tutorials or source code samples for the Stemmer.Cvb.ShapeFinder.dll, second the error reporting is not yet that great (what you saw was a Trace statement that fired…) and third you did in fact run into a bug! :slightly_frowning_face: So it should in fact be me who is sorry.

I have built an updated version of the Stemmer.Cvb.ShapeFinder.dll (Version 1.8.0.x, 21.3 KB) for you - put it into your %CVB%\Lib\Net directory (replacing the previous version 1.6.0.x). Make sure to remove the obj and bin folder of your project - otherwise Visual Studio might cling to the older version of the DLL. I cannot promise that this was the last bug - test coverage for ShapeFinder is currently still fairly low for the preview build.

There is in fact also one thing I’d like to point out about your code which might be unintenional. The Learn method’s third parameter (teachWindow) is measured relative to the second parameter (position), which means that usually the rect's Left and Top member are negative values which does not seem to be the case in your code.

One final remark: The Stemmer.Cvb.ShapeFinder.Classifier object is an animal that is rarely used because it is much less versatile than its sibling Stemmer.Cvb.ShapeFinder2.Classifier - the latter is capable of measuring rotation and scale (which the former will not report easily) and does report positions at a far better accuracy. ShapeFinder.Classifier might still have its place where every millisecond counts and pixel-accurate results are sufficient but most applications would benefit from the additional features of ShapeFinder2.Classifier.

Could you also send me the 1.30.0.0 version of the Stemmer.Cvb.dll as well please as I’m getting this error with the updated shapefinder dll. Thanks for the Learn method rectangle correction, I’ll make those changes.

Assembly 'Stemmer.Cvb.ShapeFinder' with identity 'Stemmer.Cvb.ShapeFinder, Version=1.8.0.0, Culture=neutral, PublicKeyToken=fe6400df9f0e0609' uses 'Stemmer.Cvb, Version=1.30.0.0, Culture=neutral, PublicKeyToken=bff841d173155285' which has a higher version than referenced assembly 'Stemmer.Cvb' with identity 'Stemmer.Cvb, Version=1.26.0.0, Culture=neutral, PublicKeyToken=bff841d173155285

You can download them here:

Sorry @joseph.kirk, I built the fix versus the most recently released version of the CVB.Net wrappers (see download link from @parsd). The 1.26.0.0 version of the Stemmer.Cvb.dll dates back to March 2018 - I’d recommend getting the latest build instead (with which my update should work out of the box). I could send the more recent DLL of course, but then you’d also need a more up-to-date Stemmer.Cvb.Wpf.dll, Stemmer.Cvb.Extensions.dll and maybe more - you’re going to be at the point where updating would have been easier fairly quickly.

Will do, thanks guys.

Hi @illusive, I’m finally working on the ShapeFinder2 application again and i’m coming back to the same trouble I was having before, training a classifier. I’m getting an empty exception when the ClassifierFactory tries to learn. See below;

Any clues to what I’m doing wrong now? I’m on v.1.30 of the Stemmer.Cvb.dll and v1.8.0 of the ShapeFinder.dll.

Sorry false alarm, it was just because it couldn’t find any discernible features on the area I was training it on.

Hey @joseph.kirk!

Thanks for the update. I’ve added a hint to the exception message for the 13.2 release so that the exception won’t look that empty. Thanks for pointing it out! :+1: