稀疏表示字典的显示【MATLAB实现】

本文主要是实现论文--基于稀疏表示的图像超分辨率《Image Super-Resolution Via Sparse Representation》中的Figure2,通过对100000个高分辨率和低分辨率图像块训练得到的高分辨率图像块字典,字典原子总数为512,图像块尺寸大小为9X9


方法一:

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


输出结果:



可以看出,相比于论文中字典的显示图,颜色有点浅,通过调节MATLAB的colorbar,可以将背景颜色变深,

结果如下图所示:





方法二:

通过http://www.cs.technion.ac.il/~elad/software/提供的ksvd工具箱中的displayDictionaryElementsAsImage( )函数,来实现字典的显示。

displayDictionaryElementsAsImage.m

[cpp]  view plain  copy
  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.   
  8. borderSize = 1;  
  9. columnScanFlag = 1;  
  10. strechEachVecFlag = 1;  
  11. showImFlag = 1;  
  12.   
  13. if (length(who('X'))==0)  
  14.     X = 8;  
  15.     Y = 8;  
  16. end  
  17. if (length(who('sortVarFlag'))==0)  
  18.     sortVarFlag = 1;  
  19. end  
  20.   
  21. numElems = size(D,2);  
  22. if (length(who('numRows'))==0)  
  23.     numRows = floor(sqrt(numElems));  
  24.     numCols = numRows;  
  25. end  
  26. if (length(who('strechEachVecFlag'))==0)   
  27.     strechEachVecFlag = 0;  
  28. end  
  29. if (length(who('showImFlag'))==0)   
  30.     showImFlag = 1;  
  31. end  
  32.   
  33. %%% sort the elements, if necessary.  
  34. %%% construct the image to display (I)  
  35. sizeForEachImage = sqrt(size(D,1))+borderSize;  
  36. I = zeros(sizeForEachImage*numRows+borderSize,sizeForEachImage*numCols+borderSize,3);  
  37. %%% fill all this image in blue  
  38. I(:,:,1) = 1; %min(min(D));  
  39. I(:,:,2) = 1; %min(min(D));  
  40. I(:,:,3) = 1; %max(max(D));  
  41. % %%% fill all this image in blue  
  42. % I(:,:,1) = 0; %min(min(D));  
  43. % I(:,:,2) = 0; %min(min(D));  
  44. % I(:,:,3) = 1; %max(max(D));  
  45.   
  46. %%% now fill the image squares with the elements (in row scan or column  
  47. %%% scan).  
  48. if (strechEachVecFlag)  
  49.     for counter = 1:size(D,2)  
  50.         D(:,counter) = D(:,counter)-min(D(:,counter));  
  51.         if (max(D(:,counter)))  
  52.             D(:,counter) = D(:,counter)./max(D(:,counter));  
  53.         end  
  54.     end  
  55. end  
  56.   
  57.   
  58. if (sortVarFlag)  
  59.     vars = var(D);  
  60.     [V,indices] = sort(vars');  
  61.     indices = fliplr(indices);  
  62.     D = [D(:,1:sortVarFlag-1),D(:,indices+sortVarFlag-1)];  
  63.     signs = sign(D(1,:));  
  64.     signs(find(signs==0)) = 1;  
  65.     D = D.*repmat(signs,size(D,1),1);  
  66.     D = D(:,1:numRows*numCols);  
  67. end  
  68.   
  69. counter=1;  
  70. for j = 1:numRows  
  71.     for i = 1:numCols  
  72. %         if (strechEachVecFlag)  
  73. %             D(:,counter) = D(:,counter)-min(D(:,counter));  
  74. %             D(:,counter) = D(:,counter)./max(D(:,counter));  
  75. %         end  
  76. %         if (columnScanFlag==1)  
  77. %             I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,1)=reshape(D(:,counter),8,8);  
  78. %             I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,2)=reshape(D(:,counter),8,8);  
  79. %             I(borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,3)=reshape(D(:,counter),8,8);  
  80. %         else  
  81.             % Go in Column Scan:  
  82.             I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,1)=reshape(D(:,counter),X,Y);  
  83.             I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,2)=reshape(D(:,counter),X,Y);  
  84.             I(borderSize+(j-1)*sizeForEachImage+1:j*sizeForEachImage,borderSize+(i-1)*sizeForEachImage+1:i*sizeForEachImage,3)=reshape(D(:,counter),X,Y);  
  85. %         end  
  86.         counter = counter+1;  
  87.     end  
  88. end  
  89.   
  90. if (showImFlag)   
  91.     I = I-min(min(min(I)));  
  92.     I = I./max(max(max(I)));  
  93.     imshow(I,[]);  
  94. end  




测试程序

displayDictionary_test.m

[cpp]  view plain  copy
  1. clc;  
  2. clear all;  
  3.   
  4. %加载字典  
  5. load('F:\Research\ScSR\ScSR\Dictionary\D_512_0.15_9.mat');  
  6.   
  7. patch_size=9;  
  8. D=Dh;  
  9. K=512;  
  10. figure;  
  11. %调用KSVD工具箱中的字典显示函数  
  12. im=displayDictionaryElementsAsImage(D, floor(sqrt(K)), floor(size(D,2)/floor(sqrt(K))),patch_size,patch_size);  


输出结果:


方法三:

因为方法一显示的字典图像偏灰,对比度不强,所以通过对字典原子像素值进行拉伸变化到0-1,增强图像对比度。

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


调整参数,将字典原子像素值拉伸变换到0-0.7

[cpp]  view plain  copy
  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    

猜你喜欢

转载自blog.csdn.net/weixin_41923961/article/details/80570864