SVD图像处理(MATLAB)

使用SVD处理图像模拟演示

参考文献

https://github.com/matzewolf/Image_compression_SVD/blob/master/svd_compress.m

在这里插入图片描述

MATLAB代码

clc;
clearvars;
close all;A_org=imread("lena256.bmp");
compr=20;
A_org=double(A_org);A_red  = svd_compress( A_org, compr );
subplot(1,2,1),imshow(A_org,[]);
subplot(1,2,2),imshow(A_red,[]);function [ A_red ] = svd_compress( A_org, compr )% svd_compress compresses an input matrix (e.g. an image) using the
% Singular Value Decomposition (SVD).
%   Input args: A_org: Any matrix with double real entries, e.g. an image 
%   file (converted from uint8 to double).
%   compr: Quality of compression. If 0 <= compr < 1, it only keeps
%   Singular Values (SVs) larger than compr times the biggest SV. If 1 <= 
%   compr <= number of SVs, it keeps the biggest compr SVs. Otherwise the 
%   function returns an error.
%   Output args: A_red: Compressed version of A_org in double using the
%   SVD, e.g. an image file (convert from double to uint8).% SVD on the original matrix
[U,S,V] = svd(A_org);% Extract Singular Values (SVs)
singvals = diag(S);% Determine SVs to be saved
if compr >= 0 && compr < 1% only SVs bigger than compr times biggest SVindices = find(singvals >= compr * singvals(1));
elseif compr >= 1 && compr <= length(singvals)% only the biggest compr SVsindices = 1:compr;
else% return errorerror('Incorrect input arg: compr must satisfy 0 <= compr <= number of Singular Values');
end% Truncate U,S,V
U_red = U(:,indices);
S_red = S(indices,indices);
V_red = V(:,indices);% Calculate compressed matrix
A_red = U_red * S_red * V_red';end

运行结果

在这里插入图片描述

