图像修复序列——BSCB模型

article/2025/10/15 15:15:09

1. 参考文献


2. BSCB模型代码

2.1 BSCB模型demo

% demo_BSCB.m
% Author: HSW
% Date: 2015/3/25
% HARBIN INSTITUTE OF TECHNOLOGY
%
% set matlab
close all;
clear all;
clc;options.null = 0;
% read image
Img = imread('Image\butterfly.bmp');
Img = imread('Image\peppers.bmp'); 
% Img = rgb2gray(Img); 
Img = im2double(Img);
if max(Img(:)) < 2Img = Img*255;
endFlagColor = (size(Img,3) == 3);% set mask
SetMask = 1;
if SetMask == 1% read maskMask = imread('Mask\peppers_mask512.bmp');Mask = Mask > 5; MFlagColor = (size(Mask,3) == 3);if MFlagColor ~= FlagColor && FlagColor == 1Mask = repmat(Mask,[1,1,3]);elseif MFlagColor ~= FlagColor && FlagColor == 0Mask = Mask(:,:,1);end
elseif SetMask == 2% Interactively set maskif not(exist('grab_mode'))grab_mode = 'line';endoptions.grab_mode = grab_mode;if not(exist('grab_radius'))grab_radius = 1;endswitch grab_modecase 'points'options.r = grab_radius;U = grab_inpainting_mask(Img,options);case 'line'options.r = grab_radius;[U,options.point_list] = grab_inpainting_mask(Img,options);end %switchIin = find(U(:,:,1) == Inf);Iout = find(U(:,:,1) ~= Inf);m1 = length(Iin); % 缺损点的总数% product the maskMask = zeros(size(Img));if FlagColor == 1tmpMask = zeros([size(Img,1),size(Img,2)]);for channel = 1:3tmpMask(Iin) = 1 ;Mask(:,:,channel) = tmpMask;endelseMask(Iin) = 1; % 缺损区域为1end % if FlagColor
end% if SetMask
nImg = (1-Mask).*Img;
PSNRin = 10*log10(255^2/mean((Img(:)-nImg(:)).^2)); 
InImg = nImg; 
% Initial Image
if FlagColor == 1Positions = find(Mask(:,:,1) == 1);for channel = 1:3tmpnImg = nImg(:,:,channel);tmpnImg(Positions) = floor(255*rand(1,length(Positions))) + 1;InImg(:,:,channel) = tmpnImg;end
elsePositions = find(Mask == 1);randValue = floor(255*rand(1,length(Positions))) + 1;InImg(Positions) = randValue;
end
% Main BSCB Model
IterNum = 3000;
I = BSCB_Diffusion(InImg,FlagColor,Mask,0.1);
for iter = 1:IterNumI = BSCB_Inpainting(I,FlagColor,Mask,0.2);if mod(iter,500) == 0 figure; imshow(I/255,[]); title(['Results of IterNum = ', num2str(iter)]); I = BSCB_Diffusion(I,FlagColor,Mask,0.2);end 
endI = max(0,min(I,255)); 
PSNRout = 10*log10(255^2/mean((I(:) - Img(:)).^2)); 
figure;
subplot(1,3,1);
imshow(Img/255,[]);
title('Original Image');
subplot(1,3,2);
imshow(nImg/255,[]);
title(['Masked Image PSNR = ',num2str(PSNRin), ' dB']);
subplot(1,3,3);
imshow(I/255,[]);
title(['Inpainting Image PSNR = ', num2str(PSNRout), ' dB']);

2.2 BSCB图像修复模型实现

