【图像修复】基于matlab损坏图像修复【含Matlab源码 731期】
【摘要】
一、获取代码方式
获取代码方式1: 完整代码已上传我的资源:【图像修复】基于matlab损坏图像修复【含Matlab源码 731期】
获取代码方式2: 通过订阅紫极神光博客付费专栏,凭支付凭证,私信博...
一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【图像修复】基于matlab损坏图像修复【含Matlab源码 731期】
获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
备注:
订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
二、部分源代码
clc; clear all;
%First part
%reading img1, saving as im1 variable
im1 = imread('img1.jpg');
%for boundary it's a must to convert to gray
im1 = rgb2gray(im1);
subplot(331), imshow(im1), title('Img1 convert to gray');
% as a mask all colors will be white or black
mask = im1 < 255;
subplot(332), imshow(mask), title('Apply a mask');
%invert colors, otherwise the boundary sees nearly
%the whole image, need to swap colors
mask = imcomplement(mask);
subplot(333), imshow(mask), title('Invert colors');
%gives location x y coordinates where is the first area,
%it starts there too
%if don't have ; at the end, it shows the dimensions of
%the image
dim = size(mask)
col = round(dim(2)/2)-90;
row = min(find(mask(:,col)))
%an interesting drawback, if I would fill the "holes"
%with erode function which is better then imfill, the
%program can't see any of the boundaries, hence in the
%for loop I had to use 180 rounds, otherwise can't see
%the big parts.
%erode image
% se = strel('line',10, 90);
% boundaries = imerode(mask,se);
% imshow(boundaries);
%use bwtraceboundary function to find the boundary from
%the above declared point.
%it needs a binary image, row and col coordinates for start
%and direction, W for west so left.
boundary = bwtraceboundary(mask,[row, col],'W');
subplot(334), imshow(im1), title('Boundaries');
hold on;
plot(boundary(:,2),boundary(:,2),'g','LineWidth',4);
%the imfill fills the smaller objects. However, as I
%mentioned above the erode function does better job.
%Unfortunately, with that result the for loop cannot
%find any boundaries, despite the imshow(mask)black&white
%image looks better.
BW_filled = imfill(mask,'holes');
boundaries = bwboundaries(BW_filled);
%shows the border of all white part, with the imfill
%function the for loop must iterate 180 times, hilarious.
%Under 180 it doesn't find the last top right big white
%object.
for k=1:180
b = boundaries{k};
plot(b(:,2),b(:,1),'g','LineWidth',4);
end
%Second part
%denoise test; averaging or median filter
im2 = imread('Penguins.jpg');
im2 = imresize(im2, [768, 1024]);
rgbImage = im2;
subplot(335), imshow(rgbImage), title('Resized but noisy Img2');
%averaging filter
mat = ones(5,5)/25;
averagingFilter_im2 = imfilter(rgbImage, mat);
subplot(336), imshow(averagingFilter_im2), title('Img2 averaging filter');
%median filter
for k=1:3
medianFilter_im2(:,:,k)=medfilt2(rgbImage(:,:,k),[3,3]);
end
subplot(337), imshow(medianFilter_im2), title('Img2 median filter');
%Third part
im1 = imread('img1.jpg');
recoveredImage = im1;
recoveredImageMedianFilter = im1;
zero = recoveredImage == 255;
recoveredImage(zero) = averagingFilter_im2(zero);
zero1 = recoveredImageMedianFilter == 255;
recoveredImage(zero1) = averagingFilter_im2(zero1);
recoveredImageMedianFilter(zero1) = medianFilter_im2(zero);
subplot(338), imshow(recoveredImage), title('Recovered image with averaging filter');
subplot(339), imshow(recoveredImageMedianFilter), title('Recovered image with median filter');
%Compare recoveredImage and originalImage
meanRecoveredIm = mean(recoveredImage(:))
meanOriginalIm = mean(originalImage(:))
%It coutns the Structural Similarity Index (SSIM) value
%for original and recovered image.
ssimValue = ssim(originalImage, recoveredImage)
%It counts the Peak Signal to Noise Ratio value
%for original and recovered image. They must be the same
%class and size as well.
peaks2NoiseRatio = psnr(originalImage, recoveredImage)
%It counts the Mean Squared Error (MSE) between arrays
%of the 2 declared variable, currently the original and the recovered image.
meanSquaredErr = immse(originalImage, recoveredImage)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/115527169
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)