% Image Compression with Singular Value Decomposition (SVD).
%   This script uses the SVD for Image Compression, analyses the algorithm
%   (also with Information Theory) and visualizes the results.close all; clear; clc;
tic;
COL = 256; % number of colors in uint8, so 2^8 = 256.%% Compression% Original image matrix
Lena_org = imread('Lena.bmp'); % in uint8
Lena = double(Lena_org); % in double% Call compressing function (and measure performance)
compr = 0.01; % change compr to change quality
tic;
Lena_red = uint8(svd_compress(Lena,compr));
func_time = toc; % compression function execution time
fprintf('Execution time of svd_compress: %d seconds.\n',func_time);% Save compressed image
imwrite(Lena_red,'ReducedLena.bmp');%% Analysis of the algorithm% SVD on the image
[U,S,V] = svd(Lena);% Extract Singular Values (SVs)
singvals = diag(S);% Determine SVs to be saved
if compr >= 0 && compr < 1% only SVs bigger than compr times biggest SVindices = find(singvals >= compr * singvals(1));
elseif compr >= 1 && compr <= length(singvals)% only the biggest compr SVsindices = 1:compr;
else% return errorerror(...'Incorrect input arg: compr must satisfy 0 <= compr <= number of Singular Values');
end% Size of the image
m = size(Lena,1);
n = size(Lena,2);
storage = m*n;
fprintf('Size of image: %d px by %d px, i.e. uses %d px of storage.\n',m,n,storage);% SVs and reduced storage
r = min([m,n]); % original number of SVs
r_red = length(indices); % to be saved number of SVs
r_max = floor(m*n/(m+n+1)); % maximum to be saved number of SVs for compression
storage_red = m*r_red + n*r_red + r_red;
if compr >= 0 && compr < 1% only SVs bigger than compr times biggest SVfprintf('The smallest SV chosen to be smaller than %d of the biggest SV.\n',compr);
elseif compr >= 1 && compr <= length(singvals)% only the biggest compr SVs
else% return errorfprintf('There was some error before. Analysis cannot continue.\n')
end
fprintf('Out of %d SVs, only %d SVs saved ',r,r_red);
fprintf('(Maximum number of SVs for compression: %d SVs).\n',r_max);
fprintf('Reduced storage: %d px.\n',storage_red);% Determine made error
error = 1 - sum(singvals(indices))/sum(singvals);
fprintf('Made error: %d.\n',error);
errorImage = Lena_org - Lena_red;% Entropy
entropy_org = entropy(Lena_org);
fprintf('Entropy of original image: %d bit.\n',entropy_org);
entropy_red = entropy(Lena_red);
fprintf('Entropy of compressed image: %d bit.\n',entropy_red);
entropy_err = entropy(errorImage);
fprintf('Entropy of error image: %d bit.\n',entropy_err);% 1D Histogram: Original Probability
[orgProb,~,~] = histcounts(Lena_org,1:(COL+1),'Normalization','probability');% 2D Histogram: Joint Probabiltiy
[jointProb,~,~] = histcounts2(Lena_red,Lena_org,...1:(COL+1),1:(COL+1),'Normalization','probability');% Joint Entropy
p_logp_nan = jointProb.*log2(jointProb);
p_logp = p_logp_nan(isfinite(p_logp_nan));
joint_entropy = -sum(p_logp);
fprintf('Joint entropy: %d bit.\n',joint_entropy);% Mutual Information
mi = entropy_org + entropy_red - joint_entropy;
fprintf('Mutual information: %d bit.\n',mi);% Conditional Probability
condProb = jointProb./orgProb;
condProb(isnan(condProb)|isinf(condProb))=0; % all NaN and inf converted to zero
col_sum = sum(condProb,1); % test if condProb really sums up to 1 columnwise%% Relationship between selcted SVs and ...numSVals = 1:1:r; %SVs for which the properties are calculated% ...used storage
storageSV = m*numSVals + n*numSVals + numSVals;% ...made error and entropies (compressed and error)
displayedError = zeros(size(numSVals));
entropySV = zeros(4,length(numSVals));% 1st row entropy of compressed image, 2nd row entropy of error image% 3rd row joint entropy, 4th row mutual information
j = 1; % position in the display vectors
for i = numSVals% store S in a temporary matrixS_loop = S;% truncate SS_loop(i+1:end,:) = 0;S_loop(:,i+1:end) = 0;% construct Image using truncated SLena_red_loop = uint8(U*S_loop*V');% construct error imageLena_err_loop = Lena_org - Lena_red_loop;% compute errorerror_loop = 1 - sum(diag(S_loop))/sum(diag(S));% add error to display vectordisplayedError(j) = error_loop;% compute entropy of compressed image and add to row 1 of display matrixentropySV(1,j) = entropy(Lena_red_loop);% compute entropy of error image and add to row 2 of display matrixentropySV(2,j) = entropy(Lena_err_loop);% compute joint entropy of original and compresed image[jointProb_loop,~,~] = histcounts2(Lena_org,Lena_red_loop,[COL COL],...'Normalization','probability');p_logp_nan_loop = jointProb_loop.*log2(jointProb_loop);p_logp_loop = p_logp_nan_loop(isfinite(p_logp_nan_loop));entropySV(3,j) = -sum(p_logp_loop);% compute mutual information of original and compressed imageentropySV(4,j) = entropy_org + entropySV(1,j) - entropySV(3,j);% update positionj = j + 1;
end%% Figure 1fig1 = figure('Name','Images and Histograms',...'units','normalized','outerposition',[0 0 1 1]);% Original image
subplot(2,3,1)
imshow(uint8(Lena))
title('Original image')% Histogram of original image
subplot(2,3,4)
imhist(Lena_org)
title('Histogram of original image')% Compressed image
subplot(2,3,2)
imshow(uint8(Lena_red))
title('Compressed image')% Histogram of compressed image
subplot(2,3,5)
imhist(Lena_red)
title('Histogram of compressed image')% Error image
subplot(2,3,3)
imshow(uint8(errorImage))
title('Error image')% Histogram of error image
subplot(2,3,6)
imhist(errorImage)
title('Histogram of error image')%% Figure 2fig2 = figure('Name','Joint Histogram',...'units','normalized','outerposition',[0 0 1 1]);% 2D Histogram: Joint PDF
histogram2(Lena_red,Lena_org,1:(COL+1),1:(COL+1),...'Normalization','probability','FaceColor','flat')
colorbar
title('Joint Histogram')
xlabel('Compressed image')
ylabel('Original image')
zlabel('Joint Probability')%% Figure 3fig3 = figure('Name','Properties over selected Singular Values',...'units','normalized','outerposition',[0 0 1 1]);% Used storage over saved SVs
subplot(2,2,1)
plot(numSVals, storage.*ones(size(numSVals))) % original storage (horizontal)
hold on
plot(numSVals, storageSV)
legend('Original storage', 'Storage of SVD','Location','northwest')
xlabel('Number of saved Singular Values')
ylabel('Used storage [px]')
title('Used storage over saved SVs')% Compression error over saved SVs
subplot(2,2,3)
plot(numSVals, displayedError)
xlabel('Number of saved Singular Values')
ylabel('Compression error [-]')
title('Compression error over saved SVs')% Entropies over saved SVs
subplot(2,2,[2,4])
plot(numSVals, entropy_org.*ones(size(numSVals))) % original entropy (horizontal)
hold on
plot(numSVals, entropySV)
legend('Original entropy', 'Compression entropy', 'Error entropy',...'Joint entropy','Mutual information','Location','southoutside')
xlabel('Number of saved Singular Values')
ylabel('Entropies [bit]')
title('Entropies over saved SVs')%% Save figuressaveas(fig1, 'Results.png');
saveas(fig2, 'Joint_Histogram.png');
saveas(fig3, 'Analysis.png');%% Execution timeexecution_time = toc; % total script execution time
fprintf('Total execution time of svd_lena_script: %d seconds.\n',execution_time);

运行结果

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/296252.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Kimi精选提示词,总结PPT内容

大家好&#xff0c;我是子云&#xff0c;最近真是觉得Kimi这个大模型&#xff0c;产品体验很棒&#xff0c;能力也是不错&#xff0c;感觉产品经理用心了。 发现一个Kimi 一个小技巧&#xff0c;可以学习到很多高级提示词。 Kimi输入框可以配置常用提示词&#xff0c;同时也可…

C++算法——二分法查找

一、二分查找算法思想和模版 1.算法思想 2.细节处理 3.模板 二、二分查找 1.链接 704. 二分查找 - 力扣&#xff08;LeetCode&#xff09; 2.描述 3.思路 先从最经典的题目去切入&#xff0c;思路就是二分查找&#xff0c;这里我们认为&#xff0c;目标值既可以看作为左部…

Twitter Api查询用户粉丝列表

如果大家为了获取实现方式代码的话可能要让大家失望了&#xff0c;这边文章主要是为了节省大家开发时间&#xff0c;少点坑。https://api.twitter.com/2/users/:id/followers &#xff0c;这个接口很熟悉吧&#xff0c;他是推特提供的获取用户关注者&#xff08;粉丝&#xff0…

STM32-04基于HAL库(CubeMX+MDK+Proteus)中断案例(按键中断扫描)

文章目录 一、功能需求分析二、Proteus绘制电路原理图三、STMCubeMX 配置引脚及模式&#xff0c;生成代码四、MDK打开生成项目&#xff0c;编写HAL库的按键检测代码五、运行仿真程序&#xff0c;调试代码 一、功能需求分析 在完成GPIO输入输出案例之后&#xff0c;开始新的功能…

Node.js环境调用百度智能云(百度云)api鉴权认证三步走

方式一 :Postman脚本的方式生成v1版本的认证字符串 Postman脚本下载 下载Postman pre-request Script 设置 Authorization 示例脚本 方式二&#xff1a;在线签名工具生成 (试用于验证编程字符串签名是否有错误) 签名计算工具 https://cloud.baidu.com/signature/index.html …

使用libibverbs构建RDMA应用

本文是对论文Dissecting a Small InfiniBand Application Using the Verbs API所做的中英文对照翻译 Dissecting a Small InfiniBand Application Using the Verbs API Gregory Kerr∗ College of Computer and Information ScienceNortheastern UniversityBoston, MAkerrgccs…

HbnnMall电子商城系统介绍(功能与技术栈)

今天在看我个人网站上的文章时&#xff0c;看到了曾经在2020年自己开发的电商系统。那时我已经入职小米有一段时间了&#xff0c;基本已经对各个业务线&#xff0c;各种业务知识有了系统性的了解和学习&#xff0c;所以想自己动手写一个电商系统&#xff0c;以便进一步提高自己…

ElasticSearch7.8的下载与安装和Kibana 7.8.0工具使用安装

1、ElasticSearch7.8.0下载 elasticsearch: 官方下载地址&#xff1a;https://www.elastic.co/cn/downloads/elasticsearch 链接: https://pan.baidu.com/s/1wAKQoB3nhLhcnBlPfVOLxQ 提取码: t83n kibana: 链接: https://pan.baidu.com/s/156aD9zDdvUv8LFgDEIPoSw 提取码:…

云存储中常用的相同子策略的高效、安全的基于属性的访问控制的论文阅读

参考文献为2022年发表的Efficient and Secure Attribute-Based Access Control With Identical Sub-Policies Frequently Used in Cloud Storage 动机 ABE是实现在云存储中一种很好的访问控制手段&#xff0c;但是其本身的计算开销导致在实际场景中应用收到限制。本论文研究了…

(2024)Ubuntu源码安装多个版本的opencv并切换使用

本人工作会用到x86_64的opencv和aarch64的opencv&#xff0c;所以写下来备忘自用 一、源码编译安装 依赖库安装&#xff1a; sudo apt-get install build-essential libgtk2.0-dev libgtk-3-dev libavcodec-dev libavformat-dev libjpeg-dev libswscale-dev libtiff5-dev o…

上位机图像处理和嵌入式模块部署(qmacvisual图像清晰度)

【 声明&#xff1a;版权所有&#xff0c;欢迎转载&#xff0c;请勿用于商业用途。 联系信箱&#xff1a;feixiaoxing 163.com】 做过isp的同学都知道&#xff0c;图像处理里面有一个3A&#xff0c;即自动曝光、自动白平衡和自动对焦。其中自动对焦这个&#xff0c;就需要用输入…

HarmonyOS NEXT应用开发之PersistentStorage:持久化存储UI状态

前两个小节介绍的LocalStorage和AppStorage都是运行时的内存&#xff0c;但是在应用退出再次启动后&#xff0c;依然能保存选定的结果&#xff0c;是应用开发中十分常见的现象&#xff0c;这就需要用到PersistentStorage。 PersistentStorage是应用程序中的可选单例对象。此对…

Vue的学习之旅-part1

Vue的学习之旅-part1 vue介绍vue读音编程范式ES6中不用var声明变量vue的声明、初始化传参使用data中数据时要用this指向 vue中的语法糖MVVM在Vue中&#xff0c; MVVM的各层的对应位置 方法、函数的不同之处 vue介绍 vue读音 Vue 读作 /vju:/ 不要读成v u e Vuex 的x读作叉 不…

Redis高可用技术

一.Redis高可用介绍&#xff1a; 高可用是指&#xff1a;服务器正常访问的时间 衡量的标准是&#xff1a;在多长时间内可以提供正常服务99.9%、99.99%、99.999%等等 但是在Redis语境中&#xff0c; 高可用的含义似乎要宽泛一些&#xff0c;除了保证提供正常服务(如主从分离、…

IntelliJ IDEA中文---强化智能编码与重构,提升开发效率

IntelliJ IDEA 2023是一款功能强大的集成开发环境&#xff08;IDE&#xff09;&#xff0c;专为Java开发人员设计。它支持智能代码编辑、自动补全和重构&#xff0c;帮助开发者提高编码效率。同时&#xff0c;内置了丰富的调试工具&#xff0c;支持断点调试和变量监视&#xff…

C语言--文件操作

1.标准流 • stdin - 标准输⼊流&#xff0c;在⼤多数的环境中从键盘输⼊&#xff0c;scanf函数就是从标准输⼊流中读取数据。 • stdout - 标准输出流&#xff0c;⼤多数的环境中输出⾄显⽰器界⾯&#xff0c;printf函数就是将信息输出到标准输出 流中。 • stderr - 标…

丰诺畅机电科技将莅临2024年第13届生物发酵展

参展企业介绍 无锡丰诺畅机电科技有限公司&#xff0c;是一家分离设备专业制造公司&#xff0c;集开发、设计、制造、销售、服务于一体;具有专业的生产技术&#xff0c;先进的生产工艺&#xff0c;精良的制造设备&#xff0c;完善的检测手段;为满足不同用户的过滤需求&#xf…

HTTP/UDP/TCP/IP网络协议

文章目录 计算机网络基础HTTPUDPTCP连接管理(三次握手/四次挥手)TCP可靠传输(确认答应)超时重传滑动窗口流量控制拥塞控制延时应答捎带应答粘包问题其他 IP数据链路层MUT 相关问题TCP会粘包、UDP永远不会粘包 学习博客 计算机网络基础 OSI模型定义了网络互连的七层框架&#x…

esp32控制舵机---待完善

舵机有三个引脚&#xff0c;分别是电源、电源GND和信号线。如下图所示&#xff1a; ESP32-WROOM-32E的引脚的定义如下&#xff1a; 图来自乐鑫官网:ESP32-DevKitC V4 入门指南 - ESP32 - — ESP-IDF 编程指南 v5.2.1 文档 硬件连接图&#xff1a; 待补充

013——超声波模块驱动开发(基于I.MX6uLL与SR04)

目录 一、 模块介绍 1.1 产品特色 1.2 产品实物图 1.3 接口定义 1.4 测距调节 1.5 模块工作原理 1.6 注意 二、 编码思路 三、 驱动程序 四、 应用程序 五、 Makefile 六、 其它及实验 一、 模块介绍 超声波测距模块是利用超声波来测距。模块先发送超声波&#xf…