Imebra - Change color contrasts

290 views Asked by At

I am able to load dicom image using imebra, and want to change the colors of image, but cant figure out a way. I want to achieve functionality as in Dicomite app.

Following is my code:

public void loadDCM() {

  com.imebra.DataSet loadedDataSet = com.imebra.CodecFactory.load(dicomPath.getPath());

  com.imebra.VOIs voi = loadedDataSet.getVOIs();

  com.imebra.Image image = loadedDataSet.getImageApplyModalityTransform(0);
  //        com.imebra.Image image = loadedDataSet.getImage(0);
  String colorSpace = image.getColorSpace();


  long width = image.getWidth();
  long height = image.getHeight();

  TransformsChain transformsChain = new TransformsChain();
  com.imebra.DrawBitmap drawBitmap = new com.imebra.DrawBitmap(transformsChain);


  com.imebra.TransformsChain chain = new com.imebra.TransformsChain();
  if (com.imebra.ColorTransformsFactory.isMonochrome(image.getColorSpace())) {
   // Allocate a VOILUT transform. If the DataSet does not contain any pre-defined
   //  settings then we will find the optimal ones.
   VOILUT voilutTransform = new VOILUT();

   // Retrieve the VOIs (center/width pairs)
   com.imebra.VOIs vois = loadedDataSet.getVOIs();

   // Retrieve the LUTs
   List < LUT > luts = new ArrayList < LUT > ();
   for (long scanLUTs = 0;; scanLUTs++) {
    try {
     luts.add(loadedDataSet.getLUT(new com.imebra.TagId(0x0028, 0x3010), scanLUTs));
    } catch (Exception e) {
     break;
    }
   }

   if (!vois.isEmpty()) {
    voilutTransform.setCenterWidth(vois.get(0).getCenter(), vois.get(0).getWidth());
   } else if (!luts.isEmpty()) {
    voilutTransform.setLUT(luts.get(0));
   } else {
    voilutTransform.applyOptimalVOI(image, 0, 0, width, height);
   }

   chain.addTransform(voilutTransform);

   com.imebra.DrawBitmap draw = new com.imebra.DrawBitmap(chain);

   // Ask for the size of the buffer (in bytes)

   long requestedBufferSize = draw.getBitmap(image, drawBitmapType_t.drawBitmapRGBA, 4, new byte[0]);

   byte buffer[] = new byte[(int) requestedBufferSize]; // Ideally you want to reuse this in subsequent calls to getBitmap()
   ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);

   // Now fill the buffer with the image data and create a bitmap from it
   drawBitmap.getBitmap(image, drawBitmapType_t.drawBitmapRGBA, 4, buffer);
   Bitmap renderBitmap = Bitmap.createBitmap((int) image.getWidth(), (int) image.getHeight(), Bitmap.Config.ARGB_8888);
   renderBitmap.copyPixelsFromBuffer(byteBuffer);

   image_view.setImageBitmap(renderBitmap);

  }
1

There are 1 answers

5
Paolo Brandoli On

If you are dealing with a monochrome image and you want to modify the presentation luminosity/contrast, then you have to modify the parameters of the VOILUT transform (voilutTransform variable in your code).

You can get the center and width that the transform is applying to the image before calculating the bitmap to be displayed, then modify them before calling drawBitmap.getBitmap again.

E.g., to double the contrast:

voilutTransform.setCenterWidth(voilutTransform.getCenter(), voilutTransform.getWidth() / 2);

// Now fill the buffer with the image data and create a bitmap from it
drawBitmap.getBitmap(image, drawBitmapType_t.drawBitmapRGBA, 4, buffer);

See this answer for more details about the center/width