List of usage examples for java.awt.image BufferedImage getRGB
public int getRGB(int x, int y)
From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java
public static Map<Integer, List<PathObject>> objectClusterer(final Collection<PathObject> pathObjects, final BufferedImage imgThumbnail, final double thumbScaleX, final double thumbScaleY, final int nClusters) { Map<Integer, List<PathObject>> map = new HashMap<>(); if (pathObjects.isEmpty()) return map; if (nClusters <= 1 || pathObjects.size() == 1) { map.put(Integer.valueOf(0), new ArrayList<>(pathObjects)); return map; }/*w w w. j a v a 2s .c om*/ // int maxIterations = 100; KMeansPlusPlusClusterer<ClusterableObject> km = new KMeansPlusPlusClusterer<>(nClusters); List<ClusterableObject> clusterableObjects = new ArrayList<>(); WritableRaster raster = imgThumbnail.getRaster(); int nChannels = raster.getNumBands(); double[] valueBuffer = new double[nChannels]; int w = imgThumbnail.getWidth(); int h = imgThumbnail.getHeight(); boolean isRGB = imgThumbnail.getSampleModel().getNumBands() == 3 && imgThumbnail.getSampleModel().getSampleSize(0) == 8; for (PathObject pathObject : pathObjects) { // Get pixel values for the ROI centroid // CIE LAB is used rather than RGB where possible, due to better suitability for Euclidean distances ROI roi = pathObject.getROI(); if (roi == null) continue; int x = (int) (roi.getCentroidX() * thumbScaleX + 0.5); int y = (int) (roi.getCentroidY() * thumbScaleY + 0.5); if (x < 0 || x >= w || y < 0 || y >= h) continue; if (isRGB) valueBuffer = makeCIELAB(imgThumbnail.getRGB(x, y), valueBuffer); else { for (int c = 0; c < nChannels; c++) valueBuffer[c] = raster.getSampleDouble(x, y, c); } clusterableObjects.add(new ClusterableObject(pathObject, valueBuffer)); } List<CentroidCluster<ClusterableObject>> results = km.cluster(clusterableObjects); int i = 0; for (CentroidCluster<ClusterableObject> centroidCluster : results) { Integer label = Integer.valueOf(i); List<PathObject> objects = new ArrayList<>(); for (ClusterableObject co : centroidCluster.getPoints()) objects.add(co.getPathObject()); map.put(label, objects); i++; } return map; }
From source file:cpsd.ImageGUI.java
private void ratioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ratioButtonActionPerformed Mat source = ImageClass.getInstance().getImage(); KMeans kmeans = new KMeans(); BufferedImage src = kmeans.matToBuff(source); double[] bins = new double[256]; for (int i = 0; i < 256; i++) bins[i] = -1;//from www .j a va 2 s. com int number = 500; for (int i = 0; i < src.getWidth(); i++) { for (int j = 0; j < src.getHeight(); j++) { // int p = src.getData().getSample(i,j,0); int p = src.getRGB(i, j) & 0xFF; //System.out.println(p); (bins[p])++; //System.out.println(bins[p]++); } } /*HistogramDataset dataset = new HistogramDataset(); dataset.setType(HistogramType.FREQUENCY); try{ dataset.addSeries("Histogram2",bins,number,0,300); }catch(Exception e){ e.printStackTrace(); } String plotTitle = "Coal & Ash Analysis"; String xAxis = "pixel intensity"; String yAxis = "pixel count"; PlotOrientation orientation = PlotOrientation.VERTICAL; boolean show = true; boolean toolTips = true; boolean urls = false; JFreeChart chart = ChartFactory.createHistogram(plotTitle,xAxis,yAxis,dataset,orientation,show,toolTips,urls); int width = 800; int height = 600; ChartFrame frame1 = new ChartFrame("Coal & ash Analysis",chart); frame1.setSize(width,height); frame1.setVisible(true); frame1.setDefaultCloseOperation(DISPOSE_ON_CLOSE);*/ // TODO add your handling code here: }
From source file:com.simiacryptus.mindseye.lang.Tensor.java
/** * From rgb tensor.// ww w . j a v a 2 s . c om * * @param img the img * @return the tensor */ @Nonnull public static Tensor fromRGB(@Nonnull final BufferedImage img) { final int width = img.getWidth(); final int height = img.getHeight(); @Nonnull final Tensor a = new Tensor(width, height, 3); IntStream.range(0, width).parallel().forEach(x -> { @Nonnull final int[] coords = { 0, 0, 0 }; IntStream.range(0, height).forEach(y -> { coords[0] = x; coords[1] = y; coords[2] = 0; a.set(coords, img.getRGB(x, y) & 0xFF); coords[2] = 1; a.set(coords, img.getRGB(x, y) >> 8 & 0xFF); coords[2] = 2; a.set(coords, img.getRGB(x, y) >> 16 & 0x0FF); }); }); return a; }
From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java
public static BufferedImage compensateLensDistortion(BufferedImage img, double k) { if (img == null) { return null; }//from ww w .j a va2 s .co m int w = img.getWidth(); int h = img.getHeight(); BufferedImage out = new BufferedImage(w, h, img.getType()); int x0 = (int) Math.floor(w / 2) + 1; int y0 = (int) Math.floor(h / 2) + 1; double ru, theta, ww, rd; int xd, yd; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { ru = Math.sqrt((x - x0) * (x - x0) + (y - y0) * (y - y0)); theta = Math.atan2(y - y0, x - x0); ww = Math.pow(ru / (2 * k) + Math.sqrt((ru * ru) / (4 * k * k) + 1 / (27 * k * k * k)), 1.0 / 3.0); rd = ww - 1 / (3 * k * ww); //nearest neighbour--------------------------------------- xd = (int) Math.round(rd * Math.cos(theta)) + x0; yd = (int) Math.round(rd * Math.sin(theta)) + y0; if (xd >= 0 && yd >= 0 && xd < w && yd < h) { //piksel nowy x,y = piksel stary xd,yd out.setRGB(x, y, img.getRGB(xd, yd)); } //--------------------------------------------------------- } } return out; }
From source file:io.gameover.utilities.pixeleditor.Pixelizer.java
private void openImage(BufferedImage image) { int nb = image.getWidth() / Frame.NB_PIXELS; if (image.getWidth() % Frame.NB_PIXELS != 0) { throw new IllegalArgumentException("image width is not a multiple of " + Frame.NB_PIXELS); }//w ww .j av a 2 s .c om this.frames = new ArrayList<>(nb); this.currentFrameIndex = 0; for (int f = 0; f < nb; f++) { Frame frame = new Frame(); for (int x = 0; x < Frame.NB_PIXELS; x++) { for (int y = 0; y < Frame.NB_PIXELS; y++) { int ix = x + f * Frame.NB_PIXELS; int iy = y; int c = image.getRGB(ix, iy); frame.setColor(x, y, c); } } this.frames.add(frame); } fillPanelWithButton(nb); }
From source file:de.blinkenlights.bmix.movie.BMLOutputStream.java
/** * Writes a new frame to the stream. /*from ww w . j av a 2 s. c o m*/ * @param image the BLImage containing the frame to write. * * @throws IllegalArgumentException if the frame is the incorrect dimensions for the movie. */ public void writeFrame(BufferedImage image) throws IOException { BufferedImage outputImage; if (autoFps) { if (previousFrame == null) { // first frame, just buffer this one and return previousFrame = image; lastFrameTime = System.currentTimeMillis(); return; } outputImage = previousFrame; } else { outputImage = image; } if (closed) { throw new IllegalStateException("attempt to write to closed stream"); } if (outputImage.getHeight() != size.height || outputImage.getWidth() != size.width) { throw new IllegalArgumentException("Image was incorrect size for movie; expected (" + size.width + "x" + size.height + ") but got (" + outputImage.getWidth() + "x" + outputImage.getHeight() + ")"); } StringBuilder outStr = new StringBuilder(); if (autoFps) { long duration = System.currentTimeMillis() - lastFrameTime; outStr.append("<frame duration=\"" + duration + "\">\n"); } else { outStr.append("<frame duration=\"" + millisPerFrame + "\">\n"); } for (int i = 0; i < outputImage.getHeight(); i++) { outStr.append("<row>"); for (int j = 0; j < outputImage.getWidth(); j++) { // keep the red channel only int r = (outputImage.getRGB(j, i) & 0x00ff0000) >> 16; if (bpp == 4) { r = r >> 4; outStr.append(String.format("%x", r)); } else if (bpp == 8) { outStr.append(String.format("%02x", r)); } } outStr.append("</row>\n"); } outStr.append("</frame>\n"); byte[] bytes = outStr.toString().getBytes(); out.write(bytes, 0, bytes.length); if (autoFps) { previousFrame = image; } lastFrameTime = System.currentTimeMillis(); }
From source file:com.chtr.tmoauto.webui.CommonFunctions.java
/** * //from w w w . j a va2 s . co m * @param image * @param xStart * @param yStart * @param width * @param height * @return */ private static double[] computeLuminance(BufferedImage image, int xStart, int yStart, int width, int height) { checkBounds(image, xStart, yStart, width, height); double[] luminance = new double[width * height]; for (int y = yStart; y < height; y++) { for (int x = xStart; x < width; x++) { int pixel = image.getRGB(x, y); int red = (pixel >> 16) & BYTE_MASK; int green = (pixel >> 8) & BYTE_MASK; int blue = (pixel >> 0) & BYTE_MASK; luminance[x * y] = RED_RATIO * red + GREEN_RATIO * green + BLUE_RATIO * blue; } } return luminance; }
From source file:cn.z.Ocr5.java
public static String getSingleCharOcr(BufferedImage img, Map<BufferedImage, String> map) throws Exception { if (useSvm) { final String input = new File("img/" + clazz + "/input.txt").getAbsolutePath(); final String output = new File("result/" + clazz + "/output.txt").getAbsolutePath(); CommonUtil.imgToSvmInput(img, input, whiteThreshold); svm_predict.main(new String[] { input, new File("train/" + clazz + "/data.txt.model").getAbsolutePath(), output });//w ww. ja va2 s .c o m final List<String> predict = IOUtils.readLines(new FileInputStream(output)); if (predict.size() > 0 && predict.get(0).length() > 0) { return predict.get(0).substring(0, 1); } return "#"; } String result = "#"; img = scaleImage(img); final int width = img.getWidth(); final int height = img.getHeight(); int min = width * height; final boolean bNotEight = isNotEight(img); final boolean bNotThree = isNotThree(img); final boolean bNotFive = isNotFive(img); for (final BufferedImage bi : map.keySet()) { if (bNotThree && map.get(bi).startsWith("3")) { continue; } if (bNotEight && map.get(bi).startsWith("8")) { continue; } if (bNotFive && map.get(bi).startsWith("5")) { continue; } final double count1 = getBlackCount(img); final double count2 = getBlackCount(bi); if (Math.abs(count1 - count2) / Math.max(count1, count2) > 0.25) { continue; } int count = 0; if (width < bi.getWidth() && height < bi.getHeight()) { for (int m = 0; m <= bi.getWidth() - width; m++) { for (int n = 0; n <= bi.getHeight() - height; n++) { Label1: for (int x = m; x < m + width; ++x) { for (int y = n; y < n + height; ++y) { if (CommonUtil.isWhite(img.getRGB(x - m, y - n), whiteThreshold) != CommonUtil .isWhite(bi.getRGB(x, y), whiteThreshold)) { count++; if (count >= min) { break Label1; } } } } } } } else { final int widthmin = width < bi.getWidth() ? width : bi.getWidth(); final int heightmin = height < bi.getHeight() ? height : bi.getHeight(); Label1: for (int x = 0; x < widthmin; ++x) { for (int y = 0; y < heightmin; ++y) { if (CommonUtil.isWhite(img.getRGB(x, y), whiteThreshold) != CommonUtil .isWhite(bi.getRGB(x, y), whiteThreshold)) { count++; if (count >= min) { break Label1; } } } } } if (count < min) { min = count; result = map.get(bi); } } return result; }
From source file:io.gameover.utilities.pixeleditor.Pixelizer.java
public void convertToPixelImage(BufferedImage image) { if (image != null) { actionClearChange();//w ww . j av a2s .c om int nbImgPixelPerHPixel = image.getWidth() / Frame.NB_PIXELS; int nbImgPixelPerVPixel = image.getHeight() / Frame.NB_PIXELS; for (int h = 0; h < Frame.NB_PIXELS; h++) { for (int v = 0; v < Frame.NB_PIXELS; v++) { int count = 0; long sumA = 0, sumR = 0, sumG = 0, sumB = 0; for (int x = h * nbImgPixelPerHPixel; x < (h + 1) * nbImgPixelPerHPixel; x++) { for (int y = v * nbImgPixelPerVPixel; y < (v + 1) * nbImgPixelPerVPixel; y++) { int p = image.getRGB(x, y); int[] argb = ColorUtils.extractARGB(p); sumA += argb[0]; sumR += argb[1]; sumG += argb[2]; sumB += argb[3]; count++; } } int c = ColorUtils.convertToColorAsInt((int) (sumA / count), (int) (sumR / count), (int) (sumG / count), (int) (sumB / count)); getCurrentFrame().setColor(h, v, c); } } } }
From source file:org.finra.jtaf.ewd.widget.element.Element.java
private boolean isScreenshotBlack(BufferedImage var) { double[] varArr = new double[var.getWidth() * var.getHeight() * 3]; //unroll pixels for (int i = 0; i < var.getHeight(); i++) { for (int j = 0; j < var.getWidth(); j++) { varArr[i * var.getWidth() + j + 0] = new Color(var.getRGB(j, i)).getRed(); varArr[i * var.getWidth() + j + 1] = new Color(var.getRGB(j, i)).getGreen(); varArr[i * var.getWidth() + j + 2] = new Color(var.getRGB(j, i)).getBlue(); }/*from www.j av a 2s . c om*/ } //test if all are black for (int i = 0; i != varArr.length; i++) { if (varArr[i] != 0) return false; } return true; }