分割数据集的正常的数据增强方式:对原始数据和ground-truth做相同的增强变换,代码实现如下。
对于原始数据进行增强的代码:my_Augment_data.m
%图像的水平翻转和垂直翻转
clear;clc
% file_path = './data1/JPEGImages/';% 图像文件夹路径
% new_file_path = './aug1/JPEGImages/';
file_path = '~/dataset/mydatababy/full-VOC/JPEGImages/';% 图像文件夹路径
new_file_path = '~/dataset/mydatababy/full-VOC-new/JPEGImages/';
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
img_num
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name(1:end-4);% 图像名
image_name
image = imread(strcat(file_path,image_name,'.jpg'));
imageBB = image;
imageB = image;
for k=1:3
imageBB(:,:,k)=flipud(image(:,:,k));%上下翻转
imageB(:,:,k)=fliplr(image(:,:,k));%左右翻转
end
% subplot(1,3,1);
% imshow(image);
% title('yuan')
%
% subplot(1,3,2);
% imshow(imageBB);
% title('up-down')
%
% subplot(1,3,3);
% imshow(imageB);
% title('left-right')
%
file_name1 = strcat(new_file_path,image_name,'.jpg');
imwrite(image,file_name1);
new_file_name2 = strcat(new_file_path,image_name,'_2.jpg');
imwrite(imageBB,new_file_name2);
new_file_name3 = strcat(new_file_path,image_name,'_3.jpg');
imwrite(imageB,new_file_name3);
imageI1=rot90(image,1);%逆时针转90度
imageI2=rot90(image,2);%逆时针转180度
imageI3=rot90(image,3);%逆时针转270度
new_file_name4 = strcat(new_file_path,image_name,'_4.jpg');
imwrite(imageI1,new_file_name4);
new_file_name5 = strcat(new_file_path,image_name,'_5.jpg');
imwrite(imageI2,new_file_name5);
new_file_name6 = strcat(new_file_path,image_name,'_6.jpg');
imwrite(imageI3,new_file_name6);
end
end
%
% AA=imread('test/label24/2018_010001.png');%文件名是你自己的哦
%
%
% figure;
%
% subplot(1,3,1);
% imshow(AA);
% title('yuan')
%
% subplot(1,3,2);
% imshow(BB);
% title('up-down')
%
% subplot(1,3,3);
% imshow(B);
% title('left-right')
%
%
% I=imread('test/label24/2018_010001.png');
% I1=rot90(I,1);%逆时针转90度
% I2=rot90(I,2);%逆时针转180度
% I3=rot90(I,3);%逆时针转270度
% figure
% subplot(2,2,1);
% imshow(I);
% title('原图')
% subplot(2,2,2);
% imshow(I1)
% title('逆时针转90度')
% subplot(2,2,3);
% imshow(I2);
% title('逆时针转180度')
% subplot(2,2,4);
% imshow(I3);
% title('逆时针转270度')
对于ground-truth进行数据增强的代码:my_Augment_labels.m
%图像的水平翻转和垂直翻转
clear;clc
%file_path = './data/JPEGImages/';% 图像文件夹路径
%new_file_path = './aug/JPEGImages/';
% file_path = './data1/SegmentationClass/';% 图像文件夹路径 %##labels
% new_file_path = './aug1/SegmentationClass/';
file_path = '~/dataset/mydatababy/full-VOC/SegmentationClass/';% 图像文件夹路径 %##labels
new_file_path = '~/dataset/mydatababy/full-VOC-new/SegmentationClass/';
%img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_path_list = dir(strcat(file_path,'*.png'));%获取该文件夹中所有jpg格式的图像 %##labels
img_num = length(img_path_list);%获取图像总数量
img_num
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name(1:end-4);% 图像名
image_name
%image = imread(strcat(file_path,image_name,'.jpg'));
image = imread(strcat(file_path,image_name,'.png')); %##labels
imageBB = image;
imageB = image;
% for k=1:3
% imageBB(:,:,k)=flipud(image(:,:,k));%上下翻转
% imageB(:,:,k)=fliplr(image(:,:,k));%左右翻转
% end
imageBB(:,:,1)=flipud(image(:,:,1));%上下翻转 %##labels
imageB(:,:,1)=fliplr(image(:,:,1));%左右翻转
% subplot(1,3,1);
% imshow(image);
% title('yuan')
%
% subplot(1,3,2);
% imshow(imageBB);
% title('up-down')
%
% subplot(1,3,3);
% imshow(imageB);
% title('left-right')
%
% file_name1 = strcat(new_file_path,image_name,'.jpg');
% imwrite(image,file_name1);
%
% new_file_name2 = strcat(new_file_path,image_name,'_2.jpg');
% imwrite(imageBB,new_file_name2);
%
% new_file_name3 = strcat(new_file_path,image_name,'_3.jpg');
% imwrite(imageB,new_file_name3);
file_name1 = strcat(new_file_path,image_name,'.png'); %##labels
imwrite(image,file_name1);
new_file_name2 = strcat(new_file_path,image_name,'_2.png');
imwrite(imageBB,new_file_name2);
new_file_name3 = strcat(new_file_path,image_name,'_3.png');
imwrite(imageB,new_file_name3);
imageI1=rot90(image,1);%逆时针转90度
imageI2=rot90(image,2);%逆时针转180度
imageI3=rot90(image,3);%逆时针转270度
% new_file_name4 = strcat(new_file_path,image_name,'_4.jpg');
% imwrite(imageI1,new_file_name4);
%
% new_file_name5 = strcat(new_file_path,image_name,'_5.jpg');
% imwrite(imageI2,new_file_name5);
%
% new_file_name6 = strcat(new_file_path,image_name,'_6.jpg');
% imwrite(imageI3,new_file_name6);
new_file_name4 = strcat(new_file_path,image_name,'_4.png'); %##labels
imwrite(imageI1,new_file_name4);
new_file_name5 = strcat(new_file_path,image_name,'_5.png');
imwrite(imageI2,new_file_name5);
new_file_name6 = strcat(new_file_path,image_name,'_6.png');
imwrite(imageI3,new_file_name6);
end
end
由于要加入噪声从而对数据进行增强,所以按照以下流程制作数据集,数据增强方式如下:
1.将1198张原始图像和label随机分为训练集文件夹(959张)和测试集文件夹(239张);同时,把对应的ground-truth分为训练文件夹和测试文件夹。
2.对原始图像采用8种方式进行数据增强,包括加入4种噪声,4种亮度调节;同时,把对应的ground-truth进行复制进行增强。
3.根据分成的训练和测试文件夹,生成train.txt和val.txt文件。
4.手动把训练集和测试集合并为一个文件夹作为原始数据集;同时,把ground-truth合并为一个文件夹。
5.根据合并后的文件夹,生成trainval.txt文件。
6.用增强后的959×9=8631张数据集作为训练集,用原始分离出的239张测试集作为最终的测试集,进行训练和测试。
my_split.m
clear all;
clc;
%step1: split to test and trian datasets
file_path = '~/dataset/mydata0611/full-VOC/JPEGImages/';%image path
dst_test_path = '~/dataset/mydata0611/full-VOC/JPEGTest/'; %dst_test path
dst_train_path = '~/dataset/mydata0611/full-VOC/JPEGTrain/'; %dst_train path
img_path_list = dir(strcat(file_path,'*.jpg'));%get jpg-image list
img_num = length(img_path_list);%get jpg-image number
for i = 1:img_num
name(i,1) = {img_path_list(i).name(1:end-4)};
end
image_orign = name; %get the origin name of image
len2 = img_num;%get the origin_image number
index = randperm(len2); %get the random index
test_num = floor(len2*0.2);% 0.2 as the test datasets
train_num = (len2 - test_num);% 0.8 as the train datasets
trainval_num = len2; %total number
test_index = image_orign(index(1:test_num),:); %set the index as the test datasets
train_index = image_orign(index(test_num+1:test_num+train_num),:); %set the index as the train datasets
trainval_index = image_orign(index(1:end),:);%set the index as the all datasets
for i = 1:length(test_index)
%trainval_index{1} %get the data from cell
imagename = [file_path,test_index{i},'.jpg'];
disp(imagename);
movefile(imagename,dst_test_path);
end
for i = 1:length(train_index)
%trainval_index{1} %get the data from cell
imagename = [file_path,train_index{i},'.jpg'];
disp(imagename);
movefile(imagename,dst_train_path); %move file to different folder
end
%copyfile(imagename,dst_train_path); %copy file to different folder
label_path = '~/dataset/mydata0611/full-VOC/SegmentationClass/';%image path
dst_label_test_path = '~/dataset/mydata0611/full-VOC/SegmentationTest/'; %dst_test path
dst_label_train_path = '~/dataset/mydata0611/full-VOC/SegmentationTrain/'; %dst_train path
for i = 1:length(test_index)
%trainval_index{1} %get the data from cell
imagename = [label_path,test_index{i},'.png'];
disp(imagename);
movefile(imagename,dst_label_test_path);
end
for i = 1:length(train_index)
%trainval_index{1} %get the data from cell
imagename = [label_path,train_index{i},'.png'];
disp(imagename);
movefile(imagename,dst_label_train_path); %move file to different folder
end
% fid1=fopen('D:\temppic\data\full-VOC\ImageSets\Segmentation\trainval.txt','w');
% for i = 1:length(trainval_index)
% %trainval_index{1} %get the data from cell
% fprintf(fid1,'%s\n',trainval_index{i});%
% end
% fclose(fid1);
%
% fid2=fopen('D:\temppic\data\full-VOC\ImageSets\Segmentation\train.txt','w');
% for i = 1:length(train_index)
% fprintf(fid1,'%s\n',train_index{i});%
% end
% fclose(fid2);
%
% fid3=fopen('D:\temppic\data\full-VOC\ImageSets\Segmentation\val.txt','w');
% for i = 1:length(test_index)
% fprintf(fid1,'%s\n',test_index{i});%
% end
% fclose(fid3);
my_Augment_noise_data.m
close all;
clear all;
clc;
%step2_1:
%augment image use noise
%8 methods: 4 noise, 4 light
file_path = '~/dataset/mydata0611/full-VOC/JPEGTrain/';% 图像文件夹路径
new_file_path = '~/dataset/mydata0611/full-VOC/JPEGTrain/';
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
img_num
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name(1:end-4);% 图像名
image_name
image = imread(strcat(file_path,image_name,'.jpg'));
file_name_yuan = strcat(new_file_path,image_name,'.jpg');
imwrite(image,file_name_yuan);
image_gaussian = imnoise(image, 'gaussian');
file_name_gaussian = strcat(new_file_path,image_name,'_gaussian.jpg');
imwrite(image_gaussian,file_name_gaussian);
image_saltpepper = imnoise(image, 'salt & pepper');
file_name_saltpepper = strcat(new_file_path,image_name,'_saltpepper.jpg');
imwrite(image_saltpepper,file_name_saltpepper);
image_speckle = imnoise(image, 'speckle');
file_name_speckle = strcat(new_file_path,image_name,'_speckle.jpg');
imwrite(image_speckle,file_name_speckle);
image_poisson = imnoise(image, 'poisson');
file_name_poisson = strcat(new_file_path,image_name,'_poisson.jpg');
imwrite(image_poisson,file_name_poisson);
image_stretch = intrans(image, 'stretch', mean2(im2double(image)),0.9);
image_stretch_1 = im2uint8(image_stretch);
file_name_stretch = strcat(new_file_path,image_name,'_stretch.jpg');
imwrite(image_stretch_1,file_name_stretch);
image_gscale = gscale(image, 'full8');
file_name_gscale = strcat(new_file_path,image_name,'_gscale.jpg');
imwrite(image_gscale,file_name_gscale);
image_imadjust1 = imadjust(image, [0 1], [0 0.75]);
file_name_imadjust1 = strcat(new_file_path,image_name,'_imadjust1.jpg');
imwrite(image_imadjust1,file_name_imadjust1);
image_imadjust2 = imadjust(image, [0 1], [0.5 1]);
file_name_imadjust2 = strcat(new_file_path,image_name,'_imadjust2.jpg');
imwrite(image_imadjust2,file_name_imadjust2);
end
end
% AA=imread('~/dataset/mydata0611/full-VOC/JPEGTrain/2018_000258.jpg');%文件名是你自己的哦
%
% figure;
%
% subplot(1,3,1);
% imshow(AA);
% title('yuan')
%
%
% BB = imnoise(AA, 'gaussian');
% subplot(1,3,2);
% imshow(BB);
% title('gaussian')
%
% % B = imnoise(AA, 'localvar');
% % subplot(1,3,3);
% % imshow(B);
% % title('localvar')
%
%
% figure;
%
% BB1 = imnoise(AA, 'salt & pepper');
% subplot(1,3,1);
% imshow(BB1);
% title('salt & pepper')
%
% B1 = imnoise(AA, 'speckle');
% subplot(1,3,2);
% imshow(B1);
% title('speckle')
%
%
% B2 = imnoise(AA, 'poisson');
% subplot(1,3,3);
% imshow(B2);
% title('poisson')
%
%
% figure;
%
% LL1 = imadjust(AA, [0 1], [1 0]); %fanzhuan method1 %buxuan
% subplot(1,3,1);
% imshow(LL1);
% title('imadjust')
%
% % LL3 = imcomplement(AA);%fanzhuan method2
% % subplot(1,3,3);
% % imshow(LL3);
% % title('imcomplement')
%
%
%
% % LL4 = im2bw(LL1,0.55);%erzhihua
% % subplot(1,3,2);
% % imshow(LL4);
%
%
%
%
% LL2 = intrans(AA, 'stretch', mean2(im2double(AA)),0.9);
% subplot(1,3,2);
% imshow(LL2);
% title('intrans')
% LL2_1 = im2uint8(LL2);
%
%
% LL3 = gscale(AA, 'full8');
% subplot(1,3,3);
% imshow(LL3);
% title('gscale')
%
%
% figure;
%
% imadjust1 = imadjust(AA, [0.3 0.75], [0 1]); %buxuan
% subplot(1,3,1);
% imshow(imadjust1);
% title('imadjust1')
%
% imadjust2 = imadjust(AA, [0 1], [0 0.75]);
% subplot(1,3,2);
% imshow(imadjust2);
% title('imadjust2')
%
%
% imadjust3 = imadjust(AA, [0 1], [0.5 1]);
% subplot(1,3,3);
% imshow(imadjust3);
% title('imadjust3')
%
% figure;
% imshow(LL2_1);
my_Augment_noise_labels.m
close all;
clear all;
clc;
%step2_2:
%augment label
%8 same
file_path = '~/dataset/mydata0611/full-VOC/SegmentationTrain/';% 图像文件夹路径
new_file_path = '~/dataset/mydata0611/full-VOC/SegmentationTrain/';
img_path_list = dir(strcat(file_path,'*.png'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
img_num
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name(1:end-4);% 图像名
image_name
image = imread(strcat(file_path,image_name,'.png'));
file_name_yuan = strcat(new_file_path,image_name,'.png');
imwrite(image,file_name_yuan);
%image_gaussian = imnoise(image, 'gaussian');
file_name_gaussian = strcat(new_file_path,image_name,'_gaussian.png');
imwrite(image,file_name_gaussian);
%image_saltpepper = imnoise(image, 'salt & pepper');
file_name_saltpepper = strcat(new_file_path,image_name,'_saltpepper.png');
imwrite(image,file_name_saltpepper);
%image_speckle = imnoise(image, 'speckle');
file_name_speckle = strcat(new_file_path,image_name,'_speckle.png');
imwrite(image,file_name_speckle);
%image_poisson = imnoise(image, 'poisson');
file_name_poisson = strcat(new_file_path,image_name,'_poisson.png');
imwrite(image,file_name_poisson);
%image_stretch = intrans(image, 'stretch', mean2(im2double(image)),0.9);
%image_stretch_1 = im2uint8(image_stretch);
file_name_stretch = strcat(new_file_path,image_name,'_stretch.png');
imwrite(image,file_name_stretch);
%image_gscale = gscale(image, 'full8');
file_name_gscale = strcat(new_file_path,image_name,'_gscale.png');
imwrite(image,file_name_gscale);
%image_imadjust1 = imadjust(image, [0 1], [0 0.75]);
file_name_imadjust1 = strcat(new_file_path,image_name,'_imadjust1.png');
imwrite(image,file_name_imadjust1);
%image_imadjust2 = imadjust(image, [0 1], [0.5 1]);
file_name_imadjust2 = strcat(new_file_path,image_name,'_imadjust2.png');
imwrite(image,file_name_imadjust2);
end
end
% AA=imread('~/dataset/mydata0611/full-VOC/JPEGTrain/2018_000258.jpg');%文件名是你自己的哦
%
% figure;
%
% subplot(1,3,1);
% imshow(AA);
% title('yuan')
%
%
% BB = imnoise(AA, 'gaussian');
% subplot(1,3,2);
% imshow(BB);
% title('gaussian')
%
% % B = imnoise(AA, 'localvar');
% % subplot(1,3,3);
% % imshow(B);
% % title('localvar')
%
%
% figure;
%
% BB1 = imnoise(AA, 'salt & pepper');
% subplot(1,3,1);
% imshow(BB1);
% title('salt & pepper')
%
% B1 = imnoise(AA, 'speckle');
% subplot(1,3,2);
% imshow(B1);
% title('speckle')
%
%
% B2 = imnoise(AA, 'poisson');
% subplot(1,3,3);
% imshow(B2);
% title('poisson')
%
%
% figure;
%
% LL1 = imadjust(AA, [0 1], [1 0]); %fanzhuan method1 %buxuan
% subplot(1,3,1);
% imshow(LL1);
% title('imadjust')
%
% % LL3 = imcomplement(AA);%fanzhuan method2
% % subplot(1,3,3);
% % imshow(LL3);
% % title('imcomplement')
%
%
%
% % LL4 = im2bw(LL1,0.55);%erzhihua
% % subplot(1,3,2);
% % imshow(LL4);
%
%
%
%
% LL2 = intrans(AA, 'stretch', mean2(im2double(AA)),0.9);
% subplot(1,3,2);
% imshow(LL2);
% title('intrans')
% LL2_1 = im2uint8(LL2);
%
%
% LL3 = gscale(AA, 'full8');
% subplot(1,3,3);
% imshow(LL3);
% title('gscale')
%
%
% figure;
%
% imadjust1 = imadjust(AA, [0.3 0.75], [0 1]); %buxuan
% subplot(1,3,1);
% imshow(imadjust1);
% title('imadjust1')
%
% imadjust2 = imadjust(AA, [0 1], [0 0.75]);
% subplot(1,3,2);
% imshow(imadjust2);
% title('imadjust2')
%
%
% imadjust3 = imadjust(AA, [0 1], [0.5 1]);
% subplot(1,3,3);
% imshow(imadjust3);
% title('imadjust3')
%
% figure;
% imshow(LL2_1);
make_files_val_and_train.m
clear all;
clc;
%step3: make train.txt and val.txt
file_train_path = '~/dataset/mydata0611/full-VOC/JPEGTrain/';% 图像文件夹路径 Train
img_train_path_list = dir(strcat(file_train_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_train_num = length(img_train_path_list);%获取图像总数量
file_test_path = '~/dataset/mydata0611/full-VOC/JPEGTest/';% 图像文件夹路径
img_test_path_list = dir(strcat(file_test_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_test_num = length(img_test_path_list);%获取图像总数量
%fid1=fopen('~/dataset/mydata0611/full-VOC/ImageSets/Segmentation/trainval.txt','w');
fid2=fopen('~/dataset/mydata0611/full-VOC/ImageSets/Segmentation/train.txt','w');
fid3=fopen('~/dataset/mydata0611/full-VOC/ImageSets/Segmentation/val.txt','w');
if img_train_num > 0 %有满足条件的图像
for j = 1:img_train_num %逐一读取图像
image_name = img_train_path_list(j).name(1:end-4);% 图像名
%image = imread(strcat(file_path,image_name,'.jpg'));
fprintf(fid2,'%s\n',image_name);% 显示正在处理的图像名
%图像处理过程 省略
%imshow(image)
end
fclose(fid2);
end
if img_test_num > 0 %有满足条件的图像
for j = 1:img_test_num %逐一读取图像
image_name = img_test_path_list(j).name(1:end-4);% 图像名
%image = imread(strcat(file_path,image_name,'.jpg'));
fprintf(fid3,'%s\n',image_name);% 显示正在处理的图像名
%图像处理过程 省略
%imshow(image)
end
fclose(fid3);
end
make_files_trainval.m
clear all;
clc;
%step5: make trainval.txt
file_trainval_path = '~/dataset/mydata0611/full-VOC/JPEGImages/';% 图像文件夹路径 Train
img_trainval_path_list = dir(strcat(file_trainval_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_trainval_num = length(img_trainval_path_list);%获取图像总数量
fid1=fopen('~/dataset/mydata0611/full-VOC/ImageSets/Segmentation/trainval.txt','w');
if img_trainval_num > 0 %有满足条件的图像
for j = 1:img_trainval_num %逐一读取图像
image_name = img_trainval_path_list(j).name(1:end-4);% 图像名
%image = imread(strcat(file_path,image_name,'.jpg'));
fprintf(fid1,'%s\n',image_name);% 显示正在处理的图像名
%图像处理过程 省略
%imshow(image)
end
fclose(fid1);
end
特别地,对于加入噪声和调节亮度的代码中,有两个函数gscale.m和intrans.m,并不是matlab的内置函数,特地附上代码,只需要将该函数文件和调用函数的.m文件置于同一个目录下即可正常调用。
gscale.m
function g=gscale(f,varargin)
if length(varargin)==0
method='full8';
else method=varargin{1};
end
if strcmp(class(f),'double')&(max(f(:))>1 | min(f(:))<0)
f=mat2gray(f);
end
switch method
case 'full8'
g=im2uint8(mat2gray(double(f)));
case 'full16'
g=im2uint16(mat2gray(double(f)));
case 'minmax'
low = varargin{2};high = varargin{3};
if low>1 | low<0 |high>1 | high<0
error('Parameters low and high must be in the range [0,1]')
end
if strcmp(class(f),'double')
low_in=min(f(:));
high_in=max(f(:));
elseif strcmp(class(f),'uint8')
low_in=double(min(f(:)))./255;
high_in=double(max(f(:)))./255;
elseif strcmp(class(f),'uint16')
low_in=double(min(f(:)))./65535;
high_in=double(max(f(:)))./65535;
end
g=imadjust(f,[low_in high_in],[low high]);
otherwise
error('Unknown method')
end
intrans.m
function g = intrans(f, varargin)
%INTRANS Performs intensity (gray-level) transformations.
% G = INTRANS(F, 'neg') computes the negative of input image F.
%
% G = INTRANS(F, 'log', C, CLASS) computes C*log(1 + F) and
% multiplies the result by (positive) constant C. If the last two
% parameters are omitted, C defaults to 1. Because the log is used
% frequently to display Fourier spectra, parameter CLASS offers the
% option to specify the class of the output as 'uint8' or
% 'uint16'. If parameter CLASS is omitted, the output is of the
% same class as the input.
%
% G = INTRANS(F, 'gamma', GAM) performs a gamma transformation on
% the input image using parameter GAM (a required input).
%
% G = INTRANS(F, 'stretch', M, E) computes a contrast-stretching
% transformation using the expression 1./(1 + (M./(F +
% eps)).^E). Parameter M must be in the range [0, 1]. The default
% value for M is mean2(im2double(F)), and the default value for E
% is 4.
%
% For the 'neg', 'gamma', and 'stretch' transformations, double
% input images whose maximum value is greater than 1 are scaled
% first using MAT2GRAY. Other images are converted to double first
% using IM2DOUBLE. For the 'log' transformation, double images are
% transformed without being scaled; other images are converted to
% double first using IM2DOUBLE.
%
% The output is of the same class as the input, except if a
% different class is specified for the 'log' option.
% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image Processing Using MATLAB, Prentice-Hall, 2004
% $Revision: 1.7 $ $Date: 2003/10/13 00:45:53 $
% Verify the correct number of inputs.
error(nargchk(2, 4, nargin))
% Store the class of the input for use later.
classin = class(f);
% If the input is of class double, and it is outside the range
% [0, 1], and the specified transformation is not 'log', convert the
% input to the range [0, 1].
if strcmp(class(f), 'double') & max(f(:)) > 1 & ...
~strcmp(varargin{1}, 'log')
f = mat2gray(f);
else % Convert to double, regardless of class(f).
f = im2double(f);
end
% Determine the type of transformation specified.
method = varargin{1};
% Perform the intensity transformation specified.
switch method
case 'neg'
g = imcomplement(f);
case 'log'
if length(varargin) == 1
c = 1;
elseif length(varargin) == 2
c = varargin{2};
elseif length(varargin) == 3
c = varargin{2};
classin = varargin{3};
else
error('Incorrect number of inputs for the log option.')
end
g = c*(log(1 + double(f)));
case 'gamma'
if length(varargin) < 2
error('Not enough inputs for the gamma option.')
end
gam = varargin{2};
g = imadjust(f, [ ], [ ], gam);
case 'stretch'
if length(varargin) == 1
% Use defaults.
m = mean2(f);
E = 4.0;
elseif length(varargin) == 3
m = varargin{2};
E = varargin{3};
else error('Incorrect number of inputs for the stretch option.')
end
g = 1./(1 + (m./(f + eps)).^E);
otherwise
error('Unknown enhancement method.')
end
% Convert to the class of the input image.
%g = changeclass(classin, g);