Commit 894ec6ca 刘韬

1

1 个父辈 c9cecc48
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<TargetFrameworkProfile /> <TargetFrameworkProfile />
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
......
...@@ -3,6 +3,7 @@ using ConfigHelper; ...@@ -3,6 +3,7 @@ using ConfigHelper;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
...@@ -123,14 +124,15 @@ namespace Asa ...@@ -123,14 +124,15 @@ namespace Asa
/// <summary> /// <summary>
/// 窗宽 /// 窗宽
/// </summary> /// </summary>
public int WindowWidth { set; get; } = 65535; public int WindowWidth { set; get; } = 38337;//65535;
/// <summary> /// <summary>
/// 窗位 /// 窗位
/// </summary> /// </summary>
public int WindowLevel { set; get; } = 32768; public int WindowLevel { set; get; } = 30812;// 32768;
public Bitmap Get48bImage() public Bitmap Get48bImage()
{ {
//ConvertRawToPng(buffer, 3072, 3072, "d:\\test.tiff");
if (buffer == null) return null; if (buffer == null) return null;
if (ImageWidth <= 0 || ImageHeight <= 0) if (ImageWidth <= 0 || ImageHeight <= 0)
{ {
...@@ -138,6 +140,9 @@ namespace Asa ...@@ -138,6 +140,9 @@ namespace Asa
return null; return null;
} }
//var (WindowWidth, WindowLevel) = CalculateWindowLevel(buffer);
int count = ImageWidth * ImageHeight; int count = ImageWidth * ImageHeight;
byte[] gray = new byte[count * 2]; byte[] gray = new byte[count * 2];
Array.Copy(buffer, 0, gray, 0, gray.Length); Array.Copy(buffer, 0, gray, 0, gray.Length);
...@@ -151,25 +156,25 @@ namespace Asa ...@@ -151,25 +156,25 @@ namespace Asa
short point = BitConverter.ToInt16(gray, idx1); short point = BitConverter.ToInt16(gray, idx1);
//point += 20000; //point += 20000;
if (point < WindowLevel - half) //if (point < WindowLevel - half)
{ //{
buff[idx2++] = 0; // buff[idx2++] = 0;
buff[idx2++] = 0; // buff[idx2++] = 0;
buff[idx2++] = 0; // buff[idx2++] = 0;
buff[idx2++] = 0; // buff[idx2++] = 0;
buff[idx2++] = 0; // buff[idx2++] = 0;
buff[idx2++] = 0; // buff[idx2++] = 0;
} //}
else if (point > WindowLevel + half) //else if (point > WindowLevel + half)
{ //{
buff[idx2++] = 255; // buff[idx2++] = 255;
buff[idx2++] = 255; // buff[idx2++] = 255;
buff[idx2++] = 255; // buff[idx2++] = 255;
buff[idx2++] = 255; // buff[idx2++] = 255;
buff[idx2++] = 255; // buff[idx2++] = 255;
buff[idx2++] = 255; // buff[idx2++] = 255;
} //}
else //else
{ {
byte[] bb = BitConverter.GetBytes(point); byte[] bb = BitConverter.GetBytes(point);
buff[idx2++] = bb[0]; buff[idx2++] = bb[0];
...@@ -194,6 +199,67 @@ namespace Asa ...@@ -194,6 +199,67 @@ namespace Asa
return bmp; return bmp;
} }
public (int windowWidth, int windowCenter) CalculateWindowLevel(byte[] imageData)
{
int minValue = ushort.MaxValue;
int maxValue = ushort.MinValue;
// 计算图像的最大值和最小值
for (int i = 0; i < imageData.Length; i += 2)
{
ushort pixelValue = BitConverter.ToUInt16(imageData, i);
if (pixelValue < minValue)
{
minValue = pixelValue;
}
if (pixelValue > maxValue)
{
maxValue = pixelValue;
}
}
// 计算直方图
int[] histogram = new int[maxValue - minValue + 1];
for (int i = 0; i < imageData.Length; i += 2)
{
ushort pixelValue = BitConverter.ToUInt16(imageData, i);
histogram[pixelValue - minValue]++;
}
// 动态范围压缩
int totalPixels = imageData.Length / 2;
int threshold = totalPixels / 1000; // 设置一个阈值,表示较小的像素值数量的百分比
int accumulatedPixels = 0;
int lowerBound = minValue;
int upperBound = maxValue;
for (int i = 0; i < histogram.Length; i++)
{
accumulatedPixels += histogram[i];
if (accumulatedPixels > threshold)
{
lowerBound = i + minValue;
break;
}
}
accumulatedPixels = 0;
for (int i = histogram.Length - 1; i >= 0; i--)
{
accumulatedPixels += histogram[i];
if (accumulatedPixels > threshold)
{
upperBound = i + minValue;
break;
}
}
// 计算最佳窗宽和窗位
int windowWidth = upperBound - lowerBound;
int windowCenter = (upperBound + lowerBound) / 2;
return (windowWidth, windowCenter);
}
CancellationTokenSource WaitstopToken = new CancellationTokenSource(); CancellationTokenSource WaitstopToken = new CancellationTokenSource();
volatile bool buffgetted = false; volatile bool buffgetted = false;
int getpiccount = 0; int getpiccount = 0;
...@@ -950,7 +1016,7 @@ namespace Asa ...@@ -950,7 +1016,7 @@ namespace Asa
//} //}
Marshal.Copy(imagedata.databuff, buffer, 0, bufflen); Marshal.Copy(imagedata.databuff, buffer, 0, bufflen);
string fileName = Directory.GetCurrentDirectory() + "\\current.raw"; string fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory ,"current.raw");
File.WriteAllBytes(fileName, buffer); File.WriteAllBytes(fileName, buffer);
// 图片信息 // 图片信息
WriteLog(string.Format("SaveImage success:0x{0:X000}\n", nframeId)); WriteLog(string.Format("SaveImage success:0x{0:X000}\n", nframeId));
......
...@@ -26,7 +26,7 @@ namespace CarerayImage_Test ...@@ -26,7 +26,7 @@ namespace CarerayImage_Test
private void Form1_Load(object sender, EventArgs e) private void Form1_Load(object sender, EventArgs e)
{ {
xrayImage = new Asa.XrayImage("123", Asa.XrayImage.DeviceType.HAOBO); xrayImage = new Asa.XrayImage("123", Asa.XrayImage.DeviceType.HAOBO_V2);
} }
private void button1_Click(object sender, EventArgs e) private void button1_Click(object sender, EventArgs e)
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!