前方我們講解了《 FCN-數據篇》。里面包含了如何制作類似pascal voc的label。很大篇幅在談如何著色,如何轉化為索引圖像。 由于一些內容參考網上的資料,所以對里面的一些操作含義也有些糊涂。 其實網上的東西也不都對,很多人云亦云。所以需要我們仔細甄別。 其中我就發現了一個錯誤。我們來從頭談起。
- pascal voc數據集
當我們從網上下載pascal voc2012的數據集,會發現SegmentationClass文件里的標注都是彩色圖像。
但是查看其屬性,發現其位深為8.
一般情況下,彩色圖像都是rgb格式,所以應該是24位,但這里是8位,其實這表明圖像是索引格式。
里面8位存放的是索引值,區間范圍[0,255]。圖像應該還包含一個map,對應著每個索引的顏色。
我們用matlab 查看索引:
>> im=imread('H:\data\VOCtrainval_11-May-2012\VOCdevkit\VOC2012\SegmentationClass\2007_000346.png');
>> info=imfinfo('H:\data\VOCtrainval_11-May-2012\VOCdevkit\VOC2012\SegmentationClass\2007_000346.png');
>> info.Colormapans =0 0 00.5020 0 00 0.5020 00.5020 0.5020 00 0 0.50200.5020 0 0.50200 0.5020 0.50200.5020 0.5020 0.50200.2510 0 00.7529 0 00.2510 0.5020 00.7529 0.5020 00.2510 0 0.50200.7529 0 0.50200.2510 0.5020 0.50200.7529 0.5020 0.50200 0.2510 00.5020 0.2510 00 0.7529 00.5020 0.7529 00 0.2510 0.50200.5020 0.2510 0.50200 0.7529 0.50200.5020 0.7529 0.50200.2510 0.2510 00.7529 0.2510 00.2510 0.7529 00.7529 0.7529 00.2510 0.2510 0.50200.7529 0.2510 0.50200.2510 0.7529 0.50200.7529 0.7529 0.50200 0 0.25100.5020 0 0.25100 0.5020 0.25100.5020 0.5020 0.25100 0 0.75290.5020 0 0.75290 0.5020 0.75290.5020 0.5020 0.75290.2510 0 0.25100.7529 0 0.25100.2510 0.5020 0.25100.7529 0.5020 0.25100.2510 0 0.75290.7529 0 0.75290.2510 0.5020 0.75290.7529 0.5020 0.75290 0.2510 0.25100.5020 0.2510 0.25100 0.7529 0.25100.5020 0.7529 0.25100 0.2510 0.75290.5020 0.2510 0.75290 0.7529 0.75290.5020 0.7529 0.75290.2510 0.2510 0.25100.7529 0.2510 0.25100.2510 0.7529 0.25100.7529 0.7529 0.25100.2510 0.2510 0.75290.7529 0.2510 0.75290.2510 0.7529 0.75290.7529 0.7529 0.75290.1255 0 00.6275 0 00.1255 0.5020 00.6275 0.5020 00.1255 0 0.50200.6275 0 0.50200.1255 0.5020 0.50200.6275 0.5020 0.50200.3765 0 00.8784 0 00.3765 0.5020 00.8784 0.5020 00.3765 0 0.50200.8784 0 0.50200.3765 0.5020 0.50200.8784 0.5020 0.50200.1255 0.2510 00.6275 0.2510 00.1255 0.7529 00.6275 0.7529 00.1255 0.2510 0.50200.6275 0.2510 0.50200.1255 0.7529 0.50200.6275 0.7529 0.50200.3765 0.2510 00.8784 0.2510 00.3765 0.7529 00.8784 0.7529 00.3765 0.2510 0.50200.8784 0.2510 0.50200.3765 0.7529 0.50200.8784 0.7529 0.50200.1255 0 0.25100.6275 0 0.25100.1255 0.5020 0.25100.6275 0.5020 0.25100.1255 0 0.75290.6275 0 0.75290.1255 0.5020 0.75290.6275 0.5020 0.75290.3765 0 0.25100.8784 0 0.25100.3765 0.5020 0.25100.8784 0.5020 0.25100.3765 0 0.75290.8784 0 0.75290.3765 0.5020 0.75290.8784 0.5020 0.75290.1255 0.2510 0.25100.6275 0.2510 0.25100.1255 0.7529 0.25100.6275 0.7529 0.25100.1255 0.2510 0.75290.6275 0.2510 0.75290.1255 0.7529 0.75290.6275 0.7529 0.75290.3765 0.2510 0.25100.8784 0.2510 0.25100.3765 0.7529 0.25100.8784 0.7529 0.25100.3765 0.2510 0.75290.8784 0.2510 0.75290.3765 0.7529 0.75290.8784 0.7529 0.75290 0.1255 00.5020 0.1255 00 0.6275 00.5020 0.6275 00 0.1255 0.50200.5020 0.1255 0.50200 0.6275 0.50200.5020 0.6275 0.50200.2510 0.1255 00.7529 0.1255 00.2510 0.6275 00.7529 0.6275 00.2510 0.1255 0.50200.7529 0.1255 0.50200.2510 0.6275 0.50200.7529 0.6275 0.50200 0.3765 00.5020 0.3765 00 0.8784 00.5020 0.8784 00 0.3765 0.50200.5020 0.3765 0.50200 0.8784 0.50200.5020 0.8784 0.50200.2510 0.3765 00.7529 0.3765 00.2510 0.8784 00.7529 0.8784 00.2510 0.3765 0.50200.7529 0.3765 0.50200.2510 0.8784 0.50200.7529 0.8784 0.50200 0.1255 0.25100.5020 0.1255 0.25100 0.6275 0.25100.5020 0.6275 0.25100 0.1255 0.75290.5020 0.1255 0.75290 0.6275 0.75290.5020 0.6275 0.75290.2510 0.1255 0.25100.7529 0.1255 0.25100.2510 0.6275 0.25100.7529 0.6275 0.25100.2510 0.1255 0.75290.7529 0.1255 0.75290.2510 0.6275 0.75290.7529 0.6275 0.75290 0.3765 0.25100.5020 0.3765 0.25100 0.8784 0.25100.5020 0.8784 0.25100 0.3765 0.75290.5020 0.3765 0.75290 0.8784 0.75290.5020 0.8784 0.75290.2510 0.3765 0.25100.7529 0.3765 0.25100.2510 0.8784 0.25100.7529 0.8784 0.25100.2510 0.3765 0.75290.7529 0.3765 0.75290.2510 0.8784 0.75290.7529 0.8784 0.75290.1255 0.1255 00.6275 0.1255 00.1255 0.6275 00.6275 0.6275 00.1255 0.1255 0.50200.6275 0.1255 0.50200.1255 0.6275 0.50200.6275 0.6275 0.50200.3765 0.1255 00.8784 0.1255 00.3765 0.6275 00.8784 0.6275 00.3765 0.1255 0.50200.8784 0.1255 0.50200.3765 0.6275 0.50200.8784 0.6275 0.50200.1255 0.3765 00.6275 0.3765 00.1255 0.8784 00.6275 0.8784 00.1255 0.3765 0.50200.6275 0.3765 0.50200.1255 0.8784 0.50200.6275 0.8784 0.50200.3765 0.3765 00.8784 0.3765 00.3765 0.8784 00.8784 0.8784 00.3765 0.3765 0.50200.8784 0.3765 0.50200.3765 0.8784 0.50200.8784 0.8784 0.50200.1255 0.1255 0.25100.6275 0.1255 0.25100.1255 0.6275 0.25100.6275 0.6275 0.25100.1255 0.1255 0.75290.6275 0.1255 0.75290.1255 0.6275 0.75290.6275 0.6275 0.75290.3765 0.1255 0.25100.8784 0.1255 0.25100.3765 0.6275 0.25100.8784 0.6275 0.25100.3765 0.1255 0.75290.8784 0.1255 0.75290.3765 0.6275 0.75290.8784 0.6275 0.75290.1255 0.3765 0.25100.6275 0.3765 0.25100.1255 0.8784 0.25100.6275 0.8784 0.25100.1255 0.3765 0.75290.6275 0.3765 0.75290.1255 0.8784 0.75290.6275 0.8784 0.75290.3765 0.3765 0.25100.8784 0.3765 0.25100.3765 0.8784 0.25100.8784 0.8784 0.25100.3765 0.3765 0.75290.8784 0.3765 0.75290.3765 0.8784 0.75290.8784 0.8784 0.7529
或者
[im,map]=imread('H:\data\VOCtrainval_11-May-2012\VOCdevkit\VOC2012\SegmentationClass\2007_000346.png');
如果進一步地想要顯示索引圖像或者轉為rgb,可以:
[cdata,map] = imread( filename ) %讀索引圖像文件
if ~isempty( map ) rgb = ind2rgb( cdata, map ); %將索引圖像數據轉為RGB圖像數據
end
imshow(rgb )
imshow(cdata,map) %這樣讀也可以
根據上面顯示的map,我們發現與《 FCN-數據篇》的pascal voc的colormap完全一致。
另外需要注意:cdata是從0開始,對應著map的第一行。
自定義數據
根據前面《 FCN-數據篇》 的生成
自定義數據的方法,包括:
1. 使用labelme標注圖像,生成灰度圖像
2. 將灰度標簽圖像轉化為rgb 24位圖像,根據pascal voc的colormap,使用函數label2rgb
3. 將24位png圖轉換為8位png圖。生成索引圖像
其中第3步很重要,先前網上的資料有誤。之前的代碼是:
dirs=dir('F:/xxx/*.png');
for n=1:numel(dirs)strname=strcat('F:/xxx/',dirs(n).name);img=imread(strname);[x,map]=rgb2ind(img,256);newname=strcat('F:/xxx/',dirs(n).name);imwrite(x,map,newname,'png');
end
這里也生成了一個map,當時我就很疑惑為什么這里還有一個map呢?
于是試驗了一下:
>> im=imread('G:\deeplearning\FCN_train-master\xxx.png');
>> [a,map2]=rgb2ind(im,256);
>> map2map2 =0 0 00.5020 0 00 0 0.50200.5020 0 0.50200 0.5020 00.5020 0.5020 0
顯然這里的map與pascal voc的map不一致了,會導致很嚴重的問題。
因此正確的是:
>> im=imread('G:\deeplearning\FCN_train-master\xxx.png');
>> map=labelcolormap(256);
>> x=rgb2ind(im,map);
>> imshow(x,map)
>> imwrite(x,map,'test.png','png')
其中第二步就是生成pascal voc的colormap,可以在《 FCN-數據篇》 查找。
二分類標簽數據的制作
對于二分類問題而言,但我們標注好了圖像語義后,我們常常得到的是黑白灰度圖像。比如前景部分用255顯示,背景用0顯示。而且有可能24位,也可能是8位。
這時我們需要做兩件事:
1. 將圖像轉化為24位
2. 生成8位索引圖像
代碼如下:
root='H:\data\IrsData\iris_ground-truth\MICHE_subset\';
input_dir=strcat(root,'ground truth\');
output_dir=strcat(root,'temp\');
src_type='tiff';
files = dir([input_dir, '*.', src_type]);
n = length(files);for i = 1:n [filename, type] = strtok(files(i).name, '.');im_src = imread([input_dir, files(i).name]);info=imfinfo([input_dir, files(i).name]);if info.BitDepth==8 %轉化為24位rgbim_src=cat(3,im_src,im_src,im_src);end[x,map]=rgb2ind(im_src,2);newname=strcat(output_dir,filename,'.png');imwrite(x,map,newname,'png');end
至此生成的圖像時索引圖像,二分類,前景為白色,背景為黑色,索引值為0,1.