稀疏表示字典的顯示 稀疏表示字典的顯示(MATLAB實現代碼)

稀疏表示字典的顯示(MATLAB實現代碼)

本文主要是實現論文--基於稀疏表示的圖像超分辨率《Image Super-Resolution Via Sparse Representation》中的Figure2,通過對100000個高分辨率和低分辨率圖像塊訓練得到的高分辨率圖像塊字典,字典原子總數爲512,圖像塊尺寸大小爲9X9


方法一:

  1. clc;
  2. clear all;
  3. % load dictionary
  4. load('Dictionary/D_512_0.15_9.mat');
  5. patch_size=9;
  6. nRow=24;
  7. nCol=22;
  8. D=Dh';
  9. w=nCol*patch_size;
  10. h=nRow*patch_size;
  11. gridx = 1:patch_size :w;
  12. gridx = [gridx, w-patch_size+1];
  13. gridy = 1:patch_size : h;
  14. gridy = [gridy, h-patch_size+1];
  15. K=512; %字典原子總數
  16. DD=cell(1,K);
  17. row=length(gridx);
  18. col=length(gridy);
  19. hIm=zeros([w,h]);
  20. for i=1:K
  21. DD{i}=D(i,:);
  22. end
  23. for ii = 1:length(gridx),
  24. for jj = 1:length(gridy),
  25. yy = gridx(ii);
  26. xx = gridy(jj);
  27. if (ii-1)*nRow+jj >K
  28. break
  29. end
  30. temp=DD{(ii-1)*nCol+jj};
  31. hPatch=reshape(temp,[patch_size,patch_size]);
  32. hIm(yy:yy+patch_size-1, xx:xx+patch_size-1) = hIm(yy:yy+patch_size-1, xx:xx+patch_size-1) +hPatch;
  33. end
  34. end
  35. figure;
  36. imagesc(hIm);
  37. colormap(gray);
  38. axis image;


輸出結果:



可以看出,相比於論文中字典的顯示圖,顏色有點淺,通過調節MATLAB的colorbar,可以將背景顏色變深,

結果如下圖所示:





方法二:

通過http://www.cs.technion.ac.il/~elad/software/提供的ksvd工具箱中的displayDictionaryElementsAsImage( )函數,來實現字典的顯示。

displayDictionaryElementsAsImage.m

  1. function I = displayDictionaryElementsAsImage2(D, numRows, numCols,X,Y,sortVarFlag)
  2. % function I = displayDictionaryElementsAsImage(D, numRows, numCols, X,Y)
  3. % displays the dictionary atoms as blocks. For activation, the dictionary D
  4. % should be given, as also the number of rows (numRows) and columns
  5. % (numCols) for the atoms to be displayed. X and Y are the dimensions of
  6. % each atom.
  7. borderSize = 1;
  8. columnScanFlag = 1;
  9. strechEachVecFlag = 1;
  10. showImFlag = 1;
  11. if (length(who('X'))==0)
  12. X = 8;
  13. Y = 8;
  14. end
  15. if (length(who('sortVarFlag'))==0)
  16. sortVarFlag = 1;
  17. end
  18. numElems = size(D,2);
  19. if (length(who('numRows'))==0)
  20. numRows = floor(sqrt(numElems));
  21. numCols = numRows;
  22. end
  23. if (length(who('strechEachVecFlag'))==0)
  24. strechEachVecFlag = 0;
  25. end
  26. if (length(who('showImFlag'))==0)
  27. showImFlag = 1;
  28. end
  29. %%% sort the elements, if necessary.
  30. %%% construct the image to display (I)
  31. sizeForEachImage = sqrt(size(D,1))+borderSize;
  32. I = zeros(sizeForEachImage*numRows+borderSize,sizeForEachImage*numCols+borderSize,3);
  33. %%% fill all this image in blue
  34. I(:,:,1) = 1; %min(min(D));
  35. I(:,:,2) = 1; %min(min(D));
  36. I(:,:,3) = 1; %max(max(D));
  37. % %%% fill all this image in blue
  38. % I(:,:,1) = 0; %min(min(D));
  39. % I(:,:,2) = 0; %min(min(D));
  40. % I(:,:,3) = 1; %max(max(D));
  41. %%% now fill the image squares with the elements (in row scan or column
  42. %%% scan).
  43. if (strechEachVecFlag)
  44. for counter = 1:size(D,2)
  45. D(:,counter) = D(:,counter)-min(D(:,counter));
  46. if (max(D(:,counter)))
  47. D(:,counter) = D(:,counter)./max(D(:,counter));
  48. end
  49. end
  50. end
  51. if (sortVarFlag)
  52. vars = var(D);
  53. [V,indices] = sort(vars');
  54. indices = fliplr(indices);
  55. D = [D(:,1:sortVarFlag-1),D(:,indices+sortVarFlag-1)];
  56. signs = sign(D(1,:));
  57. signs(find(signs==0)) = 1;
  58. D = D.*repmat(signs,size(D,1),1);
  59. D = D(:,1:numRows*numCols);
  60. end
  61. counter=1;
  62. for j = 1:numRows
  63. for i = 1:numCols
  64. % if (strechEachVecFlag)
  65. % D(:,counter) = D(:,counter)-min(D(:,counter));
  66. % D(:,counter) = D(:,counter)./max(D(:,counter));
  67. % end
  68. % if (columnScanFlag==1)
  69. % I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,1)=reshape(D(:,counter),8,8);
  70. % I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,2)=reshape(D(:,counter),8,8);
  71. % I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,3)=reshape(D(:,counter),8,8);
  72. % else
  73. % Go in Column Scan:
  74. I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,1)=reshape(D(:,counter),X,Y);
  75. I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,2)=reshape(D(:,counter),X,Y);
  76. I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,3)=reshape(D(:,counter),X,Y);
  77. % end
  78. counter = counter+1;
  79. end
  80. end
  81. if (showImFlag)
  82. I = I-min(min(min(I)));
  83. I = I./max(max(max(I)));
  84. imshow(I,[]);
  85. end




