2011-11-08 4 views
2

가능한 중복 :
Remove surrounding whitespace from an image자르기 공백

나는 이미지의 위, 아래, 왼쪽 및 오른쪽에있는 공백을 잘라하는 데 도움을 찾고 있어요.

나는 following question/answer on SO을 찾았지만 대답은 더 이상 나타나지 않는 제 3 자 사이트에 대한 링크입니다.

+0

타사 라이브러리 또는 원시 코드를 찾고 계십니까? 공백을 검사하는 가장자리를 반복하고 이미지의 크기를 조정하는 것은 매우 쉽지만 실제로는 최적이 아닙니다. 이런 종류의 기능을 포함하는 서드 파티 라이브러리가 훨씬 더 최적의 장원입니다. – Graymatter

+0

제 3 자 라이브러리에 문제가 없습니다. 내부 프로젝트이므로 비용이 들지 않습니다. –

+0

[이 답변] (http://stackoverflow.com/questions/4820212/automatically-trim-a-bitmap-to-minimum-size/4821100#4821100)을 확인하십시오. 투명한 공백을 가정하지만, 필요에 따라 논리를 쉽게 조정할 수 있습니다. –

답변

12

해결 방법은 here이지만 빈 이미지를 입력 할 수 있도록 반환 코드 섹션이 수정되었습니다.이 경우 원래 이미지가 반환됩니다.

class ImageCrop 
{ 
    public static byte[][] GetRGB(Bitmap bmp) 
    { 
     BitmapData bmp_data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); 
     IntPtr ptr = bmp_data.Scan0; 
     int num_pixels = bmp.Width * bmp.Height, num_bytes = bmp_data.Stride * bmp.Height, padding = bmp_data.Stride - bmp.Width * 3, i = 0, ct = 1; 
     byte[] r = new byte[num_pixels], g = new byte[num_pixels], b = new byte[num_pixels], rgb = new byte[num_bytes]; 
     System.Runtime.InteropServices.Marshal.Copy(ptr, rgb, 0, num_bytes); 

     for (int x = 0; x < num_bytes - 3; x += 3) 
     { 
      if (x == (bmp_data.Stride * ct - padding)) { x += padding; ct++; }; 
      r[i] = rgb[x]; g[i] = rgb[x + 1]; b[i] = rgb[x + 2]; i++; 
     } 
     bmp.UnlockBits(bmp_data); 
     return new byte[3][] { r, g, b }; 
    } 
    public static Image AutoCrop(Bitmap bmp) 
    { 
     //Get an array containing the R,G,B components of each pixel 
     var pixels = GetRGB(bmp); 

     int h = bmp.Height - 1, w = bmp.Width, top = 0, bottom = h, left = bmp.Width, right = 0, white = 0; 
     int tolerance = 95; // 95% 

     bool prev_color = false; 
     for (int i = 0; i < pixels[0].Length; i++) 
     { 
      int x = (i % (w)), y = (int)(Math.Floor((decimal)(i/w))), tol = 255 * tolerance/100; 
      if (pixels[0][i] >= tol && pixels[1][i] >= tol && pixels[2][i] >= tol) { white++; right = (x > right && white == 1) ? x : right; } 
      else { left = (x < left && white >= 1) ? x : left; right = (x == w - 1 && white == 0) ? w - 1 : right; white = 0; } 
      if (white == w) { top = (y - top < 3) ? y : top; bottom = (prev_color && x == w - 1 && y > top + 1) ? y : bottom; } 
      left = (x == 0 && white == 0) ? 0 : left; bottom = (y == h && x == w - 1 && white != w && prev_color) ? h + 1 : bottom; 
      if (x == w - 1) { prev_color = (white < w) ? true : false; white = 0; } 
     } 
     right = (right == 0) ? w : right; left = (left == w) ? 0 : left; 

     //Crop the image 
     if (bottom - top > 0) 
     { 
      Bitmap bmpCrop = bmp.Clone(new Rectangle(left, top, right - left + 1, bottom - top), bmp.PixelFormat); 

      return (Bitmap)(bmpCrop); 
     } 
     else 
     { 
      return bmp; 
     } 
    } 


} 
+0

훌륭한 결과물, 더 많은 투표가 있어야합니다. –

+0

.Clone() 호출에 전달되는 잘못된 차원과 관련된 "메모리 부족"예외가 발생하는 문제가있었습니다. 자세한 내용은 http://stackoverflow.com/questions/199468/c-sharp-image-clone-out-of-memory-exception을 참조하십시오. 전체 이미지가 잘려 나갈 때 "오른쪽 - 왼쪽 + 1"에서 +1 오류가 발생하는 것 같습니다. 결국, 나는 좀 더 명확하고 간결함을 희생시키면서 더 적은 메모리를 사용하는이 방법의 자체 버전을 썼다. 연결된 사본에서 찾을 수 있습니다. –