Search in sources :

Example 31 with ColorConvertOp

use of java.awt.image.ColorConvertOp in project HearthStats.net-Uploader by HearthStats.

the class OcrBase method filter.

/**
 * Filters the image to make it easier to OCR, such as by turning it greyscale and increasing the contrast.
 *
 * @param image A cropped image
 * @param iteration The iteration number, zero-based
 * @return
 * @throws OcrException
 */
protected BufferedImage filter(BufferedImage image, int iteration) throws OcrException {
    int width = image.getWidth();
    int height = image.getHeight();
    int bigWidth = width * 3;
    int bigHeight = height * 3;
    // to gray scale
    BufferedImage grayscale = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
    BufferedImageOp grayscaleConv = new ColorConvertOp(image.getColorModel().getColorSpace(), grayscale.getColorModel().getColorSpace(), null);
    grayscaleConv.filter(image, grayscale);
    // blow it up for ocr
    BufferedImage newImage = new BufferedImage(bigWidth, bigHeight, BufferedImage.TYPE_INT_RGB);
    Graphics g = newImage.createGraphics();
    g.drawImage(grayscale, 0, 0, bigWidth, bigHeight, null);
    g.dispose();
    // invert image
    for (int x = 0; x < bigWidth; x++) {
        for (int y = 0; y < bigHeight; y++) {
            int rgba = newImage.getRGB(x, y);
            Color col = new Color(rgba, true);
            col = new Color(255 - col.getRed(), 255 - col.getGreen(), 255 - col.getBlue());
            newImage.setRGB(x, y, col.getRGB());
        }
    }
    // increase contrast
    try {
        RescaleOp rescaleOp = new RescaleOp(1.8f, -30, null);
        // Source and destination are the same.
        rescaleOp.filter(newImage, newImage);
    } catch (Exception e) {
        throw new OcrException("Error rescaling OCR image", e);
    }
    return newImage;
}
Also used : BufferedImageOp(java.awt.image.BufferedImageOp) RescaleOp(java.awt.image.RescaleOp) ColorConvertOp(java.awt.image.ColorConvertOp) BufferedImage(java.awt.image.BufferedImage)

Example 32 with ColorConvertOp

use of java.awt.image.ColorConvertOp in project Java8 by huhuhuHR.

the class OperateImage method grayImage.

/**
 * 图片灰化操作
 *
 * @param srcImage
 *            读取图片路径
 * @param toPath
 *            写入灰化后的图片路径
 * @param imageFormat
 *            图片写入格式
 */
