简体   繁体   English

如何在 canvas 中的图像上获取绘制矩形的 position

[英]How to get position of drawn rectangle on image in canvas

I'm creating OCR app and my idea is to draw rectangle on a image to create bounding box from where I want to extract text from image so not to take all ocr recognized text.我正在创建 OCR 应用程序,我的想法是在图像上绘制矩形以创建我想从图像中提取文本的边界框,而不是获取所有 ocr 识别的文本。

I have canvas and inside it image and rectangle.我有 canvas 和里面的图像和矩形。 The image is put by default to top=0 and left=0 and then stretched uniform to fit on the canvas so top and left still is the same.图像默认设置为 top=0 和 left=0,然后均匀拉伸以适合 canvas,因此顶部和左侧仍然相同。 How can I get position of rectangle mapped to position on image.如何在图像上获得映射到 position 的矩形的 position。

With my code bellow and on result image, because I draw rectangle on canvas top of the rectangle is 100 and left 300 and.使用下面的代码和结果图像,因为我在 canvas 上绘制矩形,矩形顶部为 100,左侧为 300。 But I want to get position of rectangle on the image.但我想在图像上获得矩形的 position。

<Grid Grid.Row="1">
        <Canvas 
            Background="Aquamarine"
            x:Name="CanvasImagePreview"
            SizeChanged="Canvas_SizeChanged"
            PointerPressed="Canvas_PointerPressed"
            PointerReleased="Canvas_PointerReleased"
            PointerMoved="Canvas_PointerMoved"
            >
            <Image x:Name="ImagePreview"
                   Width="{Binding Path=ActualWidth, ElementName=CanvasImagePreview}" 
                   Height="{Binding Path=ActualHeight, ElementName=CanvasImagePreview}" 
                  Stretch="Uniform"/>
           
            <Rectangle 
                x:Name="BoudingBox_Rect"
                RadiusX="10"
                RadiusY="10"
                StrokeThickness="3"
                Stroke="Red"
                Visibility="Collapsed"
                />
        </Canvas>
    <Grid/>

result image结果图像

By testing, we cannot get the actual size of image when the image is stretched to fill the Canvas panel by using the {Binding} extension to set the Width and Height property.通过测试,我们无法通过使用{Binding}扩展设置WidthHeight属性来拉伸图像以填充Canvas面板时图像的实际大小。 The value of Width and Heigtht property of Image is actual the value of Canvas . ImageWidthHeigtht属性的值实际上是Canvas的值。

You need to calculate the actual size of the image based on the original size of the image which can be get from the corresponding BitmapImage .您需要根据可以从相应的BitmapImage中获取的图像的原始大小来计算图像的实际大小。 Then, get the position of rectangle on the image by calculating.然后,通过计算得到图像上矩形的position。

Please check the following code:请检查以下代码:

var CanvasActualHeight = CanvasImagePreview.ActualHeight;
var CanvasActualWidth = CanvasImagePreview.ActualWidth;
double visualHeight=0.0;
double visualWeight=0.0;

BitmapImage bitmapImage = (BitmapImage)ImagePreview.Source;
if(bitmapImage!=null)
{
    var originalHeight = bitmapImage.PixelHeight;
    var originalWidth = bitmapImage.PixelWidth;
    
    if((CanvasActualHeight / originalHeight) >(CanvasActualWidth / originalWidth))
    {
        visualHeight = originalHeight* CanvasActualWidth / originalWidth;
        visualWeight = CanvasActualWidth;
    }
    else
    {
        visualHeight = CanvasActualHeight;
       visualWeight = originalWidth * CanvasActualHeight / originalHeight;
    }
}

var relativeToCanvasTop = BoudingBox_Rect.Margin.Top;
var relativeToCanvasLeft = BoudingBox_Rect.Margin.Left;

var relativeToImageTop = BoudingBox_Rect.Margin.Top - (CanvasActualHeight - visualHeight)/2;
var relativeToImageLeft = BoudingBox_Rect.Margin.Left-(CanvasActualWidth - visualWeight)/2;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM