Search in sources :

Example 6 with VCPixelClass

use of cbit.image.VCPixelClass in project vcell by virtualcell.

the class ROIMultiPaintManager method checkAll.

private VCImage checkAll() throws Exception {
    if (!overlayEditorPanelJAI.isHistogramSelectionEmpty()) /*overlayEditorPanelJAI.getHighliteInfo() != null*/
    {
        final String highlightDiscard = "discard, continue";
        final String cancelAssign = "Cancel";
        String result = DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Warning: Selections from the 'Histogram Tool' are present.  Choose an action:\n" + "1.  Discard selection without applying.\n" + "2.  Cancel, go back to Geometry Editor. (hint: Use 'Histogram Tool' apply)", new String[] { highlightDiscard, cancelAssign }, highlightDiscard);
        if (result.equals(highlightDiscard)) {
            overlayEditorPanelJAI.setHighliteInfo(null, OverlayEditorPanelJAI.FRAP_DATA_END_PROPERTY);
        } else {
            throw UserCancelException.CANCEL_GENERIC;
        }
    }
    ComboboxROIName[] roiNamesAndColors = overlayEditorPanelJAI.getAllCompositeROINamesAndColors();
    // Check for unassigned "background" pixels
    boolean bHasUnassignedBackground = false;
    for (int i = 0; i < roiComposite.length; i++) {
        byte[] pixData = ((DataBufferByte) roiComposite[i].getRaster().getDataBuffer()).getData();
        for (int j = 0; j < pixData.length; j++) {
            if (pixData[j] == 0) {
                bHasUnassignedBackground = true;
                break;
            }
        }
        if (bHasUnassignedBackground) {
            break;
        }
    }
    // Create PixelClasses
    VCPixelClass[] vcPixelClassesFromROINames = null;
    boolean bForceAssignBackground = false;
    if (bHasUnassignedBackground) {
        final String assignToBackground = "Assign as default 'background'";
        final String cancelAssign = "Cancel";
        String result = DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Warning: some areas of image segmentation have not been assigned to a Domain.  " + "Choose an action:\n" + "1.  Leave as is, unassigned areas should be treated as 'background'.\n" + "2.  Cancel, back to Geometry Editor. (hint: look at 'Domain Regions' list for 'bkgrnd' entries)", new String[] { assignToBackground, /*assignToNeighbors,*/
        cancelAssign }, assignToBackground);
        if (result.equals(assignToBackground)) {
            bForceAssignBackground = true;
        } else {
            throw UserCancelException.CANCEL_GENERIC;
        }
        if (bForceAssignBackground) {
            vcPixelClassesFromROINames = new VCPixelClass[roiNamesAndColors.length + 1];
            vcPixelClassesFromROINames[0] = new VCPixelClass(null, RESERVED_NAME_BACKGROUND, 0);
        }
    } else {
        vcPixelClassesFromROINames = new VCPixelClass[roiNamesAndColors.length];
    }
    // find pixel indexes corresponding to colors for ROIs
    int index = (bForceAssignBackground ? 1 : 0);
    for (int j = 0; j < roiNamesAndColors.length; j++) {
        String roiNameString = roiNamesAndColors[j].getROIName();
        vcPixelClassesFromROINames[index] = new VCPixelClass(null, roiNameString, roiNamesAndColors[j].getContrastColorIndex());
        index++;
    }
    VCImage initImage = createVCImageFromBufferedImages(ROIMultiPaintManager.DEFAULT_EXTENT, roiComposite);
    // Sanity check VCImage vcPixelClassesFromROINames and new vcPixelClassesFromVCImage found same pixel values
    VCPixelClass[] vcPixelClassesFromVCImage = initImage.getPixelClasses();
    for (int i = 0; i < vcPixelClassesFromVCImage.length; i++) {
        boolean bFound = false;
        for (int j = 0; j < vcPixelClassesFromROINames.length; j++) {
            if (vcPixelClassesFromROINames[j].getPixel() == vcPixelClassesFromVCImage[i].getPixel()) {
                bFound = true;
                break;
            }
        }
        if (!bFound) {
            throw new Exception("Error processing Domain Image.  Pixels found having no matching Domain.");
        }
    }
    Vector<VCPixelClass> missingDomainVCPixelClasses = new Vector<VCPixelClass>();
    Vector<VCPixelClass> foundDomainVCPixelClasses = new Vector<VCPixelClass>();
    StringBuffer missingROISB = new StringBuffer();
    for (int i = 0; i < vcPixelClassesFromROINames.length; i++) {
        boolean bFound = false;
        for (int j = 0; j < vcPixelClassesFromVCImage.length; j++) {
            if (vcPixelClassesFromROINames[i].getPixel() == vcPixelClassesFromVCImage[j].getPixel()) {
                bFound = true;
                break;
            }
        }
        if (!bFound) {
            missingROISB.append((missingDomainVCPixelClasses.size() > 0 ? "," : "") + "'" + vcPixelClassesFromROINames[i].getPixelClassName() + "'");
            missingDomainVCPixelClasses.add(vcPixelClassesFromROINames[i]);
        } else {
            foundDomainVCPixelClasses.add(vcPixelClassesFromROINames[i]);
        }
    }
    if (missingDomainVCPixelClasses.size() > 0) {
        final String removeROI = "Remove Domain" + (missingDomainVCPixelClasses.size() > 1 ? "s" : "") + " and continue";
        final String backtoSegment = "Return to segmentation";
        String result = DialogUtils.showWarningDialog(overlayEditorPanelJAI, "Domain" + (missingDomainVCPixelClasses.size() > 1 ? "s" : "") + " named " + missingROISB.toString() + " have no pixels defined", new String[] { removeROI, backtoSegment }, removeROI);
        if (result.equals(removeROI)) {
            vcPixelClassesFromROINames = foundDomainVCPixelClasses.toArray(new VCPixelClass[0]);
        } else {
            throw UserCancelException.CANCEL_GENERIC;
        }
    }
    // Check borders
    VCImage temp = checkBorders(initImage);
    if (temp != null) {
        initImage = temp;
    }
    // Check if we added a border and don't have a VCPixelClass for background
    boolean bHasBackgroundVCPixel = false;
    for (int j = 0; j < vcPixelClassesFromROINames.length; j++) {
        if (vcPixelClassesFromROINames[j].getPixel() == 0) {
            bHasBackgroundVCPixel = true;
            break;
        }
    }
    if (!bHasBackgroundVCPixel) {
        for (int i = 0; i < initImage.getPixels().length; i++) {
            if (initImage.getPixels()[i] == 0) {
                VCPixelClass[] tempvcp = new VCPixelClass[vcPixelClassesFromROINames.length + 1];
                tempvcp[0] = new VCPixelClass(null, RESERVED_NAME_BACKGROUND, 0);
                System.arraycopy(vcPixelClassesFromROINames, 0, tempvcp, 1, vcPixelClassesFromROINames.length);
                vcPixelClassesFromROINames = tempvcp;
                break;
            }
        }
    }
    initImage.setPixelClasses(vcPixelClassesFromROINames);
    updateExtent(initImage, originalExtent, originalISize);
    return initImage;
}
Also used : VCPixelClass(cbit.image.VCPixelClass) VCImage(cbit.image.VCImage) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) UtilCancelException(org.vcell.util.UtilCancelException) UserCancelException(org.vcell.util.UserCancelException) Vector(java.util.Vector)

