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.mzph.cn/news/804705.shtml

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

相关文章

yolov7的改进工地安全帽佩戴检测系统-协同双注意力机制CDAM2(教程+代码)

研究的背景和意义 随着工业化和城市化的快速发展&#xff0c;建筑工地的安全问题日益凸显。在建筑工地中&#xff0c;工人的安全是至关重要的&#xff0c;而工地安全帽的佩戴是保障工人安全的重要措施之一。然而&#xff0c;由于工地环境复杂多变&#xff0c;工人的佩戴情况往…

为什么企业都用企微文档?真的好用吗?

现在很多企业都在使用企微文档&#xff0c;还没开始使用的企业难免会产生疑惑&#xff0c;企微文档真的好用吗&#xff1f;其实企业选择使用企微文档的原因有很多&#xff0c;主要的原因是企微文档能够满足企业在文件管理和协作方面的多种需求&#xff0c;从而提高办公效率。 下…

未来客服行业的趋势与展望:构建更高效、个性化的客户体验

客服行业是商业领域中的重要组成部分&#xff0c;它直接影响着企业的品牌形象和客户满意度。随着科技的飞速发展&#xff0c;客服行业也正在经历深刻的变革。今天将描绘未来客服行业的发展趋势&#xff0c;帮助我们更好地理解这个行业的未来走向。 1. 人工智能和机器学习的广泛…

汽车充电桩主板在出厂前需要做哪些检测?

充电桩主板作为核心组件承载着充电桩的关键功能&#xff0c;其性能和稳定性直接影响着用户充电体验、桩企产品合规和市场竞争力&#xff0c;以及主板厂商的品牌知名度。因此&#xff0c;对充电桩主板进行全面的测试尤为重要。 下面将详细介绍充电桩主板检测的内容&#xff0c;包…

信息素养与终身学习解锁题目搜索之道的新引擎【文末送书】

文章目录 信息素养&#xff1a;搜索前的准备终身学习&#xff1a;搜索后的深化新引擎的构建与运行 搜索之道&#xff1a;信息素养与终身学习的新引擎【文末送书】 随着互联网的快速发展和信息技术的日益成熟&#xff0c;搜索已经成为获取知识和信息的主要途径之一。然而&#x…

STM32CubeIDE基础学习-舵机控制实验

STM32CubeIDE基础学习-舵机控制实验 文章目录 STM32CubeIDE基础学习-舵机控制实验前言第1章 硬件介绍第2章 工程配置2.1 基础工程配置部分2.2 生成工程代码部分 第3章 代码编写第4章 实验现象总结 前言 SG90、MG996舵机在机器人领域用得非常多&#xff0c;因为舵机有内置控制电…

基于51单片机的数字时钟与日历显示控制设计

**单片机设计介绍&#xff0c; 基于51单片机的数字时钟与日历显示控制设计 文章目录 一 概要二、功能设计设计思路 三、 软件设计原理图 五、 程序六、 文章目录 一 概要 基于51单片机的数字时钟与日历显示控制设计是一个结合了硬件与软件技术的综合性项目。以下是该设计的概要…

SpringBoot中application.yml引入多个YML文件

系列文章目录 文章目录 系列文章目录前言前言 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站,这篇文章男女通用,看懂了就去分享给你的码吧。 首先,你要了解SpringBoot配置文件加载顺序,加载位置(代码内,Nacos等)…

从零开始搭建后端信息管理系统(新手小白比如)

如果你是新手小白&#xff0c;首先我们要进行一些准备工作&#xff0c;安装一些基础软件&#xff0c; 备注一下&#xff1a;这里安装的vue环境的后台管理系统&#xff0c;不同的后台管理系统&#xff0c;需要安装不同的插件 准备工作&#xff1a; 安装 Visual Studio Code …

刷代码随想录有感(27):重复的子字符串

题干&#xff1a; 代码&#xff1a; class Solution { public:void getNext(int *next, string &s){int j 0;next[0] 0;for(int i 1; i < s.size(); i){while(j > 0 && s[i] ! s[j]){j next[j - 1];}if(s[i] s[j]){j;}next[i] j;}}bool repeatedSubst…

