C# 在C语言中调整对比度后的图形修改#
我正在尝试创建一个小工具来计算图片中感兴趣的区域 为了获得更好的效果,我必须调整图像的对比度。当我尝试使用Colormatrix系统时,我只得到原始图像的结果,而不是调整后的图像 这是我的密码。首先,我通过以下方式加载了一个图像:C# 在C语言中调整对比度后的图形修改#,c#,image,graphics,C#,Image,Graphics,我正在尝试创建一个小工具来计算图片中感兴趣的区域 为了获得更好的效果,我必须调整图像的对比度。当我尝试使用Colormatrix系统时,我只得到原始图像的结果,而不是调整后的图像 这是我的密码。首先,我通过以下方式加载了一个图像: Img = Image.FromFile(openFileDialog1.FileName); newBitmap = new Bitmap(openFileDialog1.FileName); 之后,我按照以下方式调整对比度: { domainContra
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;
}
下面是计算ROI的代码:
{
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;
{
计数=0;
位图nB=新位图(newBitmap.Width、newBitmap.Height);
int-lastCol=1;
//int y=250;
int countStart=1;
int countEnd=1;
以下是例行程序的代码:
对于(int y=1;ylastCol){diff=colVal-lastCol;}否则{diff=lastCol-colVal;}
如果(差异>50)
{
roiCount=真;
countEnd=x;
调整。设置像素(x,y,颜色。红色);
lastCol=colVal;
//计数++;
}
否则如果(差值<-50)
{
//roiCount=真;
countStart=x;
调整。设置像素(x,y,颜色。蓝色);
lastCol=colVal;
}
如果(ROI计数)
{
for(int i=countStart;i50)
{
计数++;
}
roiCount=假;
//计数++;
}
}
}
label17.Text=Convert.ToString(count);pictureBox1.Image=nB;
此外,它将ROI的颜色变成绿色。计数例程工作正常,但正如我所说,使用原始图像。我如何“告诉”计数例程使用调整后的图像?我如何将修改写入原始图像
谢谢你的帮助
Timo很难说-您没有向我们显示计数例程的代码,因此我们无法知道计数例程正在处理哪个位图。我希望您需要使用对比度调整后的图像作为
Bitmap adjusted = (Bitmap)pictureBox1.Image;
然后在调整后的上执行//计数例程中的所有操作
编辑问题后,从代码中可以清楚地看出您正在处理newBitmap
(这是加载的位图-对比度更改的来源)而不是对比度改变的位图。您应该在所有要使用合同调整位图的地方将newBitmap
更改为adjusted
,该位图的声明和设置如我的回答中所示。我尝试了它,效果很好。我写了很多“意大利面代码”在例行程序中,我认为提到它可能会让我有点分心。顺便说一句,我尝试过它,它对StackOverflow有效的等效方法是向上投票和/或接受答案;-)