function Img = BSCB_Inpainting(I,FlagColor,M,delta_t)
% input:
%       I: 待修复图像
%       M:缺损区域mask,缺损区域取值为1 
%       FlagColor:确定是否为彩色图像
%       delta_t:时间差分
% output:
%      Img: 修复图像
% Author: HSW
% Date: 2015/3/25
% HARBIN INSTITUTE OF TECHNOLOGY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Img = zeros(size(I));
if FlagColor == 1for channel = 1:3Ix = (I([2:end,end],:,channel) - I([1,1:end-1],:,channel))./2;Iy = (I(:,[2:end,end],channel) - I(:,[1,1:end-1],channel))./2;Ixx = I([2:end,end],:,channel) + I([1,1:end-1],:,channel) - 2*I(:,:,channel);Iyy = I(:,[2:end,end],channel) + I(:,[1,1:end-1],channel) - 2*I(:,:,channel);Laplace = Ixx + Iyy;Laplacex = (Laplace([2:end,end],:) - Laplace([1,1:end-1],:))./2;Laplacey = (Laplace(:,[2:end,end]) - Laplace(:,[1,1:end-1]))./2;Temp = sqrt(Ix.^2 + Iy.^2 + 0.00001);normNx = (-Iy)./Temp;normNy = Ix./Temp;Beta = Laplacex.*normNx + Laplacey.*normNy;dist1 = min(0,I(:,:,channel) - I([1,1:end-1],:,channel)).^2 + max(0, I([2:end,end],:,channel) - I(:,:,channel) ).^2 ...+ min(0, I(:,:,channel) - I(:,[1,1:end-1],channel)).^2 + max(0, I(:,[2:end,end],channel) - I(:,:,channel)).^2;dist2 = max(0, I(:,:,channel) - I([1,1:end-1],:,channel)).^2 + min(0, I([2:end,end],:,channel) - I(:,:,channel)).^2 ...+ max(0, I(:,:,channel) - I(:,[1,1:end-1],channel)).^2 + min(0,I(:,[2:end,end],channel) - I(:,:,channel)).^2;MuD1 = sqrt(dist1);MuD2 = sqrt(dist2);MuD = Beta.*((Beta > 0).* MuD1 + (1 - (Beta > 0)).* MuD2);signMuD = sign(MuD); Img(:,:,channel) = I(:,:,channel) + delta_t*M(:,:,channel).*signMuD.*sqrt(sqrt(signMuD.*MuD));end% for channel 
elseIx = (I([2:end,end],:) - I([1,1:end-1],:))./2;Iy = (I(:,[2:end,end]) - I(:,[1,1:end-1]))./2;Ixx = I([2:end,end],:) + I([1,1:end-1],:) - 2*I;Iyy = I(:,[2:end,end]) + I(:,[1,1:end-1]) - 2*I;Laplace = Ixx + Iyy;Laplacex = (Laplace([2:end,end],:) - Laplace([1,1:end-1],:))./2;Laplacey = (Laplace(:,[2:end,end]) - Laplace(:,[1,1:end-1]))./2;Temp = sqrt(Ix.^2 + Iy.^2 + 0.00001);normNx = -Iy./Temp;normNy = Ix./Temp;Beta = Laplacex.*normNx + Laplacey.*normNy;dist1 = min(0,I - I([1,1:end-1],:)).^2 + max(0, I([2:end,end],:) - I ).^2 ...+ min(0, I - I(:,[1,1:end-1])).^2 + max(0, I(:,[2:end,end]) - I).^2;dist2 = max(0, I - I([1,1:end-1],:)).^2 + min(0, I([2:end,end],:) - I).^2 ...+ max(0, I - I(:,[1,1:end-1])).^2 + min(0,I(:,[2:end,end]) - I).^2;MuD1 = sqrt(dist1);MuD2 = sqrt(dist2);MuD = Beta.*(( Beta > 0 ).* MuD1 + ( 1 - (Beta > 0) ).* MuD2);signMuD = sign(MuD); Img = I + delta_t*M.*signMuD.*sqrt(sqrt(signMuD.*MuD));
%     Img = I + delta_t*M.*MuD; %I = M.*I + (1-M).*Img; 
end% if FlagColor == 1
end % function

2.3 BSCB扩散模型