基于java+springboot+vue实现的农产品智慧物流系统(文末源码+Lw)23-239

摘 要 互联网发展至今&#xff0c;无论是其理论还是技术都已经成熟&#xff0c;而且它广泛参与在社会中的方方面面。它让信息都可以通过网络传播&#xff0c;搭配信息管理工具可以很好地为人们提供服务。针对信息管理混乱&#xff0c;出错率高&#xff0c;信息安全性差&#…

Python创建现代GUI应用程序库之ttkbootstrap使用详解

概要 ttkbootstrap是一个基于Python的Tkinter库构建的,用于创建现代GUI应用程序。它结合了Tkinter的简洁性和Bootstrap框架的美观,提供了丰富的组件和样式,使开发人员能够快速构建美观、响应式的桌面应用程序。 安装 通过pip可以轻松安装ttkbootstrap: pip install ttkb…

阿里云服务器北京地域多少钱?北京地域最新收费标准及便宜购买教程

阿里云服务器在国内有十几个地域可选&#xff0c;北京地域主要适合北方用户选择&#xff0c;2024年阿里云中国内地地域云服务器做了降价调整&#xff0c;因此收费标准也有所变化&#xff0c;本文为大家展示阿里云服务器北京地域最新的收费标准&#xff0c;以及在实际购买过程中…

JUC-线程的创建、运行与查看

创建和运行线程 Thread创建线程 Thread 创建线程方式&#xff1a;创建线程类&#xff0c;匿名内部类方式 start() 方法底层其实是给 CPU 注册当前线程&#xff0c;并且触发 run() 方法执行线程的启动必须调用 start() 方法&#xff0c;如果线程直接调用 run() 方法&#xff…

设计模式-接口隔离原则

基本介绍 客户端不应该依赖它不需要的接口&#xff0c;即一个类对另一个类的依赖应该建立在最小的接口上先看一张图: 类A通过接口Interface1 依赖类B&#xff0c;类C通过接口Interface1 依赖类D&#xff0c;如果接口Interface1对于类A和类C来说不是最小接口&#xff0c;那么类…

什么是多路复用器滤波器

本章将更深入地介绍多路复用器滤波器&#xff0c;以及它们如何用于各种应用中。您将了解到多路复用器如何帮助设计人员创造出更复杂的无线产品。 了解多路复用器 多路复用器是一组射频(RF)滤波器&#xff0c;它们组合在一起&#xff0c;但不会彼此加载&#xff0c;可以在输出之…

Linux网络名称空间与网络协议栈:区别、联系与理解

在深入探讨Linux网络名称空间和网络协议栈之间的区别和联系之前&#xff0c;重要的是先明确这两个概念的定义。网络名称空间是Linux提供的一种虚拟化技术&#xff0c;允许在同一物理机器上运行的不同进程组拥有独立的网络环境&#x1f3e2;。而网络协议栈是操作系统用于实现网络…

视频图像的两种表示方式YUV与RGB(3)

上篇文章介绍了YUV的采样格式&#xff0c;本篇重点介绍YUV的存储方式。接下来将用图形式给出常见YUV图像的存储方式&#xff0c;并在存储方式后面附有取样每个像素点YUV的数据方法&#xff0c;图中&#xff0c;Cb、Cr的含义等同于U、V。 YUYV为YUV422采样的存储格式中的一种&a…

探索 2024 年最佳编码自定义 GPT

如何利用GPT技术优化您的软件开发流程&#xff1f; 介绍 在快速发展的技术世界中&#xff0c;人工智能 (AI) 已成为创新的基石&#xff0c;特别是在编码和软件开发领域。 改变这一格局的人工智能工具之一是自定义 GPT。 这些先进的模型不仅彻底改变了我们的编码方式&#xff0c…

linux fixmap分析

本文基于Linux-4.19.125&#xff0c; ARM V7&#xff0c;dual core, MMU采用2级页表&#xff08;未开启LPAE&#xff09;。 1 为什么需要fixmap Linux内核启动过程中&#xff0c;经过汇编阶段后&#xff0c;mmu功能已经开启&#xff0c;后续只能通过虚拟地址来访问DDR&#x…