我正在尝试创建一个小工具来计算图片中的兴趣区域。
为了获得更好的效果,我必须调整图像的对比度。当我尝试使用 Colormatrix-System 来完成时,我只得到原始图像的结果,而不是调整后的图像。
这是我的代码。首先,我通过以下方式加载了图像:
Img = Image.FromFile(openFileDialog1.FileName);
newBitmap = new Bitmap(openFileDialog1.FileName);
之后我通过以下方式调整了对比度:
{
domainContrast.Text = trackBar2.Value.ToString();
contrast = 0.04f * trackBar2.Value;
Bitmap bm = new Bitmap(newBitmap.Width, newBitmap.Height);
Graphics g = Graphics.FromImage(bm);
ImageAttributes ia = new ImageAttributes();
ColorMatrix cm = new ColorMatrix(new float[][] {
new float[] {contrast, 0f, 0f, 0f, 0f},
new float[] {0f, contrast, 0f, 0f, 0f},
new float[] {0f, 0f, contrast, 0f, 0f},
new float[] {0f, 0f, 0f, 1f , 0f},
new float[] {0.001f, 0.001f, 0.001f, 0f, 1f}
});
ia.SetColorMatrix(cm);
g.DrawImage(newBitmap, new Rectangle(0, 0, newBitmap.Width, newBitmap.Height), 0, 0, newBitmap.Width, newBitmap.Height, GraphicsUnit.Pixel, ia);
g.Dispose();
ia.Dispose();
pictureBox1.Image = bm;
}
这是计算 rois 的代码:
{
count = 0;
Bitmap nB = new Bitmap(newBitmap.Width, newBitmap.Height);
int lastCol = 1;
//int y =250;
int countStart = 1;
int countEnd = 1;
Here is the code of the routine:
for (int y = 1; y < newBitmap.Height - 1; y++)
{
for (int x = 1; x < newBitmap.Width - 1; x++)
{
Color pixel = newBitmap.GetPixel(x, y);
int colVal = (pixel.R + pixel.G + pixel.B);
if (lastCol == 1) lastCol = (pixel.R + pixel.G + pixel.B);
int diff;
diff = colVal - lastCol;
//if (colVal > lastCol) { diff = colVal - lastCol; } else { diff = lastCol - colVal; }
if (diff > 50)
{
roiCount = true;
countEnd = x;
adjusted.SetPixel(x, y, Color.Red);
lastCol = colVal;
//count++;
}
else if (diff < -50)
{
//roiCount = true;
countStart = x;
adjusted.SetPixel(x, y, Color.Blue);
lastCol = colVal;
}
if (roiCount)
{
for (int i = countStart; i < countEnd; i++)
{
adjusted.SetPixel(i, y, Color.Green);
}
int roiCenter = (countStart + countEnd) / 2;
//int roiYTest = y + 1;
Color roiCenterPixel = newBitmap.GetPixel(roiCenter, y);
int colRoiCenterPixel = roiCenterPixel.R + roiCenterPixel.G + roiCenterPixel.B;
Color PixelRoi = newBitmap.GetPixel(roiCenter, y + 1);
int colRoi = (PixelRoi.R + PixelRoi.G + PixelRoi.B);
int diffRoi = colRoiCenterPixel - colRoi;
if (diffRoi < -50 || diffRoi > 50)
{
count++;
}
roiCount = false;
//count++;
}
}
}
label17.Text = Convert.ToString(count); pictureBox1.Image = nB;
此外,它将 rois 的颜色变为绿色。计数程序正常工作,但正如我所说,使用原始图像。如何“告诉”计数程序使用调整后的图像?如何将修改写入原始图像?
谢谢你的帮助。
蒂莫