0
我正在嘗試創建一個小工具來統計圖片中的興趣區域。在C中調整對比度後的圖形修改#
爲了獲得更好的效果,我必須調整圖像的對比度。當我嘗試通過使用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;
}
這裏是用於計算投資回報代碼:
{
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的顏色變成綠色。計數程序正常工作,但正如我所說的,使用原始圖像。如何「告訴」計數程序使用調整後的圖像?我如何將修改寫入原始圖像?
感謝您的任何幫助。
蒂莫
我試了一下,它的工作原理。我在例程中寫了很多「意大利麪代碼」,我認爲我可能會提一下它。 – csharper1981 2013-04-24 13:09:54
我在我的問題中添加了代碼片段。 – csharper1981 2013-04-24 13:13:56
順便說一句,相當於*我試過它,它的工作原理* StackOverflow是upvote和/或接受答案;-) – 2013-04-24 13:20:30