문제

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)

도움이 되었습니까?

해결책

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.

다른 팁

This documentation page contains lots of information about what you want to do: Reducing the Number of Colors in an Image.

A simple example is the following (pretty much taken straight from that page), which will dither the image and produce a colour map (slightly different to the OP's answer - not sure which one you want to do):

>> RGB = imread('peppers.png');
>> [x,map] = rgb2ind(RGB, 2); % Reduce to a 2-colour image
>> imagesc(x)
>> colormap(map)

You should choose the number of colours based on the maximum number that however many bits can hold.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top