Search code examples
imagematlabeditorbits-per-pixel

changing bits per pixel in MATLAB


How does one change the bits per pixel of an image loaded into MATLAB? I use the file dialog and the imread functions to load the image into a matrix. i just need to change that image's bits per pixel. Giving the user that ability to choose anywhere from 1 bit to 8 bits. I know how to give the users the ability to choose one I just don't know who to change it. How does one change that? (By the way I'm in MATLAB R2012a)


Solution

  • The way I understand it, you want to do something like this:

    imdata = rgb2gray(imread('ngc6543a.jpg') );  % Assuming that we have a grayscale uint8 image
    figure('name', 'Before');
    imagesc(imdata);
    colormap('gray');
    
    numberOfBits = input('Enter number of bits:\n');
    maxValue = 2^numberOfBits - 1;
    
    newImage = imdata * (maxValue / 256);
    figure('name', 'After');
    imagesc(newImage);
    colormap('gray');
    

    The image ngc6543a.jpg is a sample image, so you can run this code immediately as it is.