測試程序

displayDictionary_test.m

  1. clc;
  2. clear all;
  3. %加載字典
  4. load('F:\Research\ScSR\ScSR\Dictionary\D_512_0.15_9.mat');
  5. patch_size=9;
  6. D=Dh;
  7. K=512;
  8. figure;
  9. %調用KSVD工具箱中的字典顯示函數
  10. im=displayDictionaryElementsAsImage(D, floor(sqrt(K)), floor(size(D,2)/floor(sqrt(K))),patch_size,patch_size);


輸出結果:


方法三:

因爲方法一顯示的字典圖像偏灰,對比度不強,所以通過對字典原子像素值進行拉伸變化到0-1,增強圖像對比度。

  1. clc;
  2. clear all;
  3. % load dictionary
  4. load('Dictionary/D_512_0.15_9.mat');
  5. patch_size=9;
  6. nRow=24;
  7. nCol=22;
  8. D=Dh';
  9. w=nCol*patch_size;
  10. h=nRow*patch_size;
  11. gridx = 1:patch_size :w;
  12. gridx = [gridx, w-patch_size+1];
  13. gridy = 1:patch_size : h;
  14. gridy = [gridy, h-patch_size+1];
  15. K=512; %字典原子總數
  16. DD=cell(1,K);
  17. row=length(gridx);
  18. col=length(gridy);
  19. hIm=zeros([w,h]);
  20. for i=1:K
  21. DD{i}=D(i,:);
  22. end
  23. for ii = 1:length(gridx),
  24. for jj = 1:length(gridy),
  25. yy = gridx(ii);
  26. xx = gridy(jj);
  27. if (ii-1)*nRow+jj >K
  28. break
  29. end
  30. temp=DD{(ii-1)*nCol+jj};
  31. hPatch=reshape(temp,[patch_size,patch_size]);
  32. I=hPatch;
  33. I = I-min(min(min(I)));
  34. I = I./max(max(max(I)));%對字典原子像素值進行拉伸變化到0-1
  35. hIm(yy:yy+patch_size-1, xx:xx+patch_size-1) = hIm(yy:yy+patch_size-1, xx:xx+patch_size-1) +I;
  36. end
  37. end
  38. figure;
  39. imshow(hIm);


調整參數,將字典原子像素值拉伸變換到0-0.7

  1. hPatch=reshape(temp,[patch_size,patch_size]);
  2. I=hPatch;
  3. I = I-min(min(min(I)));
  4. I = 0.7*I./max(max(max(I)));%對字典原子像素值進行拉伸變化到0-0.7


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章