Repository navigation
Ревью на свертку изображений #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vit0GG
wants to merge
5
commits into
main
Choose a base branch
from
code-review
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using System; | ||
| using System.IO; | ||
|
|
||
| using ImageConvolution; | ||
|
|
||
| using Xunit; | ||
|
|
||
| namespace ImageConvolution.Tests; | ||
|
|
||
| public class ConvolutionTests | ||
| { | ||
| [Fact] | ||
| public void Test_ImageIO_SaveAndLoad() | ||
| { | ||
| string testPath = "test_image.png"; | ||
| double[,] originalData = new double[2, 2] { { 0, 255 }, { 128, 64 } }; | ||
|
|
||
| try | ||
| { | ||
| ImageIO.SaveImage(originalData, testPath); | ||
| Assert.True(File.Exists(testPath)); | ||
|
|
||
| double[,] loadedData = ImageIO.LoadAsGrayscale(testPath); | ||
|
|
||
| Assert.Equal(2, loadedData.GetLength(0)); | ||
| Assert.Equal(2, loadedData.GetLength(1)); | ||
| } | ||
| finally | ||
| { | ||
| if (File.Exists(testPath)) File.Delete(testPath); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Test_Convolve_ExtendStrategy() | ||
| { | ||
| double[,] image = { { 10, 10, 10 }, { 10, 10, 10 }, { 10, 10, 10 } }; | ||
| double[,] result = ConvolutionProcessor.Convolve(image, Kernels.BlurBox, EdgeStrategy.Extend); | ||
| Assert.Equal(10, Math.Round(result[1, 1])); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Test_Convolve_ZeroPaddingStrategy() | ||
| { | ||
| double[,] image = { { 10, 10, 10 }, { 10, 10, 10 }, { 10, 10, 10 } }; | ||
| double[,] result = ConvolutionProcessor.Convolve(image, Kernels.BlurBox, EdgeStrategy.ZeroPadding); | ||
| Assert.True(result[0, 0] < 10); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Test_ConvolveParallel() | ||
| { | ||
| double[,] image = { { 100, 100, 100 }, { 100, 100, 100 }, { 100, 100, 100 } }; | ||
| double[,] result = ParallelConvolutionProcessor.ConvolveParallel(image, Kernels.BlurBox, EdgeStrategy.Extend); | ||
| Assert.Equal(100, Math.Round(result[1, 1])); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Test_KernelsExist() | ||
| { | ||
| Assert.NotNull(Kernels.BlurBox); | ||
| Assert.NotNull(Kernels.Sharpen); | ||
| Assert.NotNull(Kernels.Laplacian); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Test_ConvolveParallel_ZeroPadding() | ||
| { | ||
| double[,] image = { | ||
| { 10, 10, 10 }, | ||
| { 10, 10, 10 }, | ||
| { 10, 10, 10 } | ||
| }; | ||
|
|
||
| double[,] result = ParallelConvolutionProcessor.ConvolveParallel( | ||
| image, | ||
| Kernels.BlurBox, | ||
| EdgeStrategy.ZeroPadding); | ||
|
|
||
| Assert.True(result[0, 0] < 10); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.2" /> | ||
| <PackageReference Include="coverlet.msbuild" Version="8.0.1"> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" /> | ||
| <PackageReference Include="xunit" Version="2.9.2" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Using Include="Xunit" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\ImageConvolution\ImageConvolution.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net9.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| using System; | ||
| using System.Data; | ||
| using System.Diagnostics; | ||
| using System.Diagnostics.CodeAnalysis; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
|
|
||
| using SixLabors.ImageSharp; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
|
|
||
| namespace ImageConvolution | ||
| { | ||
| public enum EdgeStrategy | ||
| { | ||
| Extend, | ||
| ZeroPadding | ||
| } | ||
|
|
||
| [ExcludeFromCodeCoverage] | ||
| class Program | ||
| { | ||
| static void Main(string[] args) | ||
| { | ||
| Console.WriteLine("=== Программа свёртки изображений ==="); | ||
| Console.Write("Введите путь к изображению (перетащите файл в консоль): "); | ||
|
|
||
| string? inputPath = Console.ReadLine()?.Trim('\"', ' ', '\''); | ||
|
|
||
| if (string.IsNullOrEmpty(inputPath) || !File.Exists(inputPath)) | ||
| { | ||
| Console.WriteLine("Ошибка: Файл не найден! Проверьте путь и попробуйте снова."); | ||
| return; | ||
| } | ||
|
|
||
| string directory = Path.GetDirectoryName(inputPath) ?? ""; | ||
| string fileName = Path.GetFileNameWithoutExtension(inputPath); | ||
| string extension = Path.GetExtension(inputPath); | ||
| string outputPath = Path.Combine(directory, fileName + "_filtered" + extension); | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine("\n1. Загрузка изображения..."); | ||
| double[,] image = ImageIO.LoadAsGrayscale(inputPath); | ||
|
|
||
| Console.WriteLine("2. Применение свёртки (Размытие / Blur)..."); | ||
| Stopwatch sw = Stopwatch.StartNew(); | ||
|
|
||
| double[,] result = ConvolutionProcessor.Convolve( | ||
| image, | ||
| Kernels.BlurBox, | ||
| EdgeStrategy.Extend); | ||
|
|
||
| sw.Stop(); | ||
| Console.WriteLine("2.1. Применение параллельной свёртки (Размытие / Blur)..."); | ||
| Stopwatch sw1 = Stopwatch.StartNew(); | ||
|
|
||
| double[,] result2 = ParallelConvolutionProcessor.ConvolveParallel( | ||
| image, | ||
| Kernels.BlurBox, | ||
| EdgeStrategy.Extend); | ||
|
|
||
| sw1.Stop(); | ||
|
|
||
| Console.WriteLine($"Свёртка завершена за {sw.ElapsedMilliseconds} мс"); | ||
|
|
||
| Console.WriteLine($"Параллельная свёртка завершена за {sw1.ElapsedMilliseconds} мс"); | ||
|
|
||
| Console.WriteLine("3. Сохранение результата..."); | ||
| ImageIO.SaveImage(result, outputPath); | ||
|
|
||
| Console.WriteLine($"\nГотово! Изображение сохранено здесь:\n{outputPath}"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Console.WriteLine($"Произошла ошибка: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public class ImageIO | ||
| { | ||
| public static double[,] LoadAsGrayscale(string path) | ||
| { | ||
| using Image<Rgba32> image = Image.Load<Rgba32>(path); | ||
| int width = image.Width; | ||
| int height = image.Height; | ||
| double[,] result = new double[height, width]; | ||
|
|
||
| for (int y = 0; y < height; y++) | ||
| { | ||
| for (int x = 0; x < width; x++) | ||
| { | ||
| Rgba32 pixel = image[x, y]; | ||
| double gray = 0.299 * pixel.R + 0.587 * pixel.G + 0.114 * pixel.B; | ||
| result[y, x] = gray; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| public static void SaveImage(double[,] data, string path) | ||
| { | ||
| int height = data.GetLength(0); | ||
| int width = data.GetLength(1); | ||
|
|
||
| using Image<Rgba32> image = new Image<Rgba32>(width, height); | ||
| for (int y = 0; y < height; y++) | ||
| { | ||
| for (int x = 0; x < width; x++) | ||
| { | ||
| double val = data[y, x]; | ||
| byte gray = (byte)Math.Clamp(val, 0, 255); | ||
| image[x, y] = new Rgba32(gray, gray, gray); | ||
| } | ||
| } | ||
| image.Save(path); | ||
| } | ||
| } | ||
|
|
||
| public class Kernels | ||
| { | ||
| public static double[,] Sharpen = new double[,] | ||
| { | ||
| { 0, -1, 0}, | ||
| { -1, 5, -1}, | ||
| { 0, -1, 0} | ||
| }; | ||
| public static double[,] Laplacian = new double[,] | ||
| { | ||
| {-1, -1, -1}, | ||
| {-1, 8, -1}, | ||
| {-1, -1, -1} | ||
| }; | ||
| public static double[,] BlurBox = new double[,] | ||
| { | ||
| {1.0/9.0, 1.0/9.0, 1.0/9.0}, | ||
| {1.0/9.0, 1.0/9.0, 1.0/9.0}, | ||
| {1.0/9.0, 1.0/9.0, 1.0/9.0} | ||
| }; | ||
| } | ||
|
|
||
| public class ConvolutionProcessor | ||
| { | ||
| internal static double CalculatePixelValue(double[,] image, double[,] kernel, int x, int y, EdgeStrategy strategy) | ||
| { | ||
| double sum = 0.0; | ||
| int imgheight = image.GetLength(0); | ||
| int imgwidth = image.GetLength(1); | ||
| int kheight = kernel.GetLength(0); | ||
| int kwidth = kernel.GetLength(1); | ||
| int offsetY = kheight / 2; | ||
| int offsetX = kwidth / 2; | ||
|
|
||
| for (int ky = 0; ky < kheight; ky++) | ||
| { | ||
| for (int kx = 0; kx < kwidth; kx++) | ||
| { | ||
| int pixelY = y + ky - offsetY; | ||
| int pixelX = x + kx - offsetX; | ||
| double pixelValue = 0.0; | ||
|
|
||
| if (strategy == EdgeStrategy.Extend) | ||
| { | ||
| pixelY = Math.Clamp(pixelY, 0, imgheight - 1); | ||
| pixelX = Math.Clamp(pixelX, 0, imgwidth - 1); | ||
| pixelValue = image[pixelY, pixelX]; | ||
| } | ||
| else if (strategy == EdgeStrategy.ZeroPadding) | ||
| { | ||
| if (pixelY >= 0 && pixelY < imgheight && pixelX >= 0 && pixelX < imgwidth) | ||
| { | ||
| pixelValue = image[pixelY, pixelX]; | ||
| } | ||
| } | ||
| sum += pixelValue * kernel[ky, kx]; | ||
| } | ||
| } | ||
| return sum; | ||
| } | ||
|
|
||
| public static double[,] Convolve(double[,] image, double[,] kernel, EdgeStrategy strategy = EdgeStrategy.Extend) | ||
| { | ||
| int imgheight = image.GetLength(0); | ||
| int imgwidth = image.GetLength(1); | ||
| double[,] result = new double[imgheight, imgwidth]; | ||
| for (int y = 0; y < imgheight; y++) | ||
| { | ||
| for (int x = 0; x < imgwidth; x++) | ||
| { | ||
| result[y, x] = CalculatePixelValue(image, kernel, x, y, strategy); | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| public class ParallelConvolutionProcessor | ||
| { | ||
|
|
||
| public static double[,] ConvolveParallel(double[,] image, double[,] kernal, EdgeStrategy strategy = EdgeStrategy.Extend) | ||
| { | ||
| int imgheight = image.GetLength(0); | ||
| int imgwidth = image.GetLength(1); | ||
| double[,] result = new double[imgheight, imgwidth]; | ||
|
|
||
| Parallel.For(0, imgheight, y => | ||
| { | ||
| for (int x = 0; x < imgwidth; x++) | ||
| { | ||
| result[y, x] = ConvolutionProcessor.CalculatePixelValue(image, kernal, x, y, strategy); | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Можно ли переиспользовать тело цикла?