2013-04-24 2 views
0

그림에서 관심 영역을 계산하는 작은 도구를 만들려고합니다.C에서 대비를 조정 한 후 그래픽 수정

더 나은 결과를 얻으려면 이미지의 대비를 조정해야합니다. Colormatrix-System을 사용하여이 작업을 수행하려고하면 조정 된 이미지 대신 원본 이미지의 결과가 나타납니다.

다음은 나의 코드입니다.

{ 
    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; 
: 그 후 나는 다음과 같은 방법으로 대비를 조정

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

: 우선 나는 다음과 같은 방법으로 이미지를로드 ROI를 계산하는 코드는 여기

{ 
    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의 색깔을 녹색으로 바꾼다. 카운팅 루틴은 제대로 작동하지만, 내가 말했듯이 원래의 이미지를 사용합니다. 조정 된 이미지를 사용하려면 계산 루틴에 "알리는"방법은 무엇입니까? 원본 이미지에 수정 사항을 쓰려면 어떻게해야합니까?

도움 주셔서 감사합니다.

티모

답변

1

말할 하드 - 당신이 // COUNTING ROUTINE 우리에게 코드를 표시하지 않는, 그래서 우리는 계산 루틴이 작업하는 비트 맵 알 수 없다. 난 당신이 adjusted// COUNTING ROUTINE에서 모든 일을 수행 한 후

Bitmap adjusted = (Bitmap)pictureBox1.Image; 

로 명암 조정 된 이미지를 사용하고 필요가 기대.


당신이 질문을 편집 한 후 당신이 (로드 된 비트 맵이다 - 당신의 콘트라스트 변화의 소스) newBitmap에서 작업하고있는 코드에서 분명 대신 대비의 비트 맵을 변경했습니다. 계약 조정 비트 맵을 adjusted으로 사용하려는 모든 곳에서 newBitmap을 변경해야합니다. 이는 대답에 위와 같이 선언되고 설정됩니다.

+0

시도해 보았습니다. 나는 루틴의 내부에 많은 "스파게티 코드"를 썼다. 나는 그것을 언급하는 것이 약간 정신이 빠르다 고 생각했다. – csharper1981

+0

내 질문에 코드 조각을 추가했습니다. – csharper1981

+0

그건 그렇고, * 내가 그것을 시도하고 StackOverflow에서 * upvote 및/또는 답변을 받아 들일 수있다 ;-) –

관련 문제