Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lukegor committed Jul 29, 2024
1 parent cce0be9 commit ff99d23
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 80 deletions.
9 changes: 5 additions & 4 deletions JSharp/ImageProcessingCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Emgu.CV.Structure;
using Emgu.CV.Util;
using Emgu.CV.XImgproc;
using JSharp.Models;
using JSharp.Models.DataModels;
using JSharp.Resources;
using JSharp.Utility;
using System;
Expand Down Expand Up @@ -71,11 +71,12 @@ public static Mat Negate(Mat image)
{
for (int x = 0; x < cols; ++x)
{
byte pixelValue = Marshal.ReadByte(dataPtr, y * cols + x);
int offset = y * cols + x;

byte pixelValue = Marshal.ReadByte(dataPtr, offset);
byte negatedValue = (byte)(255 - pixelValue);

Marshal.WriteByte(dataPtr, y * cols + x, negatedValue);
Marshal.WriteByte(dataPtr, offset, negatedValue);
}
}
return gray.Mat;
Expand Down Expand Up @@ -314,7 +315,7 @@ public static Mat ApplyEdgeDetectionFilter(Mat inputImage, string currentKernel,
}
else if (currentKernel == Kernels.Canny)
{
CvInvoke.Canny(inputImage, result, (double)convolutionInfo.Min, (double)convolutionInfo.Max);
CvInvoke.Canny(inputImage, result, convolutionInfo.Min.Value, convolutionInfo.Max.Value);
}
else if (currentKernel == Kernels.Laplacian)
{
Expand Down
2 changes: 1 addition & 1 deletion JSharp/JSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
</ItemGroup>

<ItemGroup>
<None Update="config.json">
<None Update="shortcuts.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Properties\Settings.settings">
Expand Down
65 changes: 65 additions & 0 deletions JSharp/Models/BusinessLogicModels/Histogram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using Emgu.CV;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models.BusinessLogicModels
{
internal class Histogram : BindableBase
{
private ISeries[] _histogramSeries;
public ISeries[] HistogramSeries
{
get { return _histogramSeries; }
set { SetProperty(ref _histogramSeries, value); }
}

private int _pixelSum;
public int PixelSum
{
get { return _pixelSum; }
set { SetProperty(ref _pixelSum, value); }
}

private ObservableCollection<object> _histogramData;
public ObservableCollection<object> HistogramData
{
get { return _histogramData; }
set { SetProperty(ref _histogramData, value); }
}

public void UpdateHistogram(Mat image)
{
(int[] histogramData, int sum) = ImageProcessingCore.CalculateHistogramValues(image);

PixelSum = sum;

var dataSeries = new ColumnSeries<int>
{
Values = histogramData,
YToolTipLabelFormatter = chartPoint => $"{chartPoint.Coordinate}"
};
HistogramSeries = new ISeries[] { dataSeries };

List<object> tableData = AggregateTableHistogramData(histogramData);
HistogramData = new ObservableCollection<object>(tableData);
}

public static List<object> AggregateTableHistogramData(int[] histogramData)
{
List<object> tableData = new List<object>();
for (int i = 0; i < 256; i++)
{
tableData.Add(new { LightnessLevel = i, PixelCount = histogramData[i] });
}

return tableData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class AnalysisSettings
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class BasicMorphologicalInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class ConvolutionInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class ImageCalculatorInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class ImageInfo
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class OperationData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public record SliderProperties(int MinValue, int MaxValue, int DefaultValue);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using System.Threading.Tasks;

namespace JSharp.Models
namespace JSharp.Models.DataModels
{
public class TwoParamsVMInfo
{
Expand Down
5 changes: 3 additions & 2 deletions JSharp/Resources/Histogram.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion JSharp/ViewModels/AnalyzeParticlesWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JSharp.Models;
using JSharp.Models.DataModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/ConvolverWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Flann;
using JSharp.Models;
using JSharp.Models.DataModels;
using JSharp.Resources;
using JSharp.Utility;
using Prism.Commands;
Expand Down
49 changes: 5 additions & 44 deletions JSharp/ViewModels/HistogramWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,22 @@
using System.Threading.Tasks;
using Prism.Mvvm;
using Emgu.CV.ML;
using JSharp.Models.BusinessLogicModels;

namespace JSharp.ViewModels
{
internal class HistogramWindowViewModel : BindableBase
{
private ISeries[] _histogramSeries;
public ISeries[] HistogramSeries
{
get { return _histogramSeries; }
set { SetProperty(ref _histogramSeries, value); }
}

private int _pixelSum;
public int PixelSum
{
get { return _pixelSum; }
set { SetProperty(ref _pixelSum, value); }
}
public Histogram Histogram { get; private set; }

private ObservableCollection<object> _histogramData;
public ObservableCollection<object> HistogramData
public HistogramWindowViewModel()
{
get { return _histogramData; }
set { SetProperty(ref _histogramData, value); }
Histogram = new Histogram();
}

public void UpdateHistogram(Mat image)
{
(int[] histogramData, int sum) = ImageProcessingCore.CalculateHistogramValues(image);

//assign pixels number to label
PixelSum = sum;

//assign data to histogram
var dataSeries = new ColumnSeries<int>
{
Values = histogramData,
YToolTipLabelFormatter = chartPoint => $"{chartPoint.Coordinate}"
};
HistogramSeries = new ISeries[] { dataSeries };

//assign data to table histogram
List<object> tableData = AggregateTableHistogramData(histogramData);
HistogramData = new ObservableCollection<object>(tableData);
}

public static List<object> AggregateTableHistogramData(int[] histogramData)
{
List<object> tableData = new List<object>();
for (int i = 0; i < 256; i++)
{
tableData.Add(new { LightnessLevel = i, PixelCount = histogramData[i] });
}

return tableData;
Histogram.UpdateHistogram(image);
}
}
}
2 changes: 1 addition & 1 deletion JSharp/ViewModels/ImageCalculatorWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Emgu.CV;
using JSharp.Models;
using JSharp.Models.DataModels;
using JSharp.Utility;
using Prism.Commands;
using Prism.Mvvm;
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/InpaintWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Emgu.CV;
using JSharp.Models;
using JSharp.Models.DataModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.Util;
using JSharp.Models;
using JSharp.Models.DataModels;
using JSharp.Properties;
using JSharp.Resources;
using JSharp.Utility;
Expand Down
18 changes: 10 additions & 8 deletions JSharp/ViewModels/NewImageWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using JSharp;
using JSharp.Models;
using JSharp.Resources;
using JSharp.Models.BusinessLogicModels;
using JSharp.Models.DataModels;
using JSharp.Utility;
using LiveChartsCore.Defaults;
using Microsoft.Win32;
Expand Down Expand Up @@ -253,9 +253,11 @@ public void SaveAs()
CvInvoke.Imwrite(fileName, this.MatImage, new[] { new KeyValuePair<ImwriteFlags, int>(ImwriteFlags.TiffCompression, (int)ImwriteFlags.TiffCompression) });
break;
default:
MessageBox.Show("Invalid file format selected.", Strings.Error, MessageBoxButton.OK, MessageBoxImage.Error);
MessageBox.Show("Invalid file format selected.", Resources.Strings.Error, MessageBoxButton.OK, MessageBoxImage.Error);
return;
}

filePath = fileName;
}
}

Expand Down Expand Up @@ -316,8 +318,8 @@ public void EqualizeHistogram()
{
Mat image = this.MatImage;
List<object> histogramData;
histogramData = this.histogramWindowViewModel?.HistogramData.ToList()
?? HistogramWindowViewModel.AggregateTableHistogramData(ImageProcessingCore.CalculateHistogramValues(image).histogramData);
histogramData = this.histogramWindowViewModel?.Histogram.HistogramData.ToList()
?? Histogram.AggregateTableHistogramData(ImageProcessingCore.CalculateHistogramValues(image).histogramData);
image = ImageProcessingCore.EqualizeHistogram(image, histogramData);
UpdateImageSource(image);
}
Expand Down Expand Up @@ -366,12 +368,12 @@ public void Convolve(string currentKernel, ConvolutionInfo convolutionInfo, IEnu
{
Mat image = this.MatImage;
BorderType borderType = convolutionInfo.BorderPixelsOption;
string[] edgeDetectionCases = { Kernels.SobelEW, Kernels.SobelNS, Kernels.Canny, Kernels.Laplacian };
if (currentKernel == Kernels.BoxBlur)
string[] edgeDetectionCases = { Resources.Kernels.SobelEW, Resources.Kernels.SobelNS, Resources.Kernels.Canny, Resources.Kernels.Laplacian };
if (currentKernel == Resources.Kernels.BoxBlur)
{
image = ImageProcessingCore.ApplyBlur(image, borderType, 3);
}
else if (currentKernel == Kernels.GaussianBlur)
else if (currentKernel == Resources.Kernels.GaussianBlur)
{
image = ImageProcessingCore.ApplyGaussianBlur(image, borderType, sigmaX: 1.5, sigmaY: 1.5, 3);
}
Expand Down
1 change: 0 additions & 1 deletion JSharp/ViewModels/PlotlineGraphWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ private ISeries[] PlotGraph(Point[] points, Mat image)
return seriesList.ToArray();
}

// 24.04 doesn't work entirely correctly, to be fixed
private List<ObservablePoint> BresenhamLine(Mat image, System.Windows.Point p1, System.Windows.Point p2)
{
List<ObservablePoint> linePoints = new List<ObservablePoint>();
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/SettingsWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public SettingsWindowViewModel()
PngCompressionLevel = Settings.Default.pngCompressionLevel;
JpgSaveQuality = Settings.Default.jpqSaveQuality;
SaveFileExtension = Settings.Default.saveFileExtension;
ZoomFactor = Convert.ToUInt32(Settings.Default.ZoomFactor * 10);
ZoomFactor = Convert.ToUInt32(Settings.Default.ZoomFactor * 100);
if (string.IsNullOrEmpty(Settings.Default.LanguageVersion))
Language = "English";
else Language = Settings.Default.LanguageVersion;
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/SummaryWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JSharp.Models;
using JSharp.Models.DataModels;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
Expand Down
2 changes: 1 addition & 1 deletion JSharp/ViewModels/TwoParamsWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using JSharp.Models;
using JSharp.Models.DataModels;
using Prism.Commands;
using Prism.Mvvm;
using System;
Expand Down
6 changes: 3 additions & 3 deletions JSharp/Views/HistogramWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
<TabControl>
<TabItem Header="{x:Static resx:Histogram.Histogram_Word}">
<Grid>
<lvc:CartesianChart x:Name="Histogram" Series="{Binding HistogramSeries}"
<lvc:CartesianChart x:Name="Histogram" Series="{Binding Histogram.HistogramSeries}"
ZoomMode="X"/>
<Label x:Name="pixelSum" Content="{Binding PixelSum}" HorizontalAlignment="Left" Margin="212,-25,0,0" VerticalAlignment="Top"/>
<Label x:Name="pixelSum" Content="{Binding Histogram.PixelSum}" HorizontalAlignment="Left" Margin="212,-25,0,0" VerticalAlignment="Top"/>
</Grid>
</TabItem>
<TabItem Header="{x:Static resx:Histogram.Table_Histogram}">
<Grid Background="#FFE5E5E5">
<ScrollViewer VerticalScrollBarVisibility="Auto">
<DataGrid x:Name="HistogramDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding HistogramData}">
<DataGrid x:Name="HistogramDataGrid" AutoGenerateColumns="False" ItemsSource="{Binding Histogram.HistogramData}">
<DataGrid.Columns>
<DataGridTextColumn Header="{x:Static resx:Strings.PixelIntensity}" Binding="{Binding LightnessLevel}"/>
<DataGridTextColumn Header="{x:Static resx:Histogram.Pixel_Count}" Binding="{Binding PixelCount}"/>
Expand Down
Loading

0 comments on commit ff99d23

Please sign in to comment.