function Img = BSCB_Diffusion(I,FlagColor,M,delta_t)
% input:
%       I: 待修复图像
%       M: 缺损区域mask,缺损区域取值为1
%       FlagColor: 标记是否为彩色
%       delta_t: 时间差分
% output:
%       Img: 扩散后的图像
% Author: HSW
% Date: 2015/3/25
% HARBIN INSTITUTE OF TECHNOLOGY
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Img = zeros(size(I));
if FlagColor == 1dims = size(I,3);for channel = 1:dimsIx = ( I([2:end,end],:,channel) - I([1,1:end-1],:,channel) )./2;Iy = ( I(:,[2:end,end],channel) - I(:,[1,1:end-1],channel) )./2;Ixx = I([2:end,end],:,channel) + I([1,1:end-1],:,channel) - 2*I(:,:,channel);Iyy = I(:,[2:end,end],channel) + I(:,[1,1:end-1],channel) - 2*I(:,:,channel);Ixy = ((I([2:end,end],[2:end,end],channel) + I([1,1:end-1],[2:end,end],channel))./2 - (I([2:end,end],[1,1:end-1],channel) + I([1,1:end-1],[1,1:end-1],channel))./2)./2;TempNorm = Ix.^2 + Iy.^2 + 0.00001;DMu = (Ixx.*(Iy).^2 + Iyy.*(Ix).^2 - 2*Ix.*Iy.*Ixy)./TempNorm;Img(:,:,channel) = I(:,:,channel) + delta_t*M(:,:,channel).*DMu;end
elseIx = ( I([2:end,end],:) - I([1,1:end-1],:) )./2;Iy = ( I(:,[2:end,end]) - I(:,[1,1:end-1]) )./2;Ixx = I([2:end,end],:) + I([1,1:end-1],:) - 2*I;Iyy = I(:,[2:end,end]) + I(:,[1,1:end-1]) - 2*I;Ixy = ((I([2:end,end],[2:end,end]) + I([1,1:end-1],[2:end,end]))./2 - (I([2:end,end],[1,1:end-1]) + I([1,1:end-1],[1,1:end-1]))./2)./2;TempNorm = Ix.^2 + Iy.^2 + 0.00001;DMu = (Ixx.*(Iy).^2 + Iyy.*(Ix).^2 - 2*Ix.*Iy.*Ixy)./TempNorm;Img = I + delta_t*M.*DMu;
end %if FlagColor
end %function
2.4 检索选项参数
function v = getoptions(options, name, v, mendatory)% getoptions - retrieve options parameter
%
%   v = getoptions(options, 'entry', v0);
% is equivalent to the code:
%   if isfield(options, 'entry')
%       v = options.entry;
%   else
%       v = v0;
%   end
%
%   Copyright (c) 2007 Gabriel Peyreif nargin<4mendatory = 0;
endif isfield(options, name)v = eval(['options.' name ';']);
elseif mendatoryerror(['You have to provide options.' name '.']);
end 

2.5 创建掩模Mask

function [U,point_list] = grab_inpainting_mask(M, options)% grab_inpainting_mask - create a mask from user input
%
%   U = grab_inpainting_mask(M, options);
%
%   Select set of point in an image (useful to select a region for
%   inpainting). The set of point is U==Inf.
%
%   options.r is the radius for selection (default r=5).
%
%   Selection stops with right click.
%
%   Set options.mode='points' to gather disconnected points.
%   Set options.mode='line' to gather connected lines.
%
%   Copyright (c) 2006 Gabriel Peyreif nargin==3 && method==1U = grab_inpainting_mask_old(M, options);return;
endoptions.null = 0;
r = getoptions(options, 'r', 5);
method = getoptions(options, 'mode', 'points');if strcmp(method, 'line')if not(isfield(options, 'point_list'))[V,point_list] = pick_polygons(rescale(sum(M,3)),r);elsepoint_list = options.point_list;V = draw_polygons(rescale(sum(M,3)),r,point_list);end        U = M; U(V==1) = Inf;return;
endm = size(M,1);
n = size(M,2);
s = size(M,3);U = sum(M,3)/3;
b = 1;
[Y,X] = meshgrid(1:n,1:m); 
point_list = [];
while b==1clf;hold on;imagesc(rescale(M)); axis image; axis off; colormap gray(256);[y,x,b] = ginput(1);point_list(:,end+1) = [x;y];I = find((X-x).^2 + (Y-y).^2 <= r^2 );U(I) = Inf;for k=1:sMa = M(:,:,k);Ma(I) = 0;M(:,:,k) = Ma;end
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sk = draw_polygons(mask,r,point_list)sk = mask*0;
for i=1:length(point_list)pl = point_list{i};for k=2:length(pl)sk = draw_line(sk,pl(1,k-1),pl(2,k-1),pl(1,k),pl(2,k),r);end
endfunction [sk,point_list] = pick_polygons(mask,r)% pick_polygons - ask for the user to build a set of curves
%
%   sk = pick_polygons(mask,r);
%
%   mask is a background image (should be in [0,1] approx).
%
%   The user right-click on a set of point which create a curve.
%   Left click stop a curve.
%   Another left click stop the process.
%
%   Copyright (c) 2007 Gabriel Peyren = size(mask,1);sk = zeros(n);
point_list = {};
b = 1;
while b(end)==1% draw a lineclf;imagesc(mask+sk); axis image; axis off;colormap gray(256);[y1,x1,b] = ginput(1);pl = [x1;y1];while b==1clf;imagesc(mask+sk); axis image; axis off;[y2,x2,c] = ginput(1);if c~=1if length(pl)>1point_list{end+1} = pl;endbreak;endpl(:,end+1) = [x2;y2];sk = draw_line(sk,x1,y1,x2,y2,r);x1 = x2; y1 = y2;end
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function sk = draw_line(sk,x1,y1,x2,y2,r)n = size(sk,1);
[Y,X] = meshgrid(1:n,1:n);
q = 80;
t = linspace(0,1,q);
x = x1*t+x2*(1-t); y = y1*t+y2*(1-t);
if r==0x = round( x ); y = round( y );sk( x+(y-1)*n ) = 1;
elsefor k=1:qI = find((X-x(k)).^2 + (Y-y(k)).^2 <= r^2 );sk(I) = 1;end
endfunction U = grab_inpainting_mask_old(M, r)% grab_inpainting_mask - create a mask from user input
%
%   U = grab_inpainting_mask(M, r);
%
%   r is the radius for selection (default r=5).
%
%   Selection stops with right click.
%
%   Copyright (c) 2006 Gabriel Peyr?if nargin<2r = 5;
endm = size(M,1);
n = size(M,2);
s = size(M,3);U = sum(M,3)/3;
b = 1;
[Y,X] = meshgrid(1:n,1:m); 
while b==1clf;hold on;imagesc(rescale(M)); axis image; axis off; colormap gray(256);[y,x,b] = ginput(1);I = find((X-x).^2 + (Y-y).^2 <= r^2 );U(I) = Inf;for k=1:sMa = M(:,:,k);Ma(I) = 0;M(:,:,k) = Ma;end
end

