C# 图像处理中的很多像素实例
我有一个很大的内存问题似乎是由许多XElement实例引起的 我有一个TabControl,它在每个选项卡中显示一个带有单元格图片的图像(想象一个黑色图片,上面有大约150个圆圈,是单元格) 我写了一个算法来检测这些细胞,并得到它们的直径等参数 问题来了:在空闲状态下,程序使用大约200mb的内存。当我开始检测时,它会上升到3.5GB。我使用VisualStudio的内置内存分析器和第三方内存分析器来查找问题。XElement实例在3500时开始处于空闲状态,在检测之后,它们处于45000个实例,我不知道为什么会有这么多 在检测开始时,我为图像的每个块启动算法(图片约为25000x25000像素,因此我必须将它们划分为块):C# 图像处理中的很多像素实例,c#,memory-leaks,aforge,C#,Memory Leaks,Aforge,我有一个很大的内存问题似乎是由许多XElement实例引起的 我有一个TabControl,它在每个选项卡中显示一个带有单元格图片的图像(想象一个黑色图片,上面有大约150个圆圈,是单元格) 我写了一个算法来检测这些细胞,并得到它们的直径等参数 问题来了:在空闲状态下,程序使用大约200mb的内存。当我开始检测时,它会上升到3.5GB。我使用VisualStudio的内置内存分析器和第三方内存分析器来查找问题。XElement实例在3500时开始处于空闲状态,在检测之后,它们处于45000个实
while(startY+1500
算法方法:
ObservableCollection<ImageObjectModel> ioms = selectedWorkspaceModel.ImageObjectModels;
result.Add(CellDetection.CellCategorizer(getBitmapSource(startX,startY,sizeX,sizeY), ImageDataAccessor.ImageDataSource.PixelSpacing.SpacingX, parameterSet)); //CellCategorizer actually does the detection of the cells. The getBitmapSource is an in-built method from the zeiss.micro library
result.Last().BlockDescriptor = new BlockDescriptor(startX, startY, 1500, 1500); //position of block in image
if (result[count] != null)
{
int cellResultLength = result[count].CellResults.Count;
for (int i = 0; i < cellResultLength; i++) //All of the stuff in the loop is just to get the calculated cells in its own viewmodel to show a gallary of cells afterwars. This is not the problem
{
int startXCell = result[count].CellResults[i].CellsRectangle.X;
int startYCell = result[count].CellResults[i].CellsRectangle.Y;
int blockDX = result[count].BlockDescriptor.X;
int blockDY = result[count].BlockDescriptor.Y;
int rectangleWidth = result[count].CellResults[i].CellsRectangle.Width;
int rectangleHeight = result[count].CellResults[i].CellsRectangle.Height;
double cellDiameter = result[count].CellResults[i].CellDiameter;
double feretMin = result[count].CellResults[i].FeretMinimum;
double feretMax = result[count].CellResults[i].FeretMaximum;
var state = result[count].CellResults[i].State;
ImageBounds b = new ImageBounds(ImageBound);
b.StartX = (startXCell + blockDX) - 10;
b.StartY = (startYCell + blockDY) - 10;
b.SizeX = rectangleWidth + 20;
b.SizeY = rectangleHeight + 20;
ImageObjectModel imageObjectModel = new ImageObjectModel(new ImageDataAccessor(selectedWorkspaceModel.ImageDataSource,b));
if (state == CellResult.CellState.Match)
{
imageObjectModel.State = ImageObjectModel.ClassificationState.Match;
}
else
{
imageObjectModel.State = ImageObjectModel.ClassificationState.Mismatch;
}
imageObjectModel.StartX = (startXCell + blockDX);
imageObjectModel.StartY = (startYCell + blockDY);
imageObjectModel.CellDiameter = cellDiameter;
imageObjectModel.FeretMax = feretMax;
imageObjectModel.FeretMin = feretMin;
ioms.Add(imageObjectModel); //Adds the iom to the list so that I can draw a rectangle around the cell after the detection
}
}
observeCollection ioms=selectedWorkspaceModel.ImageObjectModels;
结果.Add(CellDetection.CellCategorizer(getBitmapSource(startX、startY、sizeX、sizeY)、ImageDataAccessor.ImageDataSource.PixelSpacingx、parameterSet))//CellClassifier实际上是检测细胞的。getBitmapSource是来自蔡司微库的内置方法
result.Last().BlockDescriptor=新的块描述符(startX、startY、1500、1500)//块在图像中的位置
如果(结果[计数]!=null)
{
int-cellResultLength=result[count].CellResults.count;
for(int i=0;i
最后是使用AForge库的细胞分类方法:
if (_inputSource == null) throw new Exception();
var inputBitmap = BitmapFromSource(_inputSource);
if (!validatePixelFormat(inputBitmap))
{
inputBitmap = transformBitmap(inputBitmap);
}
BitmapData bitmapData = inputBitmap.LockBits(
new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height),
ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
filter2.CenterColor = new RGB(255, 255, 255);
filter2.Radius = 430;
filter2.ApplyInPlace(bitmapData);
filter.CoupledSizeFiltering = true;
filter.MinWidth = 10;
filter.MinHeight = 10;
filter.MaxHeight = Int32.MaxValue;
filter.MaxWidth = Int32.MaxValue;
filter.ApplyInPlace(bitmapData);
bc.FilterBlobs = true;
bc.MinWidth = 10;
bc.MinHeight = 10;
bc.MaxHeight = Int32.MaxValue;
bc.MaxWidth = Int32.MaxValue;
bc.CoupledSizeFiltering = false;
bc.ObjectsOrder = ObjectsOrder.Size;
bc.ProcessImage(bitmapData);
Blob[] blobs = bc.GetObjectsInformation();
BlockResult resultOfBlock = new BlockResult();
inputBitmap.UnlockBits(bitmapData);
foreach (var rec in blobs)
{
var cuttedImage = inputBitmap.Clone(rec.Rectangle, inputBitmap.PixelFormat);//Need to get every cell seperated to perform further analysis
double cellDiameter = Math.Sqrt(((rec.Area*(pixelSize*pixelSize)) / Math.PI)) * 2;
cellDiameter *= 1000;
var feretMax = calculateFeretMax( pixelSize,cuttedImage); //Similar things like here happen in this method so I don't want to spam you with extra code
var feretMin = calculateFeretMin( pixelSize,cuttedImage);//Similar things like here happen in this method so I don't want to spam you with extra code
CellResult result = new CellResult(Math.Round(cellDiameter,2),Math.Round( feretMax,2),Math.Round( feretMin,2));
int countAccepted = 0;
if ((feretMin > set.FeretMinimumMin && feretMin < set.FeretMinimumMax)) countAccepted++;
if ((feretMax < set.FeretMaximumMax && feretMax > set.FeretMaximumMin)) countAccepted++;
if ((cellDiameter > set.MinDiameter && cellDiameter < set.MaxDiameter)) countAccepted++;
if(countAccepted>2) result.State = CellResult.CellState.Match;
else if(countAccepted==2) result.State = CellResult.CellState.Unclassified;
else result.State = CellResult.CellState.Mismatch;
result.CellsRectangle = rec.Rectangle;
resultOfBlock.CellResults.Add(result);
}
return resultOfBlock;
if(_inputSource==null)抛出新异常();
var inputBitmap=BitmapFromSource(_inputSource);
如果(!validatePixelFormat(inputBitmap))
{
inputBitmap=转换位图(inputBitmap);
}
BitmapData BitmapData=inputBitmap.LockBits(
新矩形(0,0,inputBitmap.Width,inputBitmap.Height),
ImageLockMode.ReadWrite,inputBitmap.PixelFormat);
filter2.CenterColor=新的RGB(255、255、255);
过滤器2.半径=430;
filter2.ApplyInPlace(位图数据);
filter.CoupledSizeFiltering=true;
filter.MinWidth=10;
filter.MinHeight=10;
filter.MaxHeight=Int32.MaxValue;
filter.MaxWidth=Int32.MaxValue;
filter.ApplyInPlace(位图数据);
bc.FilterBlobs=true;
bc.MinWidth=10;
bc.MinHeight=10;
bc.MaxHeight=Int32.MaxValue;
bc.MaxWidth=Int32.MaxValue;
bc.CoupledSizeFiltering=false;
bc.ObjectsOrder=ObjectsOrder.Size;
ProcessImage(位图数据);
Blob[]blobs=bc.GetObjectsInformation();
BlockResult resultOfBlock=新的BlockResult();
inputBitmap.解锁位(bitmapData);
foreach(BLOB中的var rec)
{
var cuttedImage=inputBitmap.Clone(rec.Rectangle,inputBitmap.PixelFormat);//需要将每个单元格分离以执行进一步的分析
双细胞直径=Math.Sqrt(((rec.Area*(pixelSize*pixelSize))/Math.PI))*2;
细胞直径*=1000;
var feretMax=calculateFeretMax(pixelSize,cuttedImage);//类似的事情在这个方法中发生,所以我不想用额外的代码向您发送垃圾邮件
var feretMin=calculateFeretMin(像素大小,切割图像
if (_inputSource == null) throw new Exception();
var inputBitmap = BitmapFromSource(_inputSource);
if (!validatePixelFormat(inputBitmap))
{
inputBitmap = transformBitmap(inputBitmap);
}
BitmapData bitmapData = inputBitmap.LockBits(
new Rectangle(0, 0, inputBitmap.Width, inputBitmap.Height),
ImageLockMode.ReadWrite, inputBitmap.PixelFormat);
filter2.CenterColor = new RGB(255, 255, 255);
filter2.Radius = 430;
filter2.ApplyInPlace(bitmapData);
filter.CoupledSizeFiltering = true;
filter.MinWidth = 10;
filter.MinHeight = 10;
filter.MaxHeight = Int32.MaxValue;
filter.MaxWidth = Int32.MaxValue;
filter.ApplyInPlace(bitmapData);
bc.FilterBlobs = true;
bc.MinWidth = 10;
bc.MinHeight = 10;
bc.MaxHeight = Int32.MaxValue;
bc.MaxWidth = Int32.MaxValue;
bc.CoupledSizeFiltering = false;
bc.ObjectsOrder = ObjectsOrder.Size;
bc.ProcessImage(bitmapData);
Blob[] blobs = bc.GetObjectsInformation();
BlockResult resultOfBlock = new BlockResult();
inputBitmap.UnlockBits(bitmapData);
foreach (var rec in blobs)
{
var cuttedImage = inputBitmap.Clone(rec.Rectangle, inputBitmap.PixelFormat);//Need to get every cell seperated to perform further analysis
double cellDiameter = Math.Sqrt(((rec.Area*(pixelSize*pixelSize)) / Math.PI)) * 2;
cellDiameter *= 1000;
var feretMax = calculateFeretMax( pixelSize,cuttedImage); //Similar things like here happen in this method so I don't want to spam you with extra code
var feretMin = calculateFeretMin( pixelSize,cuttedImage);//Similar things like here happen in this method so I don't want to spam you with extra code
CellResult result = new CellResult(Math.Round(cellDiameter,2),Math.Round( feretMax,2),Math.Round( feretMin,2));
int countAccepted = 0;
if ((feretMin > set.FeretMinimumMin && feretMin < set.FeretMinimumMax)) countAccepted++;
if ((feretMax < set.FeretMaximumMax && feretMax > set.FeretMaximumMin)) countAccepted++;
if ((cellDiameter > set.MinDiameter && cellDiameter < set.MaxDiameter)) countAccepted++;
if(countAccepted>2) result.State = CellResult.CellState.Match;
else if(countAccepted==2) result.State = CellResult.CellState.Unclassified;
else result.State = CellResult.CellState.Mismatch;
result.CellsRectangle = rec.Rectangle;
resultOfBlock.CellResults.Add(result);
}
return resultOfBlock;