public void grayImage(String srcImage, String toPath, String imageFormat) {
    try {
        BufferedImage src = ImageIO.read(new File(srcImage));
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(cs, null);
        src = op.filter(src, null);
        ImageIO.write(src, imageFormat, new File(toPath));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorSpace(java.awt.color.ColorSpace) BufferedImage(java.awt.image.BufferedImage)

Example 33 with ColorConvertOp

use of java.awt.image.ColorConvertOp in project BroadleafCommerce by BroadleafCommerce.

the class AlterHSB method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (src == null) {
        throw new NullPointerException("src image is null");
    }
    if (src == dst) {
        throw new IllegalArgumentException("src image cannot be the " + "same as the dst image");
    }
    boolean needToConvert = false;
    ColorModel srcCM = src.getColorModel();
    ColorModel dstCM;
    BufferedImage origDst = dst;
    if (srcCM instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) srcCM;
        src = icm.convertToIntDiscrete(src.getRaster(), false);
        srcCM = src.getColorModel();
    }
    if (dst == null) {
        dst = createCompatibleDestImage(src, null);
        dstCM = srcCM;
        origDst = dst;
    } else {
        dstCM = dst.getColorModel();
        if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) {
            needToConvert = true;
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        } else if (dstCM instanceof IndexColorModel) {
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        }
    }
    int[] originalPixels = ImageConverter.getPixels(src);
    int imageWidth = dst.getWidth();
    int imageHeight = dst.getHeight();
    int r = 0;
    int g = 0;
    int b = 0;
    int index = 0;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            r = (originalPixels[index] >> 16) & 0xff;
            g = (originalPixels[index] >> 8) & 0xff;
            b = (originalPixels[index] >> 0) & 0xff;
            float[] hsb = Color.RGBtoHSB(r, g, b, null);
            float h = hsb[0] * hue;
            float s = hsb[1] * saturation;
            float br = hsb[2] * brightness;
            // fix overflows
            if (h > 360)
                h = 360;
            if (h < 0)
                h = 0;
            if (s > 1)
                s = 1;
            if (s < 0)
                s = 0;
            if (br > 1)
                br = 1;
            if (br < 0)
                br = 0;
            Color rgb = new Color(Color.HSBtoRGB(h, s, br));
            r = rgb.getRed();
            g = rgb.getGreen();
            b = rgb.getBlue();
            originalPixels[index] = (originalPixels[index] & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
            index++;
        }
    }
    dst = ImageConverter.getImage(originalPixels, imageWidth, imageHeight);
    if (needToConvert) {
        ColorConvertOp ccop = new ColorConvertOp(hints);
        ccop.filter(dst, origDst);
    } else if (origDst != dst) {
        java.awt.Graphics2D g2 = origDst.createGraphics();
        try {
            g2.drawImage(dst, 0, 0, null);
        } finally {
            g2.dispose();
        }
    }
    return origDst;
}
Also used : BufferedImage(java.awt.image.BufferedImage) ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 34 with ColorConvertOp

use of java.awt.image.ColorConvertOp in project BroadleafCommerce by BroadleafCommerce.

the class AlterRGB method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (src == null) {
        throw new NullPointerException("src image is null");
    }
    if (src == dst) {
        throw new IllegalArgumentException("src image cannot be the " + "same as the dst image");
    }
    boolean needToConvert = false;
    ColorModel srcCM = src.getColorModel();
    ColorModel dstCM;
    BufferedImage origDst = dst;
    if (srcCM instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) srcCM;
        src = icm.convertToIntDiscrete(src.getRaster(), false);
        srcCM = src.getColorModel();
    }
    if (dst == null) {
        dst = createCompatibleDestImage(src, null);
        dstCM = srcCM;
        origDst = dst;
    } else {
        dstCM = dst.getColorModel();
        if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) {
            needToConvert = true;
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        } else if (dstCM instanceof IndexColorModel) {
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        }
    }
    int[] originalPixels = ImageConverter.getPixels(src);
    int imageWidth = dst.getWidth();
    int imageHeight = dst.getHeight();
    int r = 0;
    int g = 0;
    int b = 0;
    int index = 0;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            r = (originalPixels[index] >> 16) & 0xff;
            g = (originalPixels[index] >> 8) & 0xff;
            b = (originalPixels[index] >> 0) & 0xff;
            r += red;
            g += green;
            b += blue;
            // fix overflows
            if (r > 255)
                r = 255;
            if (r < 0)
                r = 0;
            if (g > 255)
                g = 255;
            if (g < 0)
                g = 0;
            if (b > 255)
                b = 255;
            if (b < 0)
                b = 0;
            originalPixels[index] = (originalPixels[index] & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
            index++;
        }
    }
    dst = ImageConverter.getImage(originalPixels, imageWidth, imageHeight);
    if (needToConvert) {
        ColorConvertOp ccop = new ColorConvertOp(hints);
        ccop.filter(dst, origDst);
    } else if (origDst != dst) {
        java.awt.Graphics2D g2 = origDst.createGraphics();
        try {
            g2.drawImage(dst, 0, 0, null);
        } finally {
            g2.dispose();
        }
    }
    return origDst;
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Example 35 with ColorConvertOp

use of java.awt.image.ColorConvertOp in project BroadleafCommerce by BroadleafCommerce.

