/** * Splits the specified RGB stack into three 8-bit grayscale stacks. Deletes the source stack if * keepSource is false. */ public static ImageStack[] splitRGB(ImageStack rgb, boolean keepSource) { int w = rgb.getWidth(); int h = rgb.getHeight(); ImageStack[] channels = new ImageStack[3]; for (int i = 0; i < 3; i++) channels[i] = new ImageStack(w, h); byte[] r, g, b; ColorProcessor cp; int slice = 1; int inc = keepSource ? 1 : 0; int n = rgb.getSize(); for (int i = 1; i <= n; i++) { IJ.showStatus(i + "/" + n); r = new byte[w * h]; g = new byte[w * h]; b = new byte[w * h]; cp = (ColorProcessor) rgb.getProcessor(slice); slice += inc; cp.getRGB(r, g, b); if (!keepSource) rgb.deleteSlice(1); channels[0].addSlice(null, r); channels[1].addSlice(null, g); channels[2].addSlice(null, b); IJ.showProgress((double) i / n); } return channels; }
public static double[] getPixels(ImagePlus imp, int[] slices, int[] frames, int[] channels) { ImagePlus subImp = AccessImage.getSubHyperStack(imp, slices, frames, channels); ImageStack imageStack = subImp.getImageStack(); double[] pixels = new double[subImp.getWidth() * subImp.getHeight() * imageStack.getSize()]; for (int i = 0; i < imageStack.getSize(); i++) { switch (subImp.getType()) { case ImagePlus.GRAY8: ByteProcessor byteProcessor = (ByteProcessor) imageStack.getProcessor(i + 1); for (int iX = 0; iX < subImp.getWidth(); iX++) { for (int iY = 0; iY < subImp.getHeight(); iY++) { pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] = (double) byteProcessor.getPixelValue(iX, iY); } } break; case ImagePlus.GRAY16: ShortProcessor shortProcessor = (ShortProcessor) imageStack.getProcessor(i + 1); for (int iX = 0; iX < subImp.getWidth(); iX++) { for (int iY = 0; iY < subImp.getHeight(); iY++) { pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] = (double) shortProcessor.getPixelValue(iX, iY); } } break; case ImagePlus.GRAY32: FloatProcessor floatProcessor = (FloatProcessor) imageStack.getProcessor(i + 1); for (int iX = 0; iX < subImp.getWidth(); iX++) { for (int iY = 0; iY < subImp.getHeight(); iY++) { pixels[i * subImp.getWidth() * subImp.getHeight() + iY * subImp.getWidth() + iX] = (double) floatProcessor.getPixelValue(iX, iY); } } break; case ImagePlus.COLOR_RGB: ColorProcessor colorProcessor = (ColorProcessor) imageStack.getProcessor(i + 1); int nX = subImp.getWidth(); int nY = subImp.getHeight(); byte[] red = new byte[nX * nY]; byte[] green = new byte[nX * nY]; byte[] blue = new byte[nX * nY]; colorProcessor.getRGB(red, green, blue); int r, g, b; for (int j = 0; j < nX * nY; j++) { r = red[j] << 16; g = green[j] << 8; b = blue[j] << 0; pixels[i * nX * nY + j] = (double) (r + g + b); } break; } } return (pixels); }
ImageStack getRGBStack(ImagePlus imp) { ImageProcessor ip = imp.getProcessor(); int w = ip.getWidth(); int h = ip.getHeight(); int size = w * h; byte[] r = new byte[size]; byte[] g = new byte[size]; byte[] b = new byte[size]; ((ColorProcessor) ip).getRGB(r, g, b); ImageStack stack = new ImageStack(w, h); stack.addSlice("Red", r); stack.addSlice("Green", g); stack.addSlice("Blue", b); stack.setColorModel(ip.getDefaultColorModel()); return stack; }
@Override public BooleanImage getBinaryImage(ImageProcessor ip) { if (th1 == null || !isSupported(ip)) return new BooleanImage(ip.getWidth(), ip.getHeight()); int width = ip.getWidth(); int height = ip.getHeight(); byte[] ch1, ch2, ch3; ch1 = new byte[ip.getWidth() * ip.getHeight()]; ch2 = new byte[ip.getWidth() * ip.getHeight()]; ch3 = new byte[ip.getWidth() * ip.getHeight()]; ColorProcessor cp = (ColorProcessor) ip; switch (colorspace) { case RGB: cp.getRGB(ch1, ch2, ch3); break; case HSB: cp.getHSB(ch1, ch2, ch3); break; case Lab: getLab(cp, ch1, ch2, ch3); break; case YUV: getYUV(cp, ch1, ch2, ch3); break; default: break; } boolean[] pix = new boolean[width * height]; for (int i = 0; i < ch1.length; i++) { boolean c1 = th1[0] <= (ch1[i] & 0xff) && (ch1[i] & 0xff) <= th1[1]; if (pass1 ? !c1 : c1) continue; boolean c2 = th2[0] <= (ch2[i] & 0xff) && (ch2[i] & 0xff) <= th2[1]; if (pass2 ? !c2 : c2) continue; boolean c3 = th3[0] <= (ch3[i] & 0xff) && (ch3[i] & 0xff) <= th3[1]; if (pass3 ? !c3 : c3) continue; pix[i] = true; } pix = applyMask(pix); return new BooleanImage(width, height, pix); }
// ----------------------------------------------------------------------------------- public void Lipschitz2D(ImageProcessor ip) { int slope, slope1, p, p1, p2, p3, p4, maxz; m_roi = ip.getRoi(); ImageHeight = ip.getHeight(); ImageWidth = ip.getWidth(); m_channels = ip instanceof ColorProcessor ? 3 : 1; m_short = ip instanceof ShortProcessor; pixel = new int[m_channels]; int[][] destPixels = new int[m_channels][ImageHeight * ImageWidth]; int[][] srcPixels = new int[m_channels][ImageHeight * ImageWidth]; byte[][] tmpBytePixels = new byte[m_channels][ImageHeight * ImageWidth]; short[][] tmpShortPixels = new short[m_channels][ImageHeight * ImageWidth]; if (m_channels == 1) { if (m_short) { tmpShortPixels[0] = (short[]) ip.getPixels(); } else { tmpBytePixels[0] = (byte[]) ip.getPixels(); } } else { ColorProcessor cip = (ColorProcessor) ip; cip.getRGB(tmpBytePixels[0], tmpBytePixels[1], tmpBytePixels[2]); } int sign = (m_Down ? 1 : -1); int topdown = (m_Down ? 0 : 255); for (int ii = 0; ii < m_channels; ii++) { for (int ij = 0; ij < ImageHeight * ImageWidth; ij++) { srcPixels[ii][ij] = (m_short ? sign * (tmpShortPixels[ii][ij] & 0xffff) : sign * (tmpBytePixels[ii][ij] & 0xff)); destPixels[ii][ij] = srcPixels[ii][ij]; } } slope = (int) (m_Slope); slope1 = (int) (slope * Math.sqrt(2.0)); maxz = m_channels; for (int y = m_roi.y; y < m_roi.y + m_roi.height; y++) // rows { IJ.showProgress(y, 2 * ImageHeight); for (int z = 0; z < m_channels; z++) { p2 = sign * (topdown + (sign) * slope); p3 = sign * (topdown + (sign) * slope1); for (int x = m_roi.x; x < m_roi.x + m_roi.width; x++) // columns { p = (p2 - slope); p1 = (p3 - slope1); if (p1 > p) p = p1; p3 = destPixels[z][x + ImageWidth * (Math.max(y - 1, 0))]; p1 = p3 - slope; if (p1 > p) p = p1; p4 = destPixels[z][Math.min(x + 1, ImageWidth - 1) + ImageWidth * (Math.max(y - 1, 0))]; p1 = p4 - slope1; if (p1 > p) p = p1; p2 = srcPixels[z][x + ImageWidth * y]; if (p > p2) { destPixels[z][x + ImageWidth * y] = p; p2 = p; } } } } for (int y = m_roi.y + m_roi.height - 1; y >= m_roi.y; y--) // rows { IJ.showProgress(2 * ImageHeight - y - 1, 2 * ImageHeight); for (int z = 0; z < maxz; z++) { p2 = sign * (topdown + (sign) * slope); p3 = sign * (topdown + (sign) * slope1); for (int x = m_roi.x + m_roi.width - 1; x >= m_roi.x; x--) // columns { p = (p2 - slope); p1 = (p3 - slope1); if (p1 > p) p = p1; p3 = destPixels[z][x + ImageWidth * (Math.min(y + 1, ImageHeight - 1))]; p1 = p3 - slope; if (p1 > p) p = p1; p4 = destPixels[z][Math.max(x - 1, 0) + ImageWidth * (Math.min(y + 1, ImageHeight - 1))]; p1 = p4 - slope1; if (p1 > p) p = p1; p2 = destPixels[z][x + ImageWidth * y]; if (p > p2) { destPixels[z][x + ImageWidth * y] = p; p2 = p; } } } } for (int ii = 0; ii < m_channels; ii++) { for (int ij = 0; ij < ImageHeight * ImageWidth; ij++) { if (m_TopHat) { tmpBytePixels[ii][ij] = (m_Down ? (byte) (srcPixels[ii][ij] - destPixels[ii][ij] + 255) : (byte) (destPixels[ii][ij] - srcPixels[ii][ij])); } else { if (m_short) { tmpShortPixels[ii][ij] = (short) ((sign * destPixels[ii][ij] & 0xffff)); } else { tmpBytePixels[ii][ij] = (byte) (sign * destPixels[ii][ij]); } } } } if (m_channels == 1) { if (m_short) { ShortProcessor sip = (ShortProcessor) ip; sip.setPixels(tmpShortPixels[0]); } else { ByteProcessor bip = (ByteProcessor) ip; bip.setPixels(tmpBytePixels[0]); } } else { ColorProcessor cip = (ColorProcessor) ip; cip.setRGB(tmpBytePixels[0], tmpBytePixels[1], tmpBytePixels[2]); } }
ImageProcessor setup(ImagePlus imp) { ImageProcessor ip; int type = imp.getType(); if (type != ImagePlus.COLOR_RGB) return null; ip = imp.getProcessor(); int id = imp.getID(); int slice = imp.getCurrentSlice(); if ((id != previousImageID) | (slice != previousSlice) | (flag)) { flag = false; // if true, flags a change from HSB to RGB or viceversa numSlices = imp.getStackSize(); stack = imp.getStack(); width = stack.getWidth(); height = stack.getHeight(); numPixels = width * height; hSource = new byte[numPixels]; sSource = new byte[numPixels]; bSource = new byte[numPixels]; // restore = (int[])ip.getPixelsCopy(); //This runs into trouble sometimes, so do it the // long way: int[] temp = (int[]) ip.getPixels(); restore = new int[numPixels]; for (int i = 0; i < numPixels; i++) restore[i] = temp[i]; fillMask = new int[numPixels]; // Get hsb or rgb from image. ColorProcessor cp = (ColorProcessor) ip; IJ.showStatus("Gathering data"); if (isRGB) cp.getRGB(hSource, sSource, bSource); else cp.getHSB(hSource, sSource, bSource); IJ.showStatus("done"); // Create a spectrum ColorModel for the Hue histogram plot. Color c; byte[] reds = new byte[256]; byte[] greens = new byte[256]; byte[] blues = new byte[256]; for (int i = 0; i < 256; i++) { c = Color.getHSBColor(i / 255f, 1f, 1f); reds[i] = (byte) c.getRed(); greens[i] = (byte) c.getGreen(); blues[i] = (byte) c.getBlue(); } ColorModel cm = new IndexColorModel(8, 256, reds, greens, blues); // Make an image with just the hue from the RGB image and the spectrum LUT. // This is just for a hue histogram for the plot. Do not show it. // ByteProcessor bpHue = new ByteProcessor(width,height,h,cm); ByteProcessor bpHue = new ByteProcessor(width, height, hSource, cm); ImagePlus impHue = new ImagePlus("Hue", bpHue); // impHue.show(); ByteProcessor bpSat = new ByteProcessor(width, height, sSource, cm); ImagePlus impSat = new ImagePlus("Sat", bpSat); // impSat.show(); ByteProcessor bpBri = new ByteProcessor(width, height, bSource, cm); ImagePlus impBri = new ImagePlus("Bri", bpBri); // impBri.show(); plot.setHistogram(impHue, 0); splot.setHistogram(impSat, 1); bplot.setHistogram(impBri, 2); updateLabels(); updatePlot(); updateScrollBars(); imp.updateAndDraw(); } previousImageID = id; previousSlice = slice; return ip; }
void sample() { byte[] hsSource, ssSource, bsSource; // ImageProcessor ip2; // ip2 = imp.getProcessor(); Rectangle myroi = ip.getRoi(); int swidth = myroi.width; int sheight = myroi.height; int sy = myroi.y; int sx = myroi.x; if (swidth == width && sheight == height) { IJ.showMessage("Select a rectangular ROI"); IJ.beep(); return; } IJ.run("Select None"); int snumPixels = swidth * sheight; hsSource = new byte[snumPixels]; ssSource = new byte[snumPixels]; bsSource = new byte[snumPixels]; int[] pixs = new int[snumPixels]; int[] bin = new int[256]; int counter = 0, pi = 0, rangePassH = 0, rangeStopH = 0, rangePassL = 0, rangeStopL = 0, i, j; for (i = sy; i < sy + sheight; i++) { for (j = sx; j < sx + swidth; j++) { pixs[counter++] = ip.getPixel(j, i); } } // Get hsb or rgb from roi. ColorProcessor cp2 = new ColorProcessor(swidth, sheight, pixs); int iminhue = 256, imaxhue = -1, iminsat = 256, imaxsat = -1, iminbri = 256, imaxbri = -1; int iminred = 256, imaxred = -1, imingre = 256, imaxgre = -1, iminblu = 256, imaxblu = -1; if (isRGB) cp2.getRGB(hsSource, ssSource, bsSource); else cp2.getHSB(hsSource, ssSource, bsSource); for (i = 0; i < snumPixels; i++) { bin[hsSource[i] & 255] = 1; if ((hsSource[i] & 255) > imaxhue) imaxhue = (hsSource[i] & 255); if ((hsSource[i] & 255) < iminhue) iminhue = (hsSource[i] & 255); if ((ssSource[i] & 255) > imaxsat) imaxsat = (ssSource[i] & 255); if ((ssSource[i] & 255) < iminsat) iminsat = (ssSource[i] & 255); if ((bsSource[i] & 255) > imaxbri) imaxbri = (bsSource[i] & 255); if ((bsSource[i] & 255) < iminbri) iminbri = (bsSource[i] & 255); // IJ.showMessage("h:"+minhue+"H:"+maxhue+"s:"+minsat+"S:"+maxsat+"b:"+minbri+"B:"+maxbri); } if (!isRGB) { // get pass or stop filter whichever has a narrower range for (i = 0; i < 256; i++) { if (bin[i] > 0) { rangePassL = i; break; } } for (i = 255; i >= 0; i--) { if (bin[i] > 0) { rangePassH = i; break; } } for (i = 0; i < 256; i++) { if (bin[i] == 0) { rangeStopL = i; break; } } for (i = 255; i >= 0; i--) { if (bin[i] == 0) { rangeStopH = i; break; } } if ((rangePassH - rangePassL) < (rangeStopH - rangeStopL)) { bandPassH.setState(true); bandStopH.setState(false); iminhue = rangePassL; imaxhue = rangePassH; } else { bandPassH.setState(false); bandStopH.setState(true); iminhue = rangeStopL; imaxhue = rangeStopH; } } else { bandPassH.setState(true); bandStopH.setState(false); } adjustMinHue(iminhue); minSlider.setValue(iminhue); adjustMaxHue(imaxhue); maxSlider.setValue(imaxhue); adjustMinSat(iminsat); minSlider2.setValue(iminsat); adjustMaxSat(imaxsat); maxSlider2.setValue(imaxsat); adjustMinBri(iminbri); minSlider3.setValue(iminbri); adjustMaxBri(imaxbri); maxSlider3.setValue(imaxbri); originalB.setEnabled(true); // IJ.showStatus("done"); }