Attaching a Image to dataset

1,293 views
Skip to first unread message

Lawrence

unread,
Aug 8, 2012, 5:57:22 AM8/8/12
to fo-d...@googlegroups.com
I'm completely new to all this Dicom stuff... So excuse me if this is completely stupid or unclear.

I have build up my dataset and have DicomPixelData of my Image (which appears to be attached to the data set as otherpixeldata).

I am then creating a Dicom File from this dataset but the file created does not seem to have any image data at all(but does have the other information in the dataset) and will not open with microdicom. 

Is the pixeldata the correct or even a way to get the image in/on/attached to the File

Thanks in advance

Lawrence




Colby Dillion

unread,
Aug 8, 2012, 10:53:22 AM8/8/12
to fo-d...@googlegroups.com
You certainly have the right idea. Can you post some of your code so that we can get a better idea of what might be going wrong?

Lawrence

unread,
Aug 9, 2012, 6:29:31 AM8/9/12
to fo-d...@googlegroups.com
Below is the method I have for taking the image turning it into a bitmap and creating the dicom pixel data, Before this method is called the Dataset has been populated with data on a patient and procedure and this is the final stage before outputing the file.

Hope this is of some use


private void ProcessImage(byte[] imageBytes)
        {
          
            Bitmap bmp;
            int rows, columns;
            var bitmapUtils = new BitmapUtils();

            using(var ms = new MemoryStream())
            {
                ms.Write(imageBytes, 0, imageBytes.Length);
                ms.Seek(0L, SeekOrigin.Begin);
                bmp = new Bitmap(Image.FromStream(ms));
            }

            var pixelData = DicomPixelData.Create(data, true); //data is my DicomDataset

            pixelData.HighBit = 7;
            pixelData.BitsStored = 8;
            pixelData.BitsAllocated = 8;
            pixelData.SamplesPerPixel = 3;
            pixelData.PlanarConfiguration = 0;
            pixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
            pixelData.PixelRepresentation = 0;


            pixelData.Height = (ushort)bmp.Height;
            pixelData.Width = (ushort)bmp.Width;

            byte[] uncompressedBitmapByteArray = bitmapUtils.GetPixelData(bmp, out rows, out columns);

//below was the what I thought I had to do to get the image to the pixeldata imo this is where the problem may be

            using (var ms = new MemoryStream())
            {
                ms.Read(uncompressedBitmapByteArray, 0, uncompressedBitmapByteArray.Length);
                ms.Seek(0L, SeekOrigin.Begin);
                var s = new StreamByteSource(ms);

                pixelData.AddFrame(s.GetBuffer((uint)ms.Length));
            }
           
        }

Mahesh Dubey

unread,
Aug 9, 2012, 6:50:22 AM8/9/12
to fo-d...@googlegroups.com
Hello;
 
1) Is your bitmap pixel format is 24bppRgb ? If not then you have to convert it.

2) At least one of rows or columns must be even.

3) If require swap BGR to RGB

4) Then add the bitmap pixel array to dicom dataset.


Regards
Mahesh 

Colby Dillion

unread,
Aug 9, 2012, 9:33:53 AM8/9/12
to fo-d...@googlegroups.com
byte[] uncompressedBitmapByteArray = bitmapUtils.GetPixelData(bmp, out rows, out columns);
pixelData.AddFrame(new MemoryByteBuffer(uncompressedBitmapByteArray));

Lawrence

unread,
Aug 10, 2012, 5:09:45 AM8/10/12
to fo-d...@googlegroups.com
Thanks a bunch!... problem solved!

Hesham Nabih

unread,
Oct 30, 2012, 3:53:34 AM10/30/12
to fo-d...@googlegroups.com
Lawrence,
Where is the reference for BitmapUtils?
Thanks

On Friday, 10 August 2012 02:09:45 UTC-7, Lawrence wrote:
Thanks a bunch!... problem solved!

Lawrence

unread,
Oct 30, 2012, 5:12:00 AM10/30/12
to fo-d...@googlegroups.com
Hey Hesham Nabih,

BitmapUtils is a class that we wrote to do some basic manipulation of our images throughout our applcation. Are you trying to get this working? 

Regards 

Lawrence

Hesham Nabih

unread,
Oct 30, 2012, 12:12:02 PM10/30/12
to fo-d...@googlegroups.com
Hi Lawrence,
Thanks for your quick reply, yes I am trying, I had similar problem; I a study case of 2500 image, too may files to upload to a web-based viewer; I converted the files into an AVI file, then to JPEG images (reduced the number of files); then converted the JPEG back to DICOM; they can be displayed into the viewer, however could not change the window center and width becasue the Grayscale issue.  I would appreciate any suggestions.
Thanks again
Hesham

Hesham Nabih

unread,
Oct 30, 2012, 12:14:29 PM10/30/12
to fo-d...@googlegroups.com
Lawrence, I forgot to add a link to my post for the viewer https://groups.google.com/forum/#!topic/fo-dicom/rTTkSEVncEA
Thanks

