2012-10-04 125 views
1

我想要使用Matlab将8位图像的位深度更改为4位2位深度。 源图像是8位和jpg文件。我想利用png'BitDepth'参数,所以首先我试图将图像转换为png格式。然后我试着使用这个参数;但我收到错误。如果有一个使用Java库的简单解决方案,对我来说也是可以的。使用matlab更改图像位深度

function [] = changeBitDepth(path, depth) 
clear all; close all; 
clc; 
A = imread(path); 
imshow(A); 
imwrite(A, '~/Desktop/football.png'); 
B = imread('~/Desktop/football.png'); 
imwrite(B, '~/Desktop/bitDepthChanged.png', 'BitDepth', depth); 
imfinfo('~/Desktop/bitDepthChanged.png'); 

回答

4

那是因为你只能有8或16位图像的标准彩色图像。您可以为例如不同的位深度索引图像或灰度图像(wiki description of png)。

索引彩色PNG允许标准的每像素有1,2,4或8位;无alpha通道的灰度图像允许每个像素有1,2,4,8或16位。一切使用每8或16

通道位深度你可以做这样的事情:

% convert to indexed image 
[IND,map] = rgb2ind(A,32); 
% save indexed png 
imwrite(IND, map, 'test.png', 'bitdepth', 4); 

here知道如何用MATLAB索引图像的交易。