下面列出了java.awt.image.BufferedImage#getMinY() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Pixel[][] getImagePixel(BufferedImage srcImg, int width, int height) {
BufferedImage bi = null;
try {
bi = resizeImage(srcImg, width, height, BufferedImage.TYPE_INT_RGB);
} catch (Exception e) {
e.printStackTrace();
return null;
}
int minx = bi.getMinX();
int miny = bi.getMinY();
Pixel[][] rgbMatrix = new Pixel[width - minx][height - miny];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j);
int red = (pixel & 0xff0000) >> 16;
int green = (pixel & 0xff00) >> 8;
int blue = (pixel & 0xff);
Pixel p = new Pixel();
p.red = red;
p.green = green;
p.blue = blue;
rgbMatrix[i - minx][j - miny] = p;
}
}
return rgbMatrix;
}
public static int[][] getGrayPixel(BufferedImage srcImg, int width, int height) {
BufferedImage bi = null;
try {
bi = resizeImage(srcImg, width, height, BufferedImage.TYPE_INT_RGB);
} catch (Exception e) {
e.printStackTrace();
return null;
}
int minx = bi.getMinX();
int miny = bi.getMinY();
int[][] matrix = new int[width - minx][height - miny];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
int pixel = bi.getRGB(i, j);
int red = (pixel & 0xff0000) >> 16;
int green = (pixel & 0xff00) >> 8;
int blue = (pixel & 0xff);
int gray = (int) (red * 0.3 + green * 0.59 + blue * 0.11);
matrix[i][j] = gray;
}
}
return matrix;
}
/**
* 将图片改变成像素数据,同时获取搜索图片时的图片相关参数
* @param bufferedImage 要转换的成像素数据的图片
* @return {@link RgbImageComparerBean}
*/
public RgbImageComparerBean getPX(BufferedImage bufferedImage) {
int width = bufferedImage.getWidth();
int height = bufferedImage.getHeight();
int minx = bufferedImage.getMinX();
int miny = bufferedImage.getMinY();
RgbImageComparerBean rgb = new RgbImageComparerBean();
int colorArray[][] = new int[width][height];
for (int i = minx; i < width; i++) {
for (int j = miny; j < height; j++) {
colorArray[i][j] = bufferedImage.getRGB(i, j);
}
}
rgb.setColorArray(colorArray);
return rgb;
}
/**
* 计算图片颜色的平均值
*
* @param fileName 图片路径
* @return 颜色的平均值
*/
public static double color(String fileName) {
File file = new File(fileName);
BufferedImage image = null;
try {
image = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
int width = image.getWidth();
int height = image.getHeight();
int minX = image.getMinX();
int minY = image.getMinY();
long sum = 0;
int count = 0;
for (int i = minX; i < width; i++) {
for (int j = minY; j < height; j++) {
int rgb = image.getRGB(i, j);
sum += rgb;
count++;
}
}
double avg = (double) sum / count;
return avg;
}
/**
* 去除杂点
* @param image
* @param sideLength
* @return
*/
public static BufferedImage pointFilter(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
int minx = image.getMinX();
int miny = image.getMinY();
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
BufferedImage outImage = new BufferedImage(width, height, image.getType());
for (int x = minx; x < width; x++) {
for (int y = miny; y < height; y++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed() == white.getRed()){
outImage.setRGB(x, y, color.getRGB());
continue;
}
//先判断左右
int self = color.getRed();
int left = ImageUtil.getColor(image, x-1, y).getRed();
int right = ImageUtil.getColor(image, x+1, y).getRed();
int up = ImageUtil.getColor(image, x, y-1).getRed();
int down = ImageUtil.getColor(image, x, y+1).getRed();
if(left == right && right == up && up == down && down != 0 && self == 0){
setRGB(outImage, x, y, white.getRGB());
}
}
}
return outImage;
}
/**
* 根据坐标获取像素点的Color对象,如果像素位置位于图片外,则默认为白色
* @param image
* @param x
* @param y
* @return
*/
public static Color getColor(BufferedImage image, int x, int y){
if(x < image.getMinX() || x > image.getWidth() - 1){
return new Color(255, 255, 255);
}
if(y < image.getMinY() || y > image.getHeight() - 1){
return new Color(255, 255, 255);
}
return new Color(image.getRGB(x, y));
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* 去除杂点、杂线
* @param image
* @param length
* @return
*/
public static BufferedImage point2Filter(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
int minx = image.getMinX();
int miny = image.getMinY();
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
BufferedImage outImage = new BufferedImage(width, height, image.getType());
for (int x = minx; x < width; x++) {
for (int y = miny; y < height; y++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed() == white.getRed()){
outImage.setRGB(x, y, color.getRGB());
continue;
}
//先判断左右
int self = color.getRed();
int left_1 = ImageUtil.getColor(image, x-1, y).getRed();
int left_2 = ImageUtil.getColor(image, x-1, y+1).getRed();
int right_1 = ImageUtil.getColor(image, x+1, y).getRed();
int right_2 = ImageUtil.getColor(image, x+1, y+1).getRed();
int up_1 = ImageUtil.getColor(image, x, y-1).getRed();
int up_2 = ImageUtil.getColor(image, x+1, y-1).getRed();
int down_1 = ImageUtil.getColor(image, x, y+1).getRed();
int down_2 = ImageUtil.getColor(image, x+1, y+1).getRed();
int left = left_1 == 0 && left_2 == 0 ? black.getRed() : white.getRed();
int right = right_1 == 0 && right_2 == 0 ? black.getRed() : white.getRed();
int up = up_1 == 0 && up_2 == 0 ? black.getRed() : white.getRed();
int down = down_1 == 0 && down_2 == 0 ? black.getRed() : white.getRed();
// if(left == right && left == 0 && up == down && up != 0 && self == 0){
// //左右为黑 上下为白
// setRGB(outImage, x, y, white.getRGB());//自己
// setRGB(outImage, x-1, y, white.getRGB());//左
// setRGB(outImage, x-1, y+1, white.getRGB());//左
// setRGB(outImage, x+1, y, white.getRGB());//右
// setRGB(outImage, x+1, y+1, white.getRGB());//右
// }else if(left == right && left != 0 && up == down && up == 0 && self == 0){
// //上下为黑 左右为白
// setRGB(outImage, x, y, white.getRGB());//自己
// setRGB(outImage, x, y-1, white.getRGB());//上
// setRGB(outImage, x+1, y-1, white.getRGB());//上
// setRGB(outImage, x, y+1, white.getRGB());//下
// setRGB(outImage, x+1, y+1, white.getRGB());//下
// }else if(left == right && up == down && left == up && left != 0 && self == 0){
// //上下左右均为白色
// setRGB(outImage, x, y, white.getRGB());//自己
// setRGB(outImage, x, y-1, white.getRGB());//上
// setRGB(outImage, x+1, y-1, white.getRGB());//上
// setRGB(outImage, x, y+1, white.getRGB());//下
// setRGB(outImage, x+1, y+1, white.getRGB());//下
// setRGB(outImage, x-1, y, white.getRGB());//左
// setRGB(outImage, x-1, y+1, white.getRGB());//左
// setRGB(outImage, x+1, y, white.getRGB());//右
// setRGB(outImage, x+1, y+1, white.getRGB());//右
// }else{
int count = 0;
count = left != 0 ? count+1 : count;
count = right != 0 ? count+1 : count;
count = up != 0 ? count+1 : count;
count = down != 0 ? count+1 : count;
if(count > 2){
setRGB(outImage, x, y, white.getRGB());//自己
}else{
setRGB(outImage, x, y, black.getRGB());
}
// }
}
}
return outImage;
}
/**
* 去除杂点、杂线
* @param image
* @param length
* @return
*/
public static BufferedImage line2Filter(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
int minx = image.getMinX();
int miny = image.getMinY();
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
BufferedImage outImage = new BufferedImage(width, height, image.getType());
for (int x = minx; x < width; x++) {
for (int y = miny; y < height; y++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed() == white.getRed()){
outImage.setRGB(x, y, color.getRGB());
continue;
}
//先判断左右
int self_1 = color.getRed();
int self_2 = ImageUtil.getColor(image, x+1, y).getRed();
int self_3 = ImageUtil.getColor(image, x, y+1).getRed();
int self_4 = ImageUtil.getColor(image, x+1, y+1).getRed();
if(self_1 == self_2 && self_2 == self_3 && self_3 == self_4 && self_1 != 0){
continue;
}
int self = 0;
int left_1 = ImageUtil.getColor(image, x-1, y).getRed();
int left_2 = ImageUtil.getColor(image, x-1, y+1).getRed();
int right_1 = ImageUtil.getColor(image, x+2, y).getRed();
int right_2 = ImageUtil.getColor(image, x+2, y+1).getRed();
int up_1 = ImageUtil.getColor(image, x, y-1).getRed();
int up_2 = ImageUtil.getColor(image, x+1, y-1).getRed();
int down_1 = ImageUtil.getColor(image, x, y+2).getRed();
int down_2 = ImageUtil.getColor(image, x+1, y+2).getRed();
int left = left_1 == 0 && left_2 == 0 ? black.getRed() : white.getRed();
int right = right_1 == 0 && right_2 == 0 ? black.getRed() : white.getRed();
int up = up_1 == 0 && up_2 == 0 ? black.getRed() : white.getRed();
int down = down_1 == 0 && down_2 == 0 ? black.getRed() : white.getRed();
if(left == right && left == 0 && up == down && up != 0 && self == 0){
//左右为黑 上下为白
setRGB(outImage, x, y, white.getRGB());//自己
}else if(left == right && left != 0 && up == down && up == 0 && self == 0){
//上下为黑 左右为白
setRGB(outImage, x, y, white.getRGB());//自己
}
else if(left == right && up == down && left == up && left != 0 && self == 0){
// //上下左右均为白色
setRGB(outImage, x, y, white.getRGB());//自己
}
else{
int count = 0;
count = left != 0 ? count+1 : count;
count = right != 0 ? count+1 : count;
count = up != 0 ? count+1 : count;
count = down != 0 ? count+1 : count;
//
if(count > 2){
setRGB(outImage, x, y, white.getRGB());//自己
}else{
setRGB(outImage, x, y, black.getRGB());
}
}
}
}
return outImage;
}
/**
* 去除杂点、杂线
* @param image
* @param length
* @return
*/
public static BufferedImage lineFilter(BufferedImage image){
int width = image.getWidth();
int height = image.getHeight();
int minx = image.getMinX();
int miny = image.getMinY();
Color black = new Color(0, 0, 0);
Color white = new Color(255, 255, 255);
BufferedImage outImage = new BufferedImage(width, height, image.getType());
for (int x = minx; x < width; x++) {
for (int y = miny; y < height; y++) {
Color color = new Color(image.getRGB(x, y));
if(color.getRed() == white.getRed()){
outImage.setRGB(x, y, color.getRGB());
continue;
}
//先判断左右
int self = color.getRed();
int left = ImageUtil.getColor(image, x-1, y).getRed();
int right = ImageUtil.getColor(image, x+1, y).getRed();
int up = ImageUtil.getColor(image, x, y-1).getRed();
int down = ImageUtil.getColor(image, x, y+1).getRed();
if(left == right && left == 0 && up == down && up != 0 && self == 0){
//左右为黑 上下为白
setRGB(outImage, x, y, white.getRGB());
}else if(left == right && left != 0 && up == down && up == 0 && self == 0){
setRGB(outImage, x, y, white.getRGB());
}else if(left == right && up == down && left == up && left != 0 && self == 0){
setRGB(outImage, x, y, white.getRGB());
}else{
int count = 0;
count = left != 0 ? count+1 : count;
count = right != 0 ? count+1 : count;
count = up != 0 ? count+1 : count;
count = down != 0 ? count+1 : count;
//
if(count > 2){
setRGB(outImage, x, y, white.getRGB());
}else{
// setRGB(outImage, x, y, black.getRGB());
}
}
}
}
return outImage;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
/**
* Return true if the BufferedImage argument has non-opaque
* bits in it and therefore can not be directly rendered by
* GDI. Return false if the image is opaque. If this function
* can not tell for sure whether the image has transparent
* pixels then it assumes that it does.
*/
protected boolean hasTransparentPixels(BufferedImage bufferedImage) {
ColorModel colorModel = bufferedImage.getColorModel();
boolean hasTransparency = colorModel == null
? true
: colorModel.getTransparency() != ColorModel.OPAQUE;
/*
* For the default INT ARGB check the image to see if any pixels are
* really transparent. If there are no transparent pixels then the
* transparency of the color model can be ignored.
* We assume that IndexColorModel images have already been
* checked for transparency and will be OPAQUE unless they actually
* have transparent pixels present.
*/
if (hasTransparency && bufferedImage != null) {
if (bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB ||
bufferedImage.getType()==BufferedImage.TYPE_INT_ARGB_PRE) {
DataBuffer db = bufferedImage.getRaster().getDataBuffer();
SampleModel sm = bufferedImage.getRaster().getSampleModel();
if (db instanceof DataBufferInt &&
sm instanceof SinglePixelPackedSampleModel) {
SinglePixelPackedSampleModel psm =
(SinglePixelPackedSampleModel)sm;
// Stealing the data array for reading only...
int[] int_data =
SunWritableRaster.stealData((DataBufferInt) db, 0);
int x = bufferedImage.getMinX();
int y = bufferedImage.getMinY();
int w = bufferedImage.getWidth();
int h = bufferedImage.getHeight();
int stride = psm.getScanlineStride();
boolean hastranspixel = false;
for (int j = y; j < y+h; j++) {
int yoff = j * stride;
for (int i = x; i < x+w; i++) {
if ((int_data[yoff+i] & 0xff000000)!=0xff000000 ) {
hastranspixel = true;
break;
}
}
if (hastranspixel) {
break;
}
}
if (hastranspixel == false) {
hasTransparency = false;
}
}
}
}
return hasTransparency;
}
public BufferedImage[] openUnmixedImages(int no, int x, int y, int w, int h) throws FormatException, IOException {
int[] zct = getZCTCoords(no);
int z = zct[0];
int chan = zct[1];
int t = zct[2];
// load all channels for current rect
BufferedImage[] channels = new BufferedImage[sizeC];
for (int c=0; c<sizeC; c++) {
int no2 = getIndex(z,c,t);
channels[c] = super.openImage(no2,x,y,w,h);
}
BufferedImage ori = channels[0];
BufferedImage[] bi = new BufferedImage[sizeC];
WritableRaster[] raster = new WritableRaster[sizeC];
for (int c=0; c<sizeC; c++) {
if (!channelIndependent[c]) {
bi[c] = new BufferedImage(ori.getColorModel(), ori.getRaster().createCompatibleWritableRaster(0, 0, w, h), ori.isAlphaPremultiplied(), null);
raster[c] = bi[c].getRaster();
} else {
bi[c] = channels[c]; // original image, independent channel
raster[c] = bi[c].getRaster();
}
}
double[] measurements = new double[sizeDependend];
double[] out = new double[sizeDependend];
for (int ix=ori.getMinX(); ix<ori.getMinX()+ori.getWidth(); ix++)
for (int iy=ori.getMinY(); iy<ori.getMinY()+ori.getHeight(); iy++) {
for (int c=0; c<sizeDependend; c++) {
measurements[c] = channels[channelMap[c]].getRaster().getSampleDouble(ix,iy,0); // only 1 banded rasters allowed here
}
fastMultiply(invMatrix,measurements,out); // here the real unmixing takes place
for (int c=0; c<sizeDependend; c++) {
if (out[c]>255) out[c] = 255d; // TODO: adjust for 16bit!!!
if (out[c]<0) out[c] = 0d;
raster[channelMap[c]].setSample(ix, iy, 0, out[c]);
}
}
unmixedChannels = bi;
currentRect = new RectZT(x,y,w,h,z,t);
return bi;
}