Lawrence

unread,
Oct 31, 2012, 5:24:15 AM10/31/12
to fo-d...@googlegroups.com
Here is the code from the bitmap utils class that is used by my application. Let me know how it goes

 public byte[] GetPixelData(Bitmap image, out int rows, out int columns)
        {
            rows = image.Height;
            columns = image.Width;

            //At least one of rows or columns must be even.
            if (rows % 2 != 0 && columns % 2 != 0)
                --columns; //trim the last column.

            BitmapData data = image.LockBits(new Rectangle(0, 0, columns, rows), ImageLockMode.ReadOnly,PixelFormat.Format24bppRgb);
            IntPtr bmpData = data.Scan0;

            try
            {
                int stride = columns * 3;
                int size = rows * stride;

                byte[] pixelData = new byte[size];

                for (int i = 0; i < rows; ++i)
                    Marshal.Copy(new IntPtr(bmpData.ToInt64() + i * data.Stride), pixelData, i * stride, stride);

                //swap BGR to RGB
                SwapRedBlue(pixelData);
                return pixelData;
            }
            finally
            {
                image.UnlockBits(data);
            }
        }


  private void SwapRedBlue(byte[] pixels)
        {
            for (int i = 0; i < pixels.Length; i += 3)
            {
                byte temp = pixels[i];
                pixels[i] = pixels[i + 2];
                pixels[i + 2] = temp;
            }
        }

Good luck!

Lawrence

Hesham Nabih

unread,
Oct 31, 2012, 1:12:39 PM10/31/12
to fo-d...@googlegroups.com
Thank you so much Lawrence, this is great help; now I am trying to integrate it into my project;
I made a new class BitmapUtils, and here is my script to open the file and check if it is RGB, then I need to send it to ProcessImage, as per your original post at the top.

     private void openButton_Click(object sender, System.EventArgs e)
       
{
           
if (openFileDialog.ShowDialog() == DialogResult.OK)
           
{
                _filename
= openFileDialog.FileName;
               
DicomImage image1 = new DicomImage(_filename);
               
DicomDataset dataset = DicomFile.Open(_filename).Dataset;
               
string rb = (image1.PhotometricInterpretation).ToString(); // Check if the file is RGB

               
if (rb == "RGB")
               
{
                   
var pixelData = DicomPixelData.Create(image1.Dataset);
                   
var pixels = PixelDataFactory.Create(pixelData, 0);
                    dataset
.Remove(DicomTag.PhotometricInterpretation);
                    dataset
.Add(new DicomCodeString(DicomTag.PhotometricInterpretation, "MONOCHROME2"));
                    dataset
.Add(DicomTag.WindowCenter, "128.0");
                    dataset
.Add(DicomTag.WindowWidth, "256.0");
// I think the ProcessImage should go here!
               
}
               
DicomImage image = new DicomImage(dataset);
               
var g = GrayscaleRenderOptions.FromDataset(image.Dataset);
               
var wp = image.PixelData;
               
var wp1 = image.Dataset;
               
int maxV = image.NumberOfFrames;
                sbSlice
.Maximum = maxV - 1;
                sbSlice
.Minimum = 0;
                sbSlice
.Value = 0;
               
int coef = 2;
               
int Wcenter = (int)image.WindowCenter;
               
int Wwidth = (int)image.WindowWidth;
                tbrCenter
.Minimum = 0;
                tbrCenter
.Maximum = Wcenter * coef;
                tbrCenter
.Value = tbrCenter.Maximum / 2;
                lbCenter
.Text = (Wcenter).ToString();
                tbrWidth
.Minimum = 0;
                tbrWidth
.Maximum = Wwidth * coef;
                tbrWidth
.Value = tbrWidth.Maximum / 2;
                lbWidth
.Text = (Wwidth).ToString();
                lblSlice
.Text = sbSlice.Value.ToString();
               
DisplayImage();
           
}
           
//      }
       
}

       
protected void DisplayImage()
       
{
           
DicomDataset dataset = DicomFile.Open(_filename).Dataset;
           
DicomImage image = new DicomImage(dataset);
            image
.WindowCenter = tbrCenter.Value;
            image
.WindowWidth = tbrWidth.Value;
           
this.pictureBox1.Image = image.RenderImage(sbSlice.Value);
       
}


Since I am not experienced programmer, it might take me a while, but certainly will keep you posted; and thanks again for your assistance.
Hesham

Eko

unread,
Jan 11, 2013, 2:11:19 PM1/11/13
to fo-d...@googlegroups.com
Hi Hesham,
This code that transform RGB back to MONOCHROME2 works for you?

When I open a RGB Dicom Image, the properties WindowCenter and WindowWidth stop working.
I saw that in your code you tried handle it, but it really work or is a just attempt?

Eko
Reply all
Reply to author
Forward
0 new messages