Fix EmguCV针对图像宽不是4的倍数的改变bug修复

This commit is contained in:
TianSong
2026-04-21 15:30:07 +08:00
parent 33fef1b6eb
commit 8189e76492
7 changed files with 132 additions and 120 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
using System.Configuration; using System.Configuration;
using XP.Common.Configs; using XP.Common.Configs;
using XP.Common.Dump.Configs; using XP.Common.Dump.Configs;
@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading; using System.Threading;
+1 -1
View File
@@ -1,4 +1,4 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
+1 -1
View File
@@ -1,4 +1,4 @@
using Prism.Mvvm; using Prism.Mvvm;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@@ -1,4 +1,4 @@
using Prism.Ioc; using Prism.Ioc;
using Prism.Modularity; using Prism.Modularity;
using System.Resources; using System.Resources;
using XP.Common.Localization; using XP.Common.Localization;
@@ -28,11 +28,11 @@ namespace XplorePlane.Services
var formatted = new FormatConvertedBitmap(bitmapSource, PixelFormats.Gray8, null, 0); var formatted = new FormatConvertedBitmap(bitmapSource, PixelFormats.Gray8, null, 0);
int width = formatted.PixelWidth; int width = formatted.PixelWidth;
int height = formatted.PixelHeight; int height = formatted.PixelHeight;
int stride = width;
byte[] pixels = new byte[height * stride];
formatted.CopyPixels(pixels, stride, 0);
var image = new Image<Gray, byte>(width, height); var image = new Image<Gray, byte>(width, height);
int stride = image.Bytes.Length / height;
var pixels = new byte[height * stride];
formatted.CopyPixels(pixels, stride, 0);
image.Bytes = pixels; image.Bytes = pixels;
return image; return image;
} }
@@ -40,7 +40,19 @@ namespace XplorePlane.Services
public static Image<Gray, byte> ToEmguCVFromPixels(byte[] pixels, int width, int height) public static Image<Gray, byte> ToEmguCVFromPixels(byte[] pixels, int width, int height)
{ {
var image = new Image<Gray, byte>(width, height); var image = new Image<Gray, byte>(width, height);
int required = image.Bytes.Length;
if (pixels.Length == required)
{
image.Bytes = pixels; image.Bytes = pixels;
}
else
{
int stride = required / height;
var padded = new byte[required];
for (int row = 0; row < height; row++)
Buffer.BlockCopy(pixels, row * width, padded, row * stride, width);
image.Bytes = padded;
}
return image; return image;
} }
@@ -50,8 +62,8 @@ namespace XplorePlane.Services
int width = emguImage.Width; int width = emguImage.Width;
int height = emguImage.Height; int height = emguImage.Height;
int stride = width;
byte[] pixels = emguImage.Bytes; byte[] pixels = emguImage.Bytes;
int stride = pixels.Length / height;
return BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, pixels, stride); return BitmapSource.Create(width, height, 96, 96, PixelFormats.Gray8, null, pixels, stride);
} }