📄 tif2bjpeg.m
字号:
function fsize=tif2bjpeg(filename, fileOut, quantLevels, ... dilateAmount, targetSize)%% fsize=tif2bjpeg(filename, quantLevels, dilateAmount)%% Uses the image in TIFF file to create a BJPEG file (edge enhanced% JPEG file). QuantLevels are the number of quantization levels to% use on the edge error information, and dilateAmount is the the% amount of dilation to use when generating the edge mask. targetSize% is the size the total JPEG+mask image should take when stored.% fileOut is the name to save the rendered BJPEG file in (JPEG at% highest quality).%% load the good image[rImage gImage bImage] = tiffread(filename);% display the imagedisplayImage(rImage, gImage, bImage);% Generate the Y plane of the imageyImage = 0.2990*rImage + 0.5870*gImage + 0.1140*bImage;% display the Y planefigure;displayImage(yImage);% get the edge image and dilatebwEdgeImage=edge(yImage, 'prewitt');seMatrix=ones(dilateAmount);bwEdgeImage=dilate(bwEdgeImage, seMatrix);figure;displayImage(double(bwEdgeImage));% loop until we get our file size below the targetcontinue=1;acc=0;fsize=targetSize;while continue % create the bad image jpSize=createJpeg(fileOut, rImage, gImage, bImage, fsize); [rImageBad gImageBad bImageBad] = jpgread(fileOut); % Generate the Y, Cr and Cb planes of the bad image yImageBad = 0.2990*rImageBad + 0.5870*gImageBad + 0.1140*bImageBad; cbImageBad = -0.1687*rImageBad - 0.3313*gImageBad + 0.5000*bImageBad; crImageBad = 0.5000*rImageBad - 0.4187*gImageBad - 0.0813*bImageBad; % the error between good and bad Y planes in edge regions edgeInfoErrorImage=double(bwEdgeImage).*(yImage-yImageBad); % find the mask size mSize=maskSize(edgeInfoErrorImage, quantLevels); fsize=mSize+jpSize targetSize % continue if total file size still is too big if fsize < targetSize continue=0; else fsize=targetSize-mSize-acc; acc=acc+250; end; end;% display the bad imagefigure;displayImage(rImageBad, gImageBad, bImageBad);% display the bad Y planefigure;displayImage(yImageBad);% quantize the edge erroredgeInfoErrorImage=round((quantLevels-1)*((edgeInfoErrorImage+1)/2));edgeInfoErrorImage=(edgeInfoErrorImage/(quantLevels-1))*2-1;edgeInfoErrorImage=edgeInfoErrorImage.*double(bwEdgeImage);% now add in our error Y image stuffyImageNew=edgeInfoErrorImage+yImageBad;figure;displayImage(yImageNew);% combine new yImage back with color planesrImageNew = truncate(yImageNew + 1.4020*crImageBad,0,1);gImageNew = truncate(yImageNew - 0.3441*cbImageBad - 0.7141*crImageBad,0,1);bImageNew = truncate(yImageNew + 1.7720*cbImageBad - 0.0001*crImageBad,0,1);% display new imagefigure;displayImage(rImageNew, gImageNew, bImageNew);jpgwrite(fileOut, rImageNew, gImageNew, bImageNew, 100);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -