2017-12-24 11 views
0

입력으로 bufferedImage를 사용하고 모든 검은 색 (R < 32, G < 32, B < 32)을 검정색으로 매핑하고 다른 색상을 흰색으로 매핑하는 프로그램을 작성하려고합니다. OCR (OCR 엔진은 BufferedImage를 입력으로 사용합니다). 픽셀을 반복하지 않고도이를 수행 할 수 있습니까? BlackWhiteColorModel이Java BufferedImage 맵 색상

public class BlackWhiteColorModel extends ColorModel { 

public BlackWhiteColorModel(int bits) { 
    super(bits); 
} 

@Override 
public int getRed(int pixel) { 
    int[] rgb = getRgb(pixel); 

    if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) { 
     return 0; 
    } else { 
     return 255; 
    } 
} 

@Override 
public int getGreen(int pixel) { 
    int[] rgb = getRgb(pixel); 

    if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) { 
     return 0; 
    } else { 
     return 255; 
    } 
} 

@Override 
public int getBlue(int pixel) { 
    int[] rgb = getRgb(pixel); 

    if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) { 
     return 0; 
    } else { 
     return 255; 
    } 
} 

@Override 
public int getAlpha(int pixel) { 
    return pixel; 
} 

private int[] getRgb(int pixel) { 
    int r = (pixel) & 0xFF; 
    int g = (pixel >> 8) & 0xFF; 
    int b = (pixel >> 16) & 0xFF; 
    int a = (pixel >> 24) & 0xFF; 

    return new int[]{r, g, b, a}; 
} 



} 

그러나, 나는 isCompatibleRasterException와 끝까지로 정의되는 경우 은 즉, 나는

public static BufferedImage BlackAndWhite(BufferedImage image) { 

    ColorModel model = new BlackWhiteColorModel(DataBuffer.TYPE_INT); 
    WritableRaster raster = image.getRaster(); 

    BufferedImage newImage = new BufferedImage(model, raster, false, null); 

    return newImage; 
} 

을 시도했다. 아무도 내게 약간의 조언을 줄 수 있습니까?

+0

당신이 필요하십니까이다 더 많은 일을 할 경우 특히 ** 블랙 & 화이트 * * 이미지 또는 ** GrayScale ** 하나? – STaefi

답변

0

(1) 계약 방법 중 하나 (isCompatibleRaster)를 구현하지 않았습니다.

@Override 
public boolean isCompatibleRaster(Raster raster) { 
    return true; 
} 

(2) getRed, getGreengetBlue이 같은 코드를 가지고 : 여기에 고급 문제 문을 주어 실행되는 지나치게 낙관적 구현입니다. 다음은 공통 코드를 getColor이라는 새로운 방법으로 제외시키는 완전한 프로그램입니다. 이 프로그램은 오류없이 실행 : enter image description here

난 당신이 성능 문제로 인해 픽셀을 반복하고 싶지 않은 추측 :

import java.awt.*; 
import java.awt.image.*; 

public class BlackWhiteColorModel extends ColorModel { 
    public static void main(String[] args) { 
     BufferedImage bufferedImage = new BufferedImage(200,200,BufferedImage.TYPE_INT_RGB); 
     Graphics g = bufferedImage.getGraphics(); 
     g.drawString("Hello, World", 20,20); 
     blackAndWhite(bufferedImage); 
    } 

    public static BufferedImage blackAndWhite(BufferedImage image) { 
     ColorModel model = new BlackWhiteColorModel(DataBuffer.TYPE_INT); 
     WritableRaster raster = image.getRaster(); 

     BufferedImage newImage = new BufferedImage(model, raster, false, null); 

     return newImage; 
    } 

    public BlackWhiteColorModel(int bits) { 
     super(bits); 
    } 

    private int getColor(int pixel) { 
     int[] rgb = getRgb(pixel); 

     if (rgb[0] < 32 && rgb[1] < 32 && rgb[2] < 32) { 
      return 0; 
     } else { 
      return 255; 
     } 
    } 

    @Override 
    public int getRed(int pixel) { 
     return getColor(pixel); 
    } 

    @Override 
    public int getGreen(int pixel) { 
     return getColor(pixel); 
    } 

    @Override 
    public int getBlue(int pixel) { 
     return getColor(pixel); 
    } 

    @Override 
    public int getAlpha(int pixel) { 
     return pixel; 
    } 

    @Override 
    public boolean isCompatibleRaster(Raster raster) { 
     return true; 
    } 

    private int[] getRgb(int pixel) { 
     int r = (pixel) & 0xFF; 
     int g = (pixel >> 8) & 0xFF; 
     int b = (pixel >> 16) & 0xFF; 
     int a = (pixel >> 24) & 0xFF; 

     return new int[]{r, g, b, a}; 
    } 
} 
+0

감사합니다. 그러나 다른 colorModel로 새 이미지를 만드는 내 방식은 작동하지 않습니다. BlackAndWhite (BufferedImage 이미지)의 출력을 저장하려면 파일이 생성되지 않고 예외도 발생하지 않습니다. 또한 텍스트 인식이 작동하지 않습니다. 다른 (기능적인) 방법, 다른 ColorModel이지만 동일한 데이터로 새 이미지를 만드는 방법에 대해 아시겠습니까? –

+0

범위를 확장하면 질문을 변경합니다.이 대답을 수락하고 확장 된 범위로 새 질문을하십시오. –

0

은 당신이 원하는 것은 임계 동작 예입니다. 그게 사실이면 난 그냥 같이 OpenCV를 사용하는 것이 좋습니다 높은 것 :

BufferedImage image = null; //use your image here 
//Convert image to OpenCv Mat 
byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); 
Mat mat = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC3); 
mat.put(0, 0, pixels); 

//do something with the Mat e.g: 
Imgproc.threshold(...); 

//Convert back 
mat.get(0, 0, pixels); 

성능은 이미지 처리가 적어도 10 배 빠른

+0

고맙습니다! 나는 휴일 동안 그것을 점검 할 것이다. –