Example 7 with VCPixelClass

use of cbit.image.VCPixelClass in project vcell by virtualcell.

the class GeomDbDriver method getSubVolume.

/**
 * getModel method comment.
 */
private SubVolume getSubVolume(QueryHashtable dbc, Connection con, ResultSet rset) throws SQLException, DataAccessException, ExpressionException {
    // showMetaData(rset);
    // 
    // if already in cache, then return that instance instead of a new one
    // 
    KeyValue svKey = new KeyValue(rset.getBigDecimal(subVolumeTable.id.toString()));
    SubVolume subVolume = (SubVolume) dbc.get(svKey);
    if (subVolume != null) {
        return subVolume;
    }
    java.math.BigDecimal imageRegionKeyBigDecimal = rset.getBigDecimal(subVolumeTable.imageRegionRef.toString());
    KeyValue imageRegionKey = null;
    if (!rset.wasNull()) {
        imageRegionKey = new KeyValue(imageRegionKeyBigDecimal);
    }
    if (imageRegionKey == null) {
        subVolume = subVolumeTable.getAnalyticOrCompartmentSubVolume(svKey, rset);
    } else {
        VCPixelClass vcPixelClass = getPixelClass(dbc, con, imageRegionKey);
        subVolume = subVolumeTable.getImageSubVolume(svKey, rset, vcPixelClass);
    }
    // 
    // put newly read SumVolume into object cache
    // 
    dbc.put(svKey, subVolume);
    return subVolume;
}
Also used : BigDecimal(java.math.BigDecimal) VCPixelClass(cbit.image.VCPixelClass) KeyValue(org.vcell.util.document.KeyValue) SubVolume(cbit.vcell.geometry.SubVolume)