the class AutoLevelsRGB method filter.

/* (non-Javadoc)
     * @see java.awt.image.BufferedImageOp#filter(java.awt.image.BufferedImage, java.awt.image.BufferedImage)
     */
public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    if (src == null) {
        throw new NullPointerException("src image is null");
    }
    if (src == dst) {
        throw new IllegalArgumentException("src image cannot be the same as the dst image");
    }
    boolean needToConvert = false;
    ColorModel srcCM = src.getColorModel();
    ColorModel dstCM;
    BufferedImage origDst = dst;
    if (srcCM instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) srcCM;
        src = icm.convertToIntDiscrete(src.getRaster(), false);
        srcCM = src.getColorModel();
    }
    if (dst == null) {
        dst = createCompatibleDestImage(src, null);
        dstCM = srcCM;
        origDst = dst;
    } else {
        dstCM = dst.getColorModel();
        if (srcCM.getColorSpace().getType() != dstCM.getColorSpace().getType()) {
            needToConvert = true;
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        } else if (dstCM instanceof IndexColorModel) {
            dst = createCompatibleDestImage(src, null);
            dstCM = dst.getColorModel();
        }
    }
    int[] originalPixels = ImageConverter.getPixels(src);
    int imageWidth = dst.getWidth();
    int imageHeight = dst.getHeight();
    /*
         * Sort all the red pixels from low to high and establish
         * the clipping regions. We also note the delta from the 
         * lowest and highest leftover pixels to black and white,
         * respectively.
         */
    int[] redPixels = new int[originalPixels.length];
    for (int j = 0; j < originalPixels.length; j++) {
        redPixels[j] = (originalPixels[j] >> 16) & 0xff;
    }
    Arrays.sort(redPixels);
    int redStart = redPixels[(int) (redPixels.length * BOTTOMCLIP)];
    int redEnd = redPixels[redPixels.length - (int) (redPixels.length * TOPCLIP)];
    int redEndDelta = 255 - redEnd;
    int redStartDelta = redStart;
    int[] greenPixels = new int[originalPixels.length];
    for (int j = 0; j < originalPixels.length; j++) {
        greenPixels[j] = (originalPixels[j] >> 8) & 0xff;
    }
    Arrays.sort(greenPixels);
    int greenStart = greenPixels[(int) (greenPixels.length * BOTTOMCLIP)];
    int greenEnd = greenPixels[greenPixels.length - (int) (greenPixels.length * TOPCLIP)];
    int greenEndDelta = 255 - greenEnd;
    int greenStartDelta = greenStart;
    int[] bluePixels = new int[originalPixels.length];
    for (int j = 0; j < originalPixels.length; j++) {
        bluePixels[j] = (originalPixels[j] >> 0) & 0xff;
    }
    Arrays.sort(bluePixels);
    int blueStart = bluePixels[(int) (bluePixels.length * BOTTOMCLIP)];
    int blueEnd = bluePixels[bluePixels.length - (int) (bluePixels.length * TOPCLIP)];
    int blueEndDelta = 255 - blueEnd;
    int blueStartDelta = blueStart;
    int r = 0;
    int g = 0;
    int b = 0;
    int index = 0;
    for (int y = 0; y < imageHeight; y++) {
        for (int x = 0; x < imageWidth; x++) {
            r = (originalPixels[index] >> 16) & 0xff;
            g = (originalPixels[index] >> 8) & 0xff;
            b = (originalPixels[index] >> 0) & 0xff;
            if (r > redStart && r < redEnd) {
                if (redEndDelta > 0) {
                    if (redEnd - r == 0) {
                        r = 255;
                    } else {
                        /*
                             * If there was a white shift, distribute all the pixels proportionally up
                             */
                        r = redEnd + redEndDelta - (((redEnd - r) * (redEnd + redEndDelta)) / redEnd);
                    }
                }
                if (redStartDelta > 0) {
                    if (r - redStartDelta == 0) {
                        r = 0;
                    } else {
                        /*
                             * If there was a black shift, distribute all the pixels proportionally down
                             */
                        r = redEnd - (((redEnd - (redStart - redStartDelta)) * (redEnd - r)) / (redEnd - redStart));
                    }
                }
            } else if (r <= redStart) {
                r = 0;
            } else {
                r = 255;
            }
            if (g > greenStart && g < greenEnd) {
                if (greenEndDelta > 0) {
                    if (greenEnd - g == 0) {
                        g = 255;
                    } else {
                        g = greenEnd + greenEndDelta - (((greenEnd - g) * (greenEnd + greenEndDelta)) / greenEnd);
                    }
                }
                if (greenStartDelta > 0) {
                    if (g - greenStartDelta == 0) {
                        g = 0;
                    } else {
                        g = greenEnd - (((greenEnd - (greenStart - greenStartDelta)) * (greenEnd - g)) / (greenEnd - greenStart));
                    }
                }
            } else if (g <= greenStart) {
                g = 0;
            } else {
                g = 255;
            }
            if (b > blueStart && b < blueEnd) {
                if (blueEndDelta > 0) {
                    if (blueEnd - b == 0) {
                        b = 255;
                    } else {
                        b = blueEnd + blueEndDelta - (((blueEnd - b) * (blueEnd + blueEndDelta)) / blueEnd);
                    }
                }
                if (blueStartDelta > 0) {
                    if (b - blueStartDelta == 0) {
                        b = 0;
                    } else {
                        b = blueEnd - (((blueEnd - (blueStart - blueStartDelta)) * (blueEnd - b)) / (blueEnd - blueStart));
                    }
                }
            } else if (b <= blueStart) {
                b = 0;
            } else {
                b = 255;
            }
            // fix overflows
            if (r > 255)
                r = 255;
            if (r < 0)
                r = 0;
            if (g > 255)
                g = 255;
            if (g < 0)
                g = 0;
            if (b > 255)
                b = 255;
            if (b < 0)
                b = 0;
            originalPixels[index] = (originalPixels[index] & 0xff000000) | (r << 16) | (g << 8) | (b << 0);
            index++;
        }
    }
    dst = ImageConverter.getImage(originalPixels, imageWidth, imageHeight);
    if (needToConvert) {
        ColorConvertOp ccop = new ColorConvertOp(hints);
        ccop.filter(dst, origDst);
    } else if (origDst != dst) {
        java.awt.Graphics2D g2 = origDst.createGraphics();
        try {
            g2.drawImage(dst, 0, 0, null);
        } finally {
            g2.dispose();
        }
    }
    return origDst;
}
Also used : ColorConvertOp(java.awt.image.ColorConvertOp) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) BufferedImage(java.awt.image.BufferedImage) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

ColorConvertOp (java.awt.image.ColorConvertOp)39 BufferedImage (java.awt.image.BufferedImage)37 ColorModel (java.awt.image.ColorModel)12 ColorSpace (java.awt.color.ColorSpace)10 IndexColorModel (java.awt.image.IndexColorModel)10 ICC_ColorSpace (java.awt.color.ICC_ColorSpace)5 Point (java.awt.Point)4 WritableRaster (java.awt.image.WritableRaster)3 IIOException (javax.imageio.IIOException)3 Color (java.awt.Color)2 Graphics (java.awt.Graphics)2 ComponentColorModel (java.awt.image.ComponentColorModel)2 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)2 Test (org.junit.jupiter.api.Test)2 ImageProcessingException (com.drew.imaging.ImageProcessingException)1 Metadata (com.drew.metadata.Metadata)1 Foundation (com.intellij.ui.mac.foundation.Foundation)1 ID (com.intellij.ui.mac.foundation.ID)1 Vector3f (com.jme3.math.Vector3f)1 ImageFeature (de.neotos.imageanalyzer.ImageFeature)1