Converting 256 level image to a 16 level image

Discussion in 'MATLAB' started by gunadp, Jun 6, 2008.

  1. gunadp

    gunadp Guest


    Could any of you tell me how to convert a 256 level image (for example
    lena.tif) to a 16 level ( 4 bit) image in matlab? Are there special
    functions such as im2bw or is it just the (/256*16)? I want to get the
    same image in different gray levels and show them through imshow.


    gunadp, Jun 6, 2008
    1. Advertisements

  2. wrote in message <c538e97f-fdd5-43d6-a4a9-
    If a number or vector x has the range 0:255, you can rescale it to the range
    0:15 with y = floor(x/16). You don't need any special function for something
    that simple.

    Roger Stafford
    Roger Stafford, Jun 6, 2008
    1. Advertisements

  3. On the other hand "16 level ( 4 bit)" does not always mean 16
    equidistant levels: a "4 bit" image might be an image with a 4 bit
    index into a colour map or grey-level map. If that is the case
    in this situation, then finding the "best" 16 levels out of 256
    is non-trivial. (It is too late at night for me to figure out how
    one might proceed in such a situation.)
    Walter Roberson, Jun 6, 2008
  4. (Walter Roberson) wrote in message <g2anvv
    Good point, Walter. If it is to be some kind of mapping, one can define a
    15-element monotonically increasing mapping vector g with g(i) being the
    smallest value in the range 0:255 which is to map into i and if the value is
    less than g(1), it maps into 0. Let x be an array of values, each in the range
    from 0 to 255. Then do this binary search table lookup:

    y = repmat(8,size(x));
    for k = [4,2,1,1/2]
    y = y + (2*(x>=reshape(g(y),size(x)))-1)*k;
    y = y - 1/2;

    Then y would be the mapping of array x into the range 0:15 in accordance
    with the mapping defined by g.

    Roger Stafford
    Roger Stafford, Jun 6, 2008
  5. -----------
    To avoid two of the reshapes, I should probably have written the binary
    search above as follows. First, make sure g is defined as a column vector.

    x2 = x:));
    y = repmat(8,size(x2));
    for k = [4,2,1,1/2]
    y = y + (2*(x2>=g(y))-1)*k;
    y = reshape(y-1/2,size(x));

    Roger Stafford
    Roger Stafford, Jun 6, 2008
  6. gunadp

    gunadp Guest

    Hi Roger and walter

    Thanks for the replies. As I am new to this field I am having problems
    in showing the image once I scale to a different level. For example
    if I run the following code,

    A = imread('C:\TestData\lena.tif');
    D = floor(A/16);
    figure; imshow(D);

    A has values upto 255 and D upto 16, but the image shown is a blank
    (black) image. Is there anything else I have to do? Your help is
    greatly appreciated


    gunadp, Jun 9, 2008
  7. gunadp

    ImageAnalyst Guest

    It by default displays the range 0-255 since you probably have a uint8
    image. 16 gray levels is so dark that it's virtually black. Use []
    as the second argument in imshow() to get it to display your 16 gray
    levels with the full range of the display.
    imshow(D, []);
    By the way, it's a good idea to use descriptive variable names in case
    someone else ever has to support your code. For example
    originalImage = imread('C:\TestData\lena.tif');
    reducedGrayLevelImage = floor(originalImage / 16);
    imshow(reducedGrayLevelImage , []);

    P.S. If you see equals3D instead of the equals sign, it's because
    you're using a newsreader that doesn't understand the "quoted
    printable" standard for messages (like the MATLAB web-based newsread.)
    ImageAnalyst, Jun 9, 2008
  8. gunadp

    Dev Guest

    Thanks, it works.
    Dev, Jun 9, 2008
  9. There is no quoted printable standard for Usenet messages. MIME
    standards do not apply to Usenet messages. The original MIME RFC
    documents even say that explicitly.
    Walter Roberson, Jun 9, 2008
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.