Example 8 with VCPixelClass

use of cbit.image.VCPixelClass in project vcell by virtualcell.

the class GeomDbDriver method insertVCImage.

/**
 * This method was created in VisualAge.
 * @return cbit.image.VCImage
 * @param user cbit.vcell.server.User
 * @param image cbit.image.VCImage
 */
private void insertVCImage(InsertHashtable hash, Connection con, User user, VCImage image, Version newVersion) throws ImageException, DataAccessException, SQLException {
    // Connection con = conFact.getConnection();
    // KeyValue imageKey = keyFactory.getNewKey(con);
    KeyValue keySizeRef = keyFactory.getNewKey(con);
    KeyValue imageDataKey = keyFactory.getNewKey(con);
    KeyValue browseImageKey = keyFactory.getNewKey(con);
    insertExtentSQL(con, keySizeRef, image.getExtent().getX(), image.getExtent().getY(), image.getExtent().getZ());
    insertImageSQL(con, image, keySizeRef, newVersion, user);
    insertImageDataSQL(con, imageDataKey, newVersion.getVersionKey(), /*imageKey*/
    image);
    try {
        insertBrowseImageDataSQL(con, browseImageKey, newVersion.getVersionKey(), /*imageKey*/
        image);
    } catch (cbit.image.GifParsingException e) {
        lg.error(e.getMessage(), e);
        throw new DataAccessException("Error Parsing BrowseImage", e);
    }
    VCPixelClass[] vcPixelClasses = image.getPixelClasses();
    for (int i = 0; i < vcPixelClasses.length; i++) {
        KeyValue keyPixelClass = keyFactory.getNewKey(con);
        insertPixelClassSQL(con, keyPixelClass, newVersion.getVersionKey(), /*imageKey*/
        vcPixelClasses[i]);
        hash.put(vcPixelClasses[i], keyPixelClass);
    }
    hash.put(image, newVersion.getVersionKey());
}
Also used : GifParsingException(cbit.image.GifParsingException) VCPixelClass(cbit.image.VCPixelClass) KeyValue(org.vcell.util.document.KeyValue) DataAccessException(org.vcell.util.DataAccessException)

Example 9 with VCPixelClass

use of cbit.image.VCPixelClass in project vcell by virtualcell.

the class RayCaster method resampleGeometry.

