稀疏表示字典的显示(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



        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/geekmanong">
                <img src="https://profile.csdnimg.cn/F/1/C/3_geekmanong" class="avatar_pic" username="geekmanong">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/geekmanong" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">zhihua_bupt</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog6.png">                                            </div>
                <div class="text"><span>原创文章 157</span><span>获赞 94</span><span>访问量 69万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">关注</a>
                                                            <a href="https://bbs.csdn.net/topics/395528039" target="_blank" class="btn btn-sm bt-button personal-messageboard">他的留言板
                    </a>
                                </div>
                        </div>
                    
    </div>
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值