csharper1981
csharper1981

Reputation: 1

Graphic modification after adjusting contrast in C#

I'm trying to create a small tool to count region of interests in a picture.

To get better results, I have to adjust the contrast of the image. When I try to do it by using a Colormatrix-System, I just get the results of the original image instead of the adjusted image.

Here is my Code. First of all I loaded an image by the following way:

Img = Image.FromFile(openFileDialog1.FileName);
newBitmap = new Bitmap(openFileDialog1.FileName);

Afterwards I adjusted Contrast in the following way:

{
    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;
}

And here is the code for counting the 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;

Additionlly, it turns the color of the rois into green. The counting routine works properly, but as I said, uses the original image. How can I "tell" the counting routine to use the adjusted image? How can I write the modifications to the original image?

Thanks for any help.

Timo

Upvotes: 0

Views: 384

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

Hard to tell - you're not showing us the code for // COUNTING ROUTINE, so we can't know which bitmap the counting routine is working on. I'd expect that you need to use the contrast-adjusted image as

Bitmap adjusted = (Bitmap)pictureBox1.Image;

and then perform everything you do in // COUNTING ROUTINE on adjusted.


After you edited the question it is clear from the code that you're working on newBitmap (which is the loaded bitmap - the source of your contrast change) instead of the contrast changed bitmap. You should change newBitmap in all places where you want to use the contract adjusted bitmap to adjusted, which is declared and set as shown above in my answer.

Upvotes: 1

Related Questions