public static Geometry resampleGeometry(GeometryThumbnailImageFactory geometryThumbnailImageFactory, Geometry origGeometry, ISize sampleSize) throws ImageException, PropertyVetoException, GeometryException, ExpressionException {
    if (origGeometry.getDimension() < 3) {
        throw new GeometryException("Presently, the Raycaster resampling works only for 3d geometries.");
    }
    GeometrySpec origGeometrySpec = origGeometry.getGeometrySpec();
    VCImage origSubvolumeImage = origGeometrySpec.getSampledImage().getCurrentValue();
    if (origSubvolumeImage == null) {
        throw new GeometryException("original geometry does not have a sampled image");
    }
    VCImage resampledSubvolumeImage = RayCaster.sampleGeometry(origGeometry, sampleSize, false);
    // 
    // Check if resampling failed:
    // if not the same number of pixelClasses (between original geometry and resampled)
    // if the subvolume handles are the same
    // 
    boolean bSameSubvolumes = true;
    if (origSubvolumeImage.getNumPixelClasses() != resampledSubvolumeImage.getNumPixelClasses()) {
        bSameSubvolumes = false;
    }
    // 
    for (VCPixelClass origPixelClass : origSubvolumeImage.getPixelClasses()) {
        VCPixelClass resampledPixelClass = resampledSubvolumeImage.getPixelClassFromPixelValue(origPixelClass.getPixel());
        if (resampledPixelClass == null) {
            bSameSubvolumes = false;
            break;
        }
    }
    // 
    if (!bSameSubvolumes) {
        StringBuffer message = new StringBuffer();
        message.append("\n\nexisting geometry:\n");
        for (SubVolume oldSubvolume : origGeometrySpec.getSubVolumes()) {
            long count = origSubvolumeImage.countPixelsByValue((byte) oldSubvolume.getHandle());
            message.append("subvolume('" + oldSubvolume.getName() + "',handle=" + oldSubvolume.getHandle() + ",numPixels=" + count + " of " + origSubvolumeImage.getNumXYZ() + "\n");
        }
        message.append("\n\nnew resampled handle VCImage:\n");
        for (VCPixelClass newPixelClass : resampledSubvolumeImage.getPixelClasses()) {
            long count = resampledSubvolumeImage.countPixelsByValue((byte) newPixelClass.getPixel());
            message.append("pixelClass('" + newPixelClass.getPixelClassName() + "',pixelValue=" + newPixelClass.getPixel() + ",numPixels=" + count + " of " + resampledSubvolumeImage.getNumXYZ() + ")\n");
        }
        throw new GeometryException("original Geometry had " + origSubvolumeImage.getNumPixelClasses() + " subvolumes, resampled Geometry found " + resampledSubvolumeImage.getNumPixelClasses() + " subvolumes " + message.toString());
    }
    // 
    // Create new VCImage that will form the basis for a new image-based geometry.
    // 
    VCImage newVCImage = null;
    if (origGeometrySpec.getImage() != null) {
        // 
        // was an image-based geometry - try to make new VCImage similar to the original (same pixelClass names and pixel values).
        // the goal is to make identical geometries if the sample size is same as the original image size.
        // 
        // create a new VCImage with same image pixel values (not subvolume handles) and pixel class names as original image.
        // 
        byte[] newVCImagePixels = new byte[sampleSize.getXYZ()];
        byte[] resampledSubvolumePixels = resampledSubvolumeImage.getPixels();
        for (int i = 0; i < sampleSize.getXYZ(); i++) {
            int subvolumeHandle = resampledSubvolumePixels[i];
            ImageSubVolume imageSubvolume = (ImageSubVolume) origGeometrySpec.getSubVolume(subvolumeHandle);
            newVCImagePixels[i] = (byte) imageSubvolume.getPixelValue();
        }
        newVCImage = new VCImageUncompressed(null, newVCImagePixels, origGeometry.getExtent(), sampleSize.getX(), sampleSize.getY(), sampleSize.getZ());
        newVCImage.setName(origGeometrySpec.getImage().getName());
        ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
        for (VCPixelClass origPixelClass : origGeometrySpec.getImage().getPixelClasses()) {
            SubVolume origSubvolume = origGeometrySpec.getImageSubVolumeFromPixelValue(origPixelClass.getPixel());
            newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origPixelClass.getPixel()));
        }
        newVCImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
    } else {
        // 
        // was an analytic geometry - create a new image-based geometry
        // 
        // create a new VCImage with image pixel values and pixelClass names equal to corresponding subvolumes from original geometry
        // make new subvolume names equal to old subvolume names.
        // 
        byte[] newVCImageSubvolumePixels = resampledSubvolumeImage.getPixels().clone();
        newVCImage = new VCImageUncompressed(null, newVCImageSubvolumePixels, origGeometry.getExtent(), sampleSize.getX(), sampleSize.getY(), sampleSize.getZ());
        ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
        for (SubVolume origSubvolume : origGeometrySpec.getSubVolumes()) {
            // note: newVCImage already has subvolume handle pixels.
            newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origSubvolume.getHandle()));
        }
        newVCImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
    }
    // 
    // construct the new geometry with the sampled VCImage.
    // 
    Geometry newGeometry = new Geometry(origGeometry.getName(), newVCImage);
    newGeometry.getGeometrySpec().setExtent(origGeometry.getExtent());
    newGeometry.getGeometrySpec().setOrigin(origGeometry.getOrigin());
    newGeometry.setDescription(origGeometry.getDescription());
    newGeometry.getGeometrySurfaceDescription().setFilterCutoffFrequency(origGeometry.getGeometrySurfaceDescription().getFilterCutoffFrequency());
    newGeometry.precomputeAll(geometryThumbnailImageFactory, true, true);
    return newGeometry;
}
Also used : VCPixelClass(cbit.image.VCPixelClass) GeometryException(cbit.vcell.geometry.GeometryException) ArrayList(java.util.ArrayList) VCImage(cbit.image.VCImage) VCImageUncompressed(cbit.image.VCImageUncompressed) GeometrySpec(cbit.vcell.geometry.GeometrySpec) Geometry(cbit.vcell.geometry.Geometry) SubVolume(cbit.vcell.geometry.SubVolume) ImageSubVolume(cbit.vcell.geometry.ImageSubVolume) ImageSubVolume(cbit.vcell.geometry.ImageSubVolume)

