Skip to content

Commit

Permalink
优化 Ocr和Ocr显示效果
Browse files Browse the repository at this point in the history
  • Loading branch information
MakesYT committed Jan 14, 2025
1 parent 7d4ef66 commit 4fcd15d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
33 changes: 32 additions & 1 deletion KitopiaEx/Ocr/AdaptiveTextBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,22 @@ static AdaptiveTextBox()
// FocusableProperty.OverrideDefaultValue<AdaptiveTextBox>(false);
}

protected override void OnInitialized()
{
base.OnInitialized();

}

protected override void OnLoaded(RoutedEventArgs e)
{


base.OnLoaded(e);
}

public override void ApplyTemplate()
{
base.ApplyTemplate();
Focusable = false;

double width = Math.Abs(BottomRight.X - TopLeft.X);
Expand All @@ -214,7 +227,25 @@ protected override void OnLoaded(RoutedEventArgs e)


Foreground = new SolidColorBrush(Colors.White);
this.FontSize = height /1.5;
double targetSize = height / 1.5;

var availableSize = new Size(double.PositiveInfinity, double.PositiveInfinity);
var textBlock = new TextBlock
{
Text = Text,
FontSize = targetSize

};
// 测量 TextBlock
textBlock.Measure(availableSize);
while (textBlock.DesiredSize.Width>width)
{
targetSize -=1;
textBlock.FontSize = targetSize;
textBlock.Measure(availableSize);
}

this.FontSize = targetSize;
}

public event EventHandler<RoutedEventArgs>? CopyingToClipboard
Expand Down
2 changes: 1 addition & 1 deletion KitopiaEx/Ocr/Ocr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public IEnumerable<OcrResult> OcrImg(ScreenCaptureResult dResult, CancellationTo
EPoint = new Point(rect.Left + rect.Width, rect.Top + rect.Height),
Text = predictText
});
//Console.WriteLine(predictText);
//Console.WriteLine(predictText+" "+rect.Left + " " + rect.Top + " " + rect.Width + " " + rect.Height);
}

return ocrResults;
Expand Down
2 changes: 1 addition & 1 deletion KitopiaEx/Ocr/TextDetector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ private Mat Preprocess(Mat srcMat)
// 将原图复制到新图像的中心位置
Cv2.CopyMakeBorder(dstImg, resizedImgWithPadding, 0, tarH - dstImg.Rows, 0, tarW - dstImg.Cols, BorderTypes.Isolated, new Scalar(255, 255, 255));
return resizedImgWithPadding;
// Cv2.Threshold(dstImg,dstImg,127,255,ThresholdTypes.Binary);
//


}
Expand Down
24 changes: 13 additions & 11 deletions KitopiaEx/Ocr/TextRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,30 +125,32 @@ private Mat Preprocess(Mat srcImg)
{

int w = srcImg.Cols;

int h = srcImg.Rows;
int tarW = (int)(w / 32) * 32;

if (tarW < w)
{
tarW += 32; // Adjust to the next multiple of 32
}

int tarH = (int)(h / 48) * 48;

if (tarH < h)
{
tarH += 48; // Adjust to the next multiple of 32
}
// Convert to grayscale
// var grayImg = new Mat();
// Cv2.CvtColor(srcImg, grayImg, ColorConversionCodes.RGBA2GRAY);

// Create a new image with white padding
// Force height to 48 without scaling the width
int tarH = 48;


// Create a new image with white padding
var paddedImg = new Mat(tarH, w, MatType.CV_8UC3, new Scalar(255, 255, 255));

// Resize the original image to the target height while keeping the width unchanged
var resizedImg = new Mat();
Cv2.Resize(srcImg, resizedImg, new OpenCvSharp.Size(w, tarH));
// Copy the original image to the center of the new image
Cv2.CopyMakeBorder(resizedImg, paddedImg, 0, 0, 0, tarW - srcImg.Cols, BorderTypes.Isolated, new Scalar(255, 255, 255));

var paddedImg = new Mat(tarH, tarW, MatType.CV_8UC3, new Scalar(255, 255, 255));
Cv2.CopyMakeBorder(srcImg, paddedImg, 0, tarH-srcImg.Rows, 0, tarW - srcImg.Cols, BorderTypes.Isolated, new Scalar(255, 255, 255));
Cv2.Resize(paddedImg,paddedImg,new Size(tarW*(48.0/tarH),48));
// Cv2.Threshold(paddedImg,paddedImg,127,255,ThresholdTypes.Binary);
return paddedImg;
}

Expand Down

0 comments on commit 4fcd15d

Please sign in to comment.