下面列出了java.awt.image.Raster#getSample() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private IIOMetadataNode gethISTNode(BufferedImage bi) {
IndexColorModel icm = (IndexColorModel)bi.getColorModel();
int mapSize = icm.getMapSize();
int[] hist = new int[mapSize];
Arrays.fill(hist, 0);
Raster r = bi.getData();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int s = r.getSample(x, y, 0);
hist[s] ++;
}
}
IIOMetadataNode hIST = new IIOMetadataNode("hIST");
for (int i = 0; i < hist.length; i++) {
IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
n.setAttribute("index", "" + i);
n.setAttribute("value", "" + hist[i]);
hIST.appendChild(n);
}
return hIST;
}
static double getValue(RasterDataNode raster, int pixelX, int pixelY, int currentLevel) {
final RenderedImage image = raster.getGeophysicalImage().getImage(currentLevel);
final Rectangle pixelRect = new Rectangle(pixelX, pixelY, 1, 1);
final Raster data = image.getData(pixelRect);
final MultiLevelImage validMaskImage = raster.getValidMaskImage();
Raster validMaskData = null;
if (validMaskImage != null) {
final RenderedImage validMask = validMaskImage.getImage(currentLevel);
validMaskData = validMask.getData(pixelRect);
}
final double value;
if (validMaskData == null || validMaskData.getSample(pixelX, pixelY, 0) > 0) {
value = data.getSampleDouble(pixelX, pixelY, 0);
} else {
value = Double.NaN;
}
return value;
}
private IIOMetadataNode gethISTNode(BufferedImage bi) {
IndexColorModel icm = (IndexColorModel)bi.getColorModel();
int mapSize = icm.getMapSize();
int[] hist = new int[mapSize];
Arrays.fill(hist, 0);
Raster r = bi.getData();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int s = r.getSample(x, y, 0);
hist[s] ++;
}
}
IIOMetadataNode hIST = new IIOMetadataNode("hIST");
for (int i = 0; i < hist.length; i++) {
IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
n.setAttribute("index", "" + i);
n.setAttribute("value", "" + hist[i]);
hIST.appendChild(n);
}
return hIST;
}
private IIOMetadataNode gethISTNode(BufferedImage bi) {
IndexColorModel icm = (IndexColorModel)bi.getColorModel();
int mapSize = icm.getMapSize();
int[] hist = new int[mapSize];
Arrays.fill(hist, 0);
Raster r = bi.getData();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int s = r.getSample(x, y, 0);
hist[s] ++;
}
}
IIOMetadataNode hIST = new IIOMetadataNode("hIST");
for (int i = 0; i < hist.length; i++) {
IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
n.setAttribute("index", "" + i);
n.setAttribute("value", "" + hist[i]);
hIST.appendChild(n);
}
return hIST;
}
private IIOMetadataNode gethISTNode(BufferedImage bi) {
IndexColorModel icm = (IndexColorModel)bi.getColorModel();
int mapSize = icm.getMapSize();
int[] hist = new int[mapSize];
Arrays.fill(hist, 0);
Raster r = bi.getData();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int s = r.getSample(x, y, 0);
hist[s] ++;
}
}
IIOMetadataNode hIST = new IIOMetadataNode("hIST");
for (int i = 0; i < hist.length; i++) {
IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
n.setAttribute("index", "" + i);
n.setAttribute("value", "" + hist[i]);
hIST.appendChild(n);
}
return hIST;
}
protected int[][] toIntArrayArray(BufferedImage image) {
int w = image.getWidth(), h = image.getHeight();
int[][] ret = new int[h][w];
if (image.getRaster().getNumDataElements() == 1) {
Raster raster = image.getRaster();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ret[i][j] = raster.getSample(j, i, 0);
}
}
} else {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ret[i][j] = image.getRGB(j, i);
}
}
}
return ret;
}
private static BufferedImage combineMasks(BufferedImage m1, BufferedImage m2, int dx, int dy) {
BufferedImage combined = new BufferedImage(m1.getWidth(),m1.getHeight(),m1.getType());
combined.getGraphics().drawImage(m1,0,0,null);
int fg = Color.white.getRGB();
Raster r2 = m2.getRaster();
int minx = -dx<0?0:-dx;
int miny = -dy<0?0:-dy;
int maxx = m2.getWidth()-dx<m2.getWidth()?m2.getWidth()-dx:m2.getWidth();
int maxy = m2.getHeight()-dy<m2.getHeight()?m2.getHeight()-dy:m2.getHeight();
for (int x=minx; x<maxx; x++)
for (int y=miny; y<maxy; y++) {
if (r2.getSample(x,y,0)>0) {
combined.setRGB(x+dx,y+dy,fg);
}
}
return combined;
}
protected int[][] toIntArrayArray(BufferedImage image) {
int w = image.getWidth(), h = image.getHeight();
int[][] ret = new int[h][w];
if (image.getRaster().getNumDataElements() == 1) {
Raster raster = image.getRaster();
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ret[i][j] = raster.getSample(j, i, 0);
}
}
} else {
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
ret[i][j] = image.getRGB(j, i);
}
}
}
return ret;
}
private IIOMetadataNode gethISTNode(BufferedImage bi) {
IndexColorModel icm = (IndexColorModel)bi.getColorModel();
int mapSize = icm.getMapSize();
int[] hist = new int[mapSize];
Arrays.fill(hist, 0);
Raster r = bi.getData();
for (int y = 0; y < bi.getHeight(); y++) {
for (int x = 0; x < bi.getWidth(); x++) {
int s = r.getSample(x, y, 0);
hist[s] ++;
}
}
IIOMetadataNode hIST = new IIOMetadataNode("hIST");
for (int i = 0; i < hist.length; i++) {
IIOMetadataNode n = new IIOMetadataNode("hISTEntry");
n.setAttribute("index", "" + i);
n.setAttribute("value", "" + hist[i]);
hIST.appendChild(n);
}
return hIST;
}
public int[][] fromFile(File file) throws IOException {
BufferedImage image = ImageIO.read(file);
if (height > 0 && width > 0)
image = toBufferedImage(image.getScaledInstance(height, width, Image.SCALE_SMOOTH));
Raster raster = image.getData();
int w = raster.getWidth(), h = raster.getHeight();
int[][] ret = new int[w][h];
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
ret[i][j] = raster.getSample(i, j, 0);
return ret;
}
protected int getAverageBrightness( final BufferedImage img ) {
final Raster r = img.getData();
int total = 0;
for ( int y = 0; y < r.getHeight(); y++ ) {
for ( int x = 0; x < r.getWidth(); x++ ) {
total += r.getSample( r.getMinX() + x, r.getMinY() + y, 0 );
}
}
return (int) (total / (r.getWidth() / STABILIZER * (r.getHeight() / STABILIZER)));
}
private boolean isPixelValid(RasterDataNode raster, int pixelX, int pixelY, int level) {
if (raster.isValidMaskUsed()) {
PlanarImage image = ImageManager.getInstance().getValidMaskImage(raster, level);
Raster data = getRasterTile(image, pixelX, pixelY);
return data.getSample(pixelX, pixelY, 0) != 0;
} else {
return true;
}
}
private boolean isPixelValid(RasterDataNode raster, int pixelX, int pixelY, int level) {
if (raster.isValidMaskUsed()) {
PlanarImage image = ImageManager.getInstance().getValidMaskImage(raster, level);
Raster data = getRasterTile(image, pixelX, pixelY);
return data.getSample(pixelX, pixelY, 0) != 0;
} else {
return true;
}
}
/**
* Encodes and writes raster data as a 24-bit bitmap.
* @param raster the source raster data
* @param out the output to which the bitmap will be written
* @throws java.io.IOException if an error occurs
*/
public static void write24(Raster raster, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
int width = raster.getWidth();
int height = raster.getHeight();
// calculate bytes per line
int bytesPerLine = getBytesPerLine24(width);
// write lines
for (int y = height - 1; y >= 0; y--) {
// write pixel data for each line
for (int x = 0; x < width; x++) {
// get RGB values for pixel
int r = raster.getSample(x, y, 0);
int g = raster.getSample(x, y, 1);
int b = raster.getSample(x, y, 2);
// write RGB values
out.writeByte(b);
out.writeByte(g);
out.writeByte(r);
}
// write padding bytes at end of line
for (int i = width * 3; i < bytesPerLine; i++) {
out.writeByte(0);
}
}
}
public List<Shape> detectSegmentations(int minSegmentationSize, PlanarImage sourceImage) {
logger.trace("getSegmentations (minSegmentation=" + minSegmentationSize + ")");
rf.initializeClassColors();
//if (rf.getNegativeChannel() != null) rf.getNegativeChannel().initializeClassColors();
if (sourceImage == null) return new ArrayList<Shape>(0);
short[][] smap = new short[sourceImage.getWidth()][sourceImage.getHeight()];
for (int x = 0; x < sourceImage.getWidth(); x++)
for (int y = 0; y < sourceImage.getHeight(); y++)
smap[x][y] = Short.MAX_VALUE;
// init
Point[] tileArr = sourceImage.getTileIndices(null);
int c;
for (Point tileNum : tileArr) {
if (isCancelled()) break;
final int b = 2;
Raster raster = sourceImage.getTile(tileNum.x, tileNum.y);
for (int x = sourceImage.tileXToX(tileNum.x) + b; x < Math.min(sourceImage.tileXToX(tileNum.x) + sourceImage.getTileWidth() - b, sourceImage.getWidth()); x++) {
for (int y = sourceImage.tileYToY(tileNum.y) + b; y < Math.min(sourceImage.tileYToY(tileNum.y) + sourceImage.getTileHeight() - b, sourceImage.getHeight()); y++) {
if (fullRoi != null && !(fullRoi.contains(x + roi.getBounds().x, y + roi.getBounds().y))) {
continue;
}
c = raster.getSample(x, y, 0) < 200 ? 0 : 1; // dark=foreground, white=background (only red channel is taken into account)
if (c == 0) { // not background, not assigned
smap[x][y] = 1; // 1
} else {
smap[x][y] = 0; // 0
}
} //y
} // x
} // tileNum
if (isCancelled()) return null;
return findPolygons(smap, minSegmentationSize);
}
public List<Shape> detectSegmentations(int minSegmentationSize, PlanarImage sourceImage, Shape roi) {
logger.trace("getSegmentations (minSegmentation=" + minSegmentationSize + ")");
if (sourceImage == null) return new ArrayList<Shape>(0);
short[][] smap = new short[sourceImage.getWidth()][sourceImage.getHeight()];
for (int x = 0; x < sourceImage.getWidth(); x++)
for (int y = 0; y < sourceImage.getHeight(); y++)
smap[x][y] = Short.MAX_VALUE;
// init
Point[] tileArr = sourceImage.getTileIndices(null);
int c;
for (Point tileNum : tileArr) {
if (isCancelled()) break;
final int b = 2;
Raster raster = sourceImage.getTile(tileNum.x, tileNum.y);
for (int x = sourceImage.tileXToX(tileNum.x) + b; x < Math.min(sourceImage.tileXToX(tileNum.x) + sourceImage.getTileWidth() - b, sourceImage.getWidth()); x++) {
for (int y = sourceImage.tileYToY(tileNum.y) + b; y < Math.min(sourceImage.tileYToY(tileNum.y) + sourceImage.getTileHeight() - b, sourceImage.getHeight()); y++) {
if (fullRoi != null && !(fullRoi.contains(x + roi.getBounds().x, y + roi.getBounds().y))) {
continue;
}
c = raster.getSample(x, y, 0) < 200 ? 0 : 1; // dark=foreground, white=background (only red channel is taken into account)
if (c == 0) { // not background, not assigned
smap[x][y] = 1; // 1
} else {
smap[x][y] = 0; // 0
}
} //y
} // x
} // tileNum
if (isCancelled()) return null;
return findPolygons(smap, orderPoints, minSegmentationSize, roi);
}
/**
* Encodes and writes raster data as an 8-bit bitmap.
* @param raster the source raster data
* @param out the output to which the bitmap will be written
* @throws java.io.IOException if an error occurs
*/
public static void write8(Raster raster, net.sf.image4j.io.LittleEndianOutputStream out) throws IOException {
int width = raster.getWidth();
int height = raster.getHeight();
// calculate bytes per line
int bytesPerLine = getBytesPerLine8(width);
// write lines
for (int y = height - 1; y >= 0; y--) {
// write raster data for each line
for (int x = 0; x < width; x++) {
// get color index for pixel
int index = raster.getSample(x, y, 0);
out.writeByte(index);
}
// write padding bytes at end of line
for (int i = width; i < bytesPerLine; i++) {
out.writeByte(0);
}
}
}
private static long getNumMaskPixels(final RenderedImage maskImage, int sceneRasterWidth, int sceneRasterHeight) {
final int minTileX = maskImage.getMinTileX();
final int minTileY = maskImage.getMinTileY();
final int numXTiles = maskImage.getNumXTiles();
final int numYTiles = maskImage.getNumYTiles();
final Rectangle imageRect = new Rectangle(0, 0, sceneRasterWidth, sceneRasterHeight);
long numMaskPixels = 0;
for (int tileX = minTileX; tileX < minTileX + numXTiles; ++tileX) {
for (int tileY = minTileY; tileY < minTileY + numYTiles; ++tileY) {
final Rectangle tileRectangle = new Rectangle(maskImage.getTileGridXOffset() + tileX * maskImage.getTileWidth(),
maskImage.getTileGridYOffset() + tileY * maskImage.getTileHeight(),
maskImage.getTileWidth(), maskImage.getTileHeight());
final Rectangle r = imageRect.intersection(tileRectangle);
if (!r.isEmpty()) {
Raster maskTile = maskImage.getTile(tileX, tileY);
for (int y = r.y; y < r.y + r.height; y++) {
for (int x = r.x; x < r.x + r.width; x++) {
if (maskTile.getSample(x, y, 0) != 0) {
numMaskPixels++;
}
}
}
}
}
}
return numMaskPixels;
}
int getBlue (Raster raster, int x,int y)
{
return raster.getSample(x, y, 2);
}
int getRed (Raster raster, int x,int y)
{
return raster.getSample(x, y, 0);
}