Example 10 with VCPixelClass

use of cbit.image.VCPixelClass in project vcell by virtualcell.

the class GeometryThumbnailImageFactoryAWT method getThumbnailImage.

public ThumbnailImage getThumbnailImage(GeometrySpec geometrySpec) throws ImageException {
    int REAL_SAMPLE_X = 0;
    int REAL_SAMPLE_Y = 0;
    if (geometrySpec.getDimension() > 0) {
        // Calc Scaling parameters
        double srcScaleX = SAMPLED_GEOM_SIZE_MAX / geometrySpec.getExtent().getX();
        double srcScaleY = SAMPLED_GEOM_SIZE_MAX / geometrySpec.getExtent().getY();
        if (srcScaleX < srcScaleY) {
            REAL_SAMPLE_X = SAMPLED_GEOM_SIZE_MAX;
            REAL_SAMPLE_Y = Math.max((int) (srcScaleX * geometrySpec.getExtent().getY()), 1);
        } else {
            REAL_SAMPLE_Y = SAMPLED_GEOM_SIZE_MAX;
            REAL_SAMPLE_X = Math.max((int) (srcScaleY * geometrySpec.getExtent().getX()), 1);
        }
    }
    if (geometrySpec.getDimension() > 0) {
        BufferedImage brightImage = new BufferedImage(REAL_SAMPLE_X, REAL_SAMPLE_Y, BufferedImage.TYPE_INT_RGB);
        Graphics2D brightG2D = brightImage.createGraphics();
        brightG2D.setColor(java.awt.Color.white);
        brightG2D.fillRect(0, 0, REAL_SAMPLE_X, REAL_SAMPLE_Y);
        VCImage currSampledImage = geometrySpec.getSampledImage().getCurrentValue();
        java.awt.image.IndexColorModel handleColorMap = DisplayAdapterService.getHandleColorMap();
        byte[] reds = new byte[256];
        handleColorMap.getReds(reds);
        byte[] greens = new byte[256];
        handleColorMap.getGreens(greens);
        byte[] blues = new byte[256];
        handleColorMap.getBlues(blues);
        // Create projections of each subvolume handle
        VCPixelClass[] pixClassHandles = currSampledImage.getPixelClasses();
        byte[] pixels = currSampledImage.getPixels();
        for (int i = 0; i < pixClassHandles.length; i += 1) {
            byte[] zBuf = new byte[currSampledImage.getNumX() * currSampledImage.getNumY()];
            java.util.Arrays.fill(zBuf, (byte) 0);
            // Project z
            for (int j = 0; j < pixels.length; j += 1) {
                if (pixels[j] == pixClassHandles[i].getPixel()) {
                    zBuf[j % zBuf.length] = (byte) 1;
                }
            }
            // Scale X-Y
            int cmapIndex = (pixClassHandles[i].getPixel() & 0xff);
            byte ired = reds[cmapIndex];
            byte igrn = greens[cmapIndex];
            byte iblu = blues[cmapIndex];
            IndexColorModel colorModel = new IndexColorModel(8, 2, new byte[] { 0, ired }, new byte[] { 0, igrn }, new byte[] { 0, iblu }, new byte[] { 0, (byte) (200) });
            int width = currSampledImage.getNumX();
            int height = currSampledImage.getNumY();
            BufferedImage bufferedImage = getImage(colorModel, zBuf, width, height);
            ImageIcon theImageIcon = new ImageIcon(bufferedImage.getScaledInstance(REAL_SAMPLE_X, REAL_SAMPLE_Y, Image.SCALE_AREA_AVERAGING));
            brightG2D.drawImage(theImageIcon.getImage(), 0, 0, theImageIcon.getImageObserver());
        }
        int[] rgb = brightImage.getRGB(0, 0, REAL_SAMPLE_X, REAL_SAMPLE_Y, null, 0, REAL_SAMPLE_X);
        return new ThumbnailImage(rgb, REAL_SAMPLE_X, REAL_SAMPLE_Y, brightImage);
    }
    return null;
}
Also used : ImageIcon(javax.swing.ImageIcon) VCPixelClass(cbit.image.VCPixelClass) VCImage(cbit.image.VCImage) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ThumbnailImage(cbit.image.ThumbnailImage) IndexColorModel(java.awt.image.IndexColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

VCPixelClass (cbit.image.VCPixelClass)17 VCImage (cbit.image.VCImage)7 KeyValue (org.vcell.util.document.KeyValue)7 SubVolume (cbit.vcell.geometry.SubVolume)6 PropertyVetoException (java.beans.PropertyVetoException)6 ImageException (cbit.image.ImageException)4 Geometry (cbit.vcell.geometry.Geometry)4 ArrayList (java.util.ArrayList)4 Vector (java.util.Vector)4 DataAccessException (org.vcell.util.DataAccessException)4 VCImageCompressed (cbit.image.VCImageCompressed)3 VCImageUncompressed (cbit.image.VCImageUncompressed)3 AnalyticSubVolume (cbit.vcell.geometry.AnalyticSubVolume)3 GeometryException (cbit.vcell.geometry.GeometryException)3 GeometrySpec (cbit.vcell.geometry.GeometrySpec)3 ImageSubVolume (cbit.vcell.geometry.ImageSubVolume)3 IOException (java.io.IOException)3 Extent (org.vcell.util.Extent)3 ISize (org.vcell.util.ISize)3 Origin (org.vcell.util.Origin)3