3. 模型效果



http://chatgpt.dhexx.cn/article/bgjePYrQ.shtml

相关文章

day29:图像修复

在实际应用 中&#xff0c; 图像常常会受到噪声的干扰&#xff0c;例如拍照时镜头上存在灰尘或者飞行的小动物。这些 干 扰会导 拍摄到的图像出现部分内容被遮挡 的情况.对于较为久远的图像&#xff0c;可能只有实体图像而没有数字存储形式的底板&#xff0c; 因此相片在保存和…

【图像修复】基于深度学习的图像修复算法的MATLAB仿真

1.软件版本 matlab2021a 2.本算法理论知识 在许多领域&#xff0c;人们对图像质量的要求都很高&#xff0c;如医学图像领域、卫星遥感领域等。随着信息时代的快速发展&#xff0c;低分辨率图像已经难以满足特定场景的需要。因此&#xff0c;低分辨率图像恢复与重建的研究逐渐…

【毕业设计】深度学习图像修复算法研究与实现 - python

文章目录 1 前言2 什么是图像内容填充修复3 原理分析3.1 第一步&#xff1a;将图像理解为一个概率分布的样本3.2 补全图像 3.3 快速生成假图像3.4 生成对抗网络(Generative Adversarial Net, GAN) 的架构3.5 使用G(z)生成伪图像 4 在Tensorflow上构建DCGANs5 最后 1 前言 &…

图像修复 图像补全_图像修复简介

图像修复 图像补全 In practical applications, images are often corroded by noise. These noises are dust or water droplets on the lens, or scratches from old photos, or the image is artificially painted or part of the image Itself has been damaged. 在实际应用…

Opencv--图像修复

Opencv–图像修复 前言 在实际应用中&#xff0c;我们的图像常常会被噪声腐蚀&#xff0c;这些噪声或是镜头上的灰尘或水滴&#xff0c;或是旧照片的划痕&#xff0c;或者是图像遭到人为的涂画&#xff08;比如马赛克&#xff09;或者图像的部分本身已经损坏。如果我们想让这…

数字图像处理之图像修复

目录 目标 实验 主函数&#xff1a;加噪声&#xff0c;扭曲原始图片&#xff0c;使用滤波器修复图片 子函数1&#xff1a;中心化图片 子函数2&#xff1a;加高斯噪声 子函数3&#xff1a;维纳反卷积滤波器 子函数4&#xff1a;逆滤波器 实验结果 原始图片&#xff0c;退化…

图像修复模型——TV模型

