How to convert a 2D array of pixels in to a grayscale planar image

Im having following problems, I hope someone may help me.
1.     I want to convert a 2D array of pixels (Grayscale values) into a Grayscale PlannerImage. I tried the following code I found on net.
The steps to do this are:
-Construct a DataBuffer from your data array.
-Construct a SampleModel describing the data layout.
-Construct a Raster from the DataBuffer and SampleModel. You can use methods from the RasterFactory class to do this.
-Construct a ColorModel which describes your data. The factory method PlanarImage.createColorModel(sampleModel) will take care of this for some common cases.
-Construct a TiledImage with the SampleModel and ColorModel.
-Populate the TiledImage with your data by using the TiledImage.setData() method to copy your raster into the TiledImage.
Only the last step involves any actual processing. The rest is just object creation.
public static RenderedImage createRenderedImage(float[][] theData, int width, int height, int numBands) {
int len = width * height * numBands;
Point origin = new Point(0,0);
// create a float sample model
SampleModel sampleModel =
RasterFactory.createBandedSampleModel(DataBuffer.TYPE_FLOAT,width,height,numBands);
// create a compatible ColorModel
ColorModel colourModel = PlanarImage.createColorModel(sampleModel);
// create a TiledImage using the float SampleModel
TiledImage tiledImage = new TiledImage(origin,sampleModel,width,height);
// create a DataBuffer from the float[][] array
DataBufferFloat dataBuffer = new DataBufferFloat(theData, len);
// create a Raster
Raster raster = RasterFactory.createWritableRaster(sampleModel,dataBuffer,origin);
// set the TiledImage data to that of the Raster
tiledImage.setData(raster);
RenderedImageAdapter img = new RenderedImageAdapter((RenderedImage)tiledImage);
return img;
I passed it 2D array of pixels with 3 bands, and it gave an exception >>Array index out of bounds at this line >>tiledImage.setData(raster). Then I tried it with a 1D array of length height*width with a single band. So Now it gives me a monochromatic RenderedImage. How can I make it a grayscale PlanarImage.

jyang, thank you very much for your response. I believe I found a different solution all together, by converting each 16-bit intensity value into an 8-bit intensity value via an intensity ratio (ie: divide each intensity by the maximum and multiply by 256). The attachment shows the new program.
Attachments:
mod_image.vi ‏2004 KB

Similar Messages

  • How to convert arraylist into array

    how to convert arraylist into array?

    If you are using generics, I would use this version of toArray:
    List < X > list = ...
    X[] array = list.toArray(new X[list.size()]);If you are not using generics, that same thing looks like:
    List  list = ...
    X[] array = (X[]) list.toArray(new X[list.size()]);

  • How to convert an int array to string array?

    Hi,
    Can anyone please help me with this question?
    int number={4,6,45,3,2,77};
    I like to convert this into a list and sort
    List mylist = Arrays.asList(number);
    Collections.sort(mylist);
    But first I need to convert the int array to String array.
    Please advise. Thanks.

    If you want to convert your int array to a String array, you have no choice but doing a conversion element by element in a for loop.
    However, if the sort method doesn't behave as desired, you can use the one with 2 parameters, the second parameter being a comparator class.
    Check the javadoc for more information : http://java.sun.com/j2se/1.3/docs/api/java/util/Collections.html
    /Stephane

  • NI-DAQmx Analog out to Cluster how to convert to individual array elements

    I am trying to decompose the cluster that comes from a single channel multiple point anolog input capture. What you get is a cluster that has the starting time, the time interval between samples and the actual analog input data points. What I want to do is after capture I want to have access to the data so I can make calculations on it. When I am done I want to write the data to a spreadsheet file. I can't seem to get the cluster to array function to wire up. I have tried every possible combination of functions and I can't get them to wire up eventhough the input type is a cluster. I have posted a screen shot of my code below.
    I can get this to work with raw data using the express vi that writes to spreadsheet but I would like to know how to do this manually as well.
    Perry
    Attachments:
    unbundle.jpg ‏85 KB

    You don't understand how dataflow works with LabVIEW and an understanding of this is essential. Dataflow means that a function does not execute until all of the data wired to it is present. In your second file, you create the date/time for the header in the second frame of the sequence structure, when the frame completes, it passes out of the structure to the concantanate string function. This is then wired to the Write Characters to File Function which will now execute because all the data inputs are present. This is set to create a new file so it is executing after the Write to Spreadsheet File which is inside the sequence structure. You do not need the sequence structure at all. You do need to provide some dataflow between the Write Characters and the Write to Spreadsheet File. Here's a bit cleaner example of your code.
    Attachments:
    Example with Dataflow.vi ‏190 KB

  • How to transfer a 2D array from NI-scop into a gray scale image and save it

    Dear all,
    I acquired a 2D array and want to transfer it into a gray scale image (12 bits) and save it into my hard disk but I don't know how to do it.
    I can use Intensity graph to show the 2Darray into gray scale image, but the intensity graph.vi can not save the image in real time.
    Thank you.
    Solved!
    Go to Solution.

    The 24-bit is a color image with 8-bits per color channel per pixel (interleaved, if I remember correctly).  It is not a 24-bit gray-level, so I don't think it will work for you.  I believe the IMAQ library (a LabVIEW add-on) will handle 16-bit gray-scale, if you can get a copy.
    As a native LabVIEW workaround, you could also split your image into two 8-bit images, with one image representing the high bytes, the other the low.  You could then combine them when you do the analysis, if your analysis program is capable of this.
    This account is no longer active. Contact ShadesOfGray for current posts and information.

  • How Convert a Byte array to a image format

    Hi,
    How to convert a Byte array to an image format....I shall be more clear what i want...See i recieve a Byte array fom server this Byte array before recieveing at my end it was converted from an image/text format into a Byte array and is sent from server and, I recieve this Byte array now I have to convert this Byte array into their respective image....please tell me how to convert a Byte array to a image ......... Kindly explain clearly as i am new to Java......
    Thanking You.

    Note: This thread was originally posted in the [New To Java|http://forums.sun.com/forum.jspa?forumID=54] forum, but moved to this forum for closer topic alignment.

  • How to convert a .jsp to a .java/.class for use in pplcode

    .

    At first , thanks for your help ,armalcolm
    But I don't understand how to convert java string array to c string(char)array.I have seen the doc. , but I don't understand still.
    Belown is my code
    <code>
    JNIEXPORT void JNICALL Java_init
    (JNIEnv *env, jobject jobj, jobjectArray jfilename, jint jlength)
         jobject obj = env->NewObject(env,mid,param);
         jobjectArray obj_java = (jobjectArray)env->CallObjectMethod(obj,mid,param);
         length = jlength;
         filename = (char**) malloc(length * sizeof(char*));
         for (i=0;i<length;i++)
              jstring jstr = env->(jstring) GetObjectArray(obj_java,i);
              filename[i] = (char*) malloc(strlen(jstr)+1);
              strcpy(filename,GetStringUTFChars(jstr,0));
    </code>
    I know this is a error codes.Can you help me??
    I don't know how to use NewObject() , CallObjectMethod() and GetObjectArray() method
    Thanks

  • How to convert nikon files to jpg

    how to convert nikon files to jpg

    To see Specs for ViewNX 2:
    http://imaging.nikon.com/lineup/software/viewnx2/spec.htm
    To Download, if it looks like it may work for you:
    http://nikonimglib.com/nvnx/
    {says "free download" but don't know if there's a catch}
    For more information about the Download, specs, and details
    for Macintosh see english North/South American download page:
    http://nikonimglib.com/nvnx/index_en_nsa.html#os-macintosh
    PS: if you get that, and find it crashes, see threads such as this:
    Nikon Capture NX2 crashes when opening image
    {thought the original poster could read all contents here... if not, sorry}
    Good luck!
    edited

  • How to convert image to array pixel values in java

    I would like to know how to convert image into arrary of pixels in java ?
    which API should i use ?
    please give me link to a sample program !

    I find the most efficient way to just walk over the image and call getRGB(x,y) on every pixel, more so than Pixelgrabber. If someone knows a more efficient way, give me a beep.

  • Can I convert 3D image to an array of pixels in LabVIEW?

    Hi all;
    I am still new with labview. I has 1 question, can I convert 3D image to an array of pixels using labview?
    Most of the examples I found they only convert 2D image. Hope anyone can give me some hint.
    Thank You

    look at this thread.
    It has links to other threads where the 3D graphs were used.
    You should also search for "gif reader" to find threads where images are read from gif files.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • How to convert digital array data into analog signal

    i want help to convert digital data array into analog signal

    shubham62 wrote:
    We are implimenting real time audio trans-receiver. We have converted input audio data(analog form) into digital data. At the receiver side we received digital data which is in the form of digital data array.But we are unable to recover back our original input data. So please help us to sort out the problem.
    Still no useful information.  How was the data converted to digital in the first place?  What conversion parameters did you use?  As was said, you just need to do the math backwards to get it back into the analog.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How to convert 1D array of string to string

    How to convert 1D array of string to string.

    Maximus00, as Pavel indicated, there is a lesser known feature of "Concatenate Strings" that does exactly what your code is doing if the input is an array of strings. In one step! See attached image.
    LabVIEW Champion . Do more with less code and in less time .
    Attachments:
    ConcatenateArray ofStrings.gif ‏7 KB

  • How to convert a waveform signal into array of complex numbers

    How to convert a waveform signal into array of complex numbers.

    Hi Chaks,
    try this:
    (Get Waveform components, convert to complex DBL)
    Message Edited by GerdW on 01-28-2008 09:23 AM
    Best regards,
    GerdW
    CLAD, using 2009SP1 + LV2011SP1 + LV2014SP1 on WinXP+Win7+cRIO
    Kudos are welcome
    Attachments:
    ToCDB.png ‏1 KB

  • How to convert Pixels into inch?

    Hi Friends,
    I am using Crystal XI R2.
    I have standard Report formatting specifications. This document states all the specifications in Pixels and not in inches.
    I am not sure on the pixel to inches conversion.
    Can anyone please help on this how to convert the pixels into inches...
    Thanks in advance.
    Regards,
    Ashish

    you need to know their scale as in Dot per inch to be able to do the conversion. 72 dpi will give you are larger number compared to 96 dpi
    here is a link
    http://mystic-nights.com/poser/tools/pixels-to-inches.html
    Edited by: Zilla Eh on Nov 28, 2008 1:49 AM

  • How can one convert a picture to 1080 pixels in height at 72 dpi and retain the aspect ratio?

    How can one convert a picture to 1080 pixels in height at 72 dpi and retain the aspect ratio?

    The ppi has nothing to do with this. It will stay at whatever it is.
    Go to image>resize>resize image, turn ON Resample image and enter 1080 for the height, making sure to leave constrain proportions turned on. Don't mess with the ppi setting.
    If the image is set to another ppi setting, then after you're done with the above, go back into that window, turn OFF resample image, and set the ppi to 72. PPI only affects the inc/cm measurement.

Maybe you are looking for

  • Data not shown in output but was in XML(diagnostics)

    Hi Gurus, I have created a custom account analysis report for payables by editing the XLAAARPT.xml and XLAAAR01.rtf then saving both into a new file name. Then, i tried to run the custom report and it was completed normally but when i check for the o

  • HELP using a fire wire to tranfer files from my PC to my new MacBook

    I just purchased a new MacBook and am trying to transfer my songs, pictures and movies from my desktop pc (windows XP) to my new MacBook. I was instructed to use a 6pin to 6pin firewire to tranfer the files from my pc to my MacBook by using the MacBo

  • Why can't I swap my PC serial for a Mac one?

    Hi, Hoping this is the correct place to ask this question. My company purchased Creative Suite 3 and 2 x copies of Photoshop CS3 a few years ago - they have been in use until recently. We have since upgraded to Mac's for design purposes; I de-activat

  • Report on Costing Run

    We have a requirement for a report.  We run the standard costing run every period for all finished and semi-finished materials.  Is there a standard report available from the system for reporting the standard cost (cost component items) for the mater

  • How do you purchase items off your wishlist all at once?

    How do you purchase items off your wishlist all at once?