Files
UVtools/UVtools.Core/Operations/OperationBlur.cs
T
Tiago Conceição 8e4995a6a5 v3.8.3
- **UVtoolsCmd:**
  - **print-properties:**
    - (Change) `-b`, `--base` option to `-a`, `-all` to indicate all properties and sub-properties, now defaults to only show base properties
    - (Add) `-r`, `--range` option to prints only the matching layer(s) index(es) in a range
    - (Add) `-i`, `--indexes` option to prints only the matching layer(s) index(es)
  - (Add) Command: `set-properties <input-file> <property=value> Set properties in a file or to it layers with new values`
  - (Add) Command: `print-issues <input-file> Detect and print issues in a file`
  - (Add) New option to the `run` command: `-p, --property <property=value>  Set a property with a new value (Compatible with operations only)`
  - (Remove) Command: `print-layers` as it has been moved to `print-properties`, use `-r :` to obtain same result as default on `print-layers`
- **Issues:**
  - (Fix) Issues groups with only one issue was displaying the wrong area value
  - (Fix) Volume incorrectly calculated, resulting in a high value for group of issues
  - (Fix) Incorrect calculation of the bounding rectangle for a group of issues
- **Repair layers:**
  - (Add) Switch to opt between "Re-detect the selected issues before repair" and "Use and repair the previous detected issues" (Default)
  - (Improvement) Do not allow to run the tool if there are no detected issues when the option "Use and repair the previous detected issues" is selected
- (Improvement) Linux: Recompile libcvextern.so on a older system to be able to run on both older and newest system (#603)
- (Upgrade) .NET from 6.0.10 to 6.0.11
2022-11-10 04:09:00 +00:00

205 lines
6.0 KiB
C#

/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using Emgu.CV;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations;
public sealed class OperationBlur : Operation
{
#region Members
private BlurAlgorithm _blurOperation = BlurAlgorithm.Blur;
private uint _size = 1;
#endregion
#region Overrides
public override string IconClass => "fa-solid fa-burn";
public override string Title => "Blur";
public override string Description =>
$"Blur layer images by applying a low pass filter.\n\n" +
"NOTE: Target printer must support AntiAliasing in order to use this function.\n" +
"See https://docs.opencv.org/master/d4/d13/tutorial_py_filtering.html";
public override string ConfirmationText =>
$"blur model with {BlurOperation} from layers {LayerIndexStart} through {LayerIndexEnd}?";
public override string ProgressTitle =>
$"Bluring model with {BlurOperation} from layers {LayerIndexStart} through {LayerIndexEnd}";
public override string ProgressAction => "Blured layers";
public override string? ValidateInternally()
{
var sb = new StringBuilder();
if (BlurOperation is BlurAlgorithm.GaussianBlur or BlurAlgorithm.MedianBlur)
{
if (Size % 2 != 1)
{
sb.AppendLine("Size must be a odd number.");
}
}
if (BlurOperation == BlurAlgorithm.Filter2D)
{
if (Kernel is null)
{
sb.AppendLine("Kernel can not be empty.");
}
}
return sb.ToString();
}
#endregion
#region Enums
public enum BlurAlgorithm
{
[Description("Blur: Normalized box filter")]
Blur,
[Description("Pyramid: Down/up-sampling step of Gaussian pyramid decomposition")]
Pyramid,
[Description("Median Blur: Each pixel becomes the median of its surrounding pixels")]
MedianBlur,
[Description("Gaussian Blur: Each pixel is a sum of fractions of each pixel in its neighborhood")]
GaussianBlur,
[Description("Filter 2D: Applies an arbitrary linear filter to an image")]
Filter2D
}
#endregion
#region Properties
public BlurAlgorithm BlurOperation
{
get => _blurOperation;
set
{
if(!RaiseAndSetIfChanged(ref _blurOperation, value)) return;
RaisePropertyChanged(nameof(IsSizeEnabled));
RaisePropertyChanged(nameof(IsKernelVisible));
}
}
public uint Size
{
get => _size;
set => RaiseAndSetIfChanged(ref _size, value);
}
public bool IsSizeEnabled => BlurOperation != BlurAlgorithm.Pyramid &&
BlurOperation != BlurAlgorithm.Filter2D;
public bool IsKernelVisible => BlurOperation == BlurAlgorithm.Filter2D;
public KernelConfiguration Kernel { get; set; } = new ();
public override string ToString()
{
var result = $"[{_blurOperation}] [Size: {_size}]" + LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Constructor
public OperationBlur() { }
public OperationBlur(FileFormat slicerFile) : base(slicerFile) { }
#endregion
#region Methods
protected override bool ExecuteInternally(OperationProgress progress)
{
Parallel.For(LayerIndexStart, LayerIndexEnd + 1, CoreSettings.GetParallelOptions(progress), layerIndex =>
{
using (var mat = SlicerFile[layerIndex].LayerMat)
{
Execute(mat);
SlicerFile[layerIndex].LayerMat = mat;
}
progress.LockAndIncrement();
});
return !progress.Token.IsCancellationRequested;
}
public override bool Execute(Mat mat, params object[]? arguments)
{
Size size = new((int)Size, (int)Size);
Point anchor = Kernel.Anchor;
if (anchor.IsEmpty) anchor = EmguExtensions.AnchorCenter;
//if (size.IsEmpty) size = new Size(3, 3);
//if (anchor.IsEmpty) anchor = EmguExtensions.AnchorCenter;
var target = GetRoiOrDefault(mat);
using var original = mat.Clone();
switch (BlurOperation)
{
case BlurAlgorithm.Blur:
CvInvoke.Blur(target, target, size, Kernel.Anchor);
break;
case BlurAlgorithm.Pyramid:
CvInvoke.PyrDown(target, target);
CvInvoke.PyrUp(target, target);
break;
case BlurAlgorithm.MedianBlur:
CvInvoke.MedianBlur(target, target, (int)Size);
break;
case BlurAlgorithm.GaussianBlur:
CvInvoke.GaussianBlur(target, target, size, 0);
break;
case BlurAlgorithm.Filter2D:
CvInvoke.Filter2D(target, target, Kernel.GetKernel(), anchor);
break;
default:
throw new ArgumentOutOfRangeException();
}
ApplyMask(original, target);
return true;
}
#endregion
#region Equality
private bool Equals(OperationBlur other)
{
return _blurOperation == other._blurOperation && _size == other._size;
}
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is OperationBlur other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return ((int) _blurOperation * 397) ^ (int) _size;
}
}
#endregion
}