1. 参考文献 2. TV图像修复模型 2.1 TV模型 % demo_TV.m % Author: HSW % Date: 2015/3/25 % HARBIN INSTITUTE OF TECHNOLOGY % % set matlab close all; clear all; clc;options.null 0; % read image Img imread(Image\butterfly.bmp); % Img imread(Image\peppers.bmp…

图像修复 学习笔记

目录 局部卷积&#xff08;PConv&#xff09;图像修复 Pconv torch 实现&#xff1a; 局部卷积&#xff08;PConv&#xff09;图像修复 本文提出了局部卷积&#xff08;PConv&#xff09;层来处理不规则孔。图1显示了使用建议的PConv的一些修复结果。看样子还不错&#xff0…

基于改进Criminisi算法的图像修复

1、内容简介 略 516-可以交流、咨询、答疑 2、内容说明 摘 要&#xff1a;针对 Criminisi算法难以获得理想的修复效果&#xff0c;且存在修复时间过长等缺陷&#xff0c;提出一种改进 Criminisi算法的 图像修复算法。改进优先权计算方式找到最优待修复块&#xff0c;完善最优…

图像修复简介

点击上方“小白学视觉”&#xff0c;选择加"星标"或“置顶” 重磅干货&#xff0c;第一时间送达推荐阅读 42个pycharm使用技巧&#xff0c;瞬间从黑铁变王者Google C项目编程风格指南 (中文版) 分享在实际应用中&#xff0c;图像经常被噪声腐蚀。这些噪音是镜头上的灰…

Halcon图像修复

1.之前研究OpenCV的图像修复时&#xff0c;知道Opencv提供的inpaint API能够实现这个效果。 void inpaint( InputArray src, 原图 InputArray inpaintMask, 二进制掩模&#xff0c;指示要修复的像素 OutputArray dst, 目标图像 double inpaintRadius, 像素周围的邻域补绘。…

图像修复

转自&#xff1a;https://blog.csdn.net/moxibingdao/article/details/107075598 本文继 去雨去雾去模糊篇 和 图像增强与图像恢复篇 之后&#xff0c;继续盘点CVPR 2020 中低层图像处理技术&#xff0c;本篇聚焦于图像修复&#xff08;Image Inpainting&#xff09;。 示例如…

CVPR 2020 论文大盘点-图像修复Inpainting篇

转自&#xff1a;https://mp.weixin.qq.com/s?__bizMzIwMTE1NjQxMQ&mid2247519592&idx2&sn3a0598c9f52e47929678a572ea451d98&chksm96f0ff3ca187762a107b4b9194e862b757d3d943ec399b35cbb7576cd92ee55cc648d7121ac3&scene21#wechat_redirect 本文继 去雨…

图像修复介绍

图像修复是一种利用缺损图像中已知部分的信息预测缺损区域的内容&#xff0c;允许使用替代内容取填充目标区域的技术。其最终目的是保证修复后的图像整体结构连贯统一&#xff0c;修复区域边缘处过渡自然&#xff0c;修复内容细节丰富合理&#xff0c;最好能够使观察者无法分辨…

【OpenCV】- 图像修复

说明&#xff1a;图像修复可以解决类似噪声或者是镜头上的灰尘或水滴或者旧照片上面的划痕等。 文章目录 1、实现图像修补&#xff1a;inpaint()函数2、opencv之鼠标响应函数3、示例程序 1、实现图像修补&#xff1a;inpaint()函数 说明&#xff1a;图像修补技术由inpaint()函数…

图像修复(Image Restoration)算法数据集详细介绍

目录 人脸数据集 1.Helen Face 2.CelebA (Celebrity Attribute) 3.CelebA-HQ 4.FFHQ&#xff08;Flickr-Faces-HQ&#xff09; 场景数据集 1.MS COCO (Common Objects in Context) 2.ImageNet 3.Places2 街景数据集 1.Paris StreetView 2.Cityscapes 纹理数据集 …

图像修复 : ICCV 2021 基于条件纹理和结构并行生成的图像修复【翻译】

声明&#xff1a;精简翻译&#xff0c;未完全校对 积压的存稿、好久没更文了、先发一篇这个代码很不错、推荐有兴趣的同学学习博主也写了对应的测评文章待发、点赞越多、发的越快如有同学&#xff0c;学有余力、可以转载这个文章&#xff08; 附原文地址即可 &#xff09;、校对…

Linux udhcpc/udhcpd 移植

参考文档&#xff1a; http://blog.chinaunix.net/uid-14704264-id-4272838.html https://www.cnblogs.com/chenfulin5/p/9481249.html 若系统busybox 自带了 udhcpc 和 udhcpd 工具 udhcpc 作为客户端工具&#xff0c;用于动态获取IP&#xff1b; udhcpd 作为服务器工具&…

udhcpc6的default.script

udhcpc6使用中遇到的问题 和udhcpc一样&#xff0c;udhcpc6是busybox中的一个工具&#xff0c;主要用来提供dhcpv6客户端服务。 在使用过程中遇到了一个问题&#xff0c;直接执行udhcpc6 -i eth0&#xff0c;可以看到打印信息中显示正在发送discover包&#xff0c;如果本地有…

UNIX source code-DHCP

文章目录 DHCP基础知识什么是DHCP为什么要使用DHCP IP地址分配机制工作原理报文类型基本步骤中继重用IP租赁期限 代码解析文件作用udhcpd.c结构体代码逻辑&#xff08;流程&#xff09; udhcpc.c结构体代码逻辑&#xff08;流程&#xff09; file.c结构体&#xff08;read_conf…