Image Overlay Using MATLAB for Blurred Image Processing

[Editor’s Note:] Remember the post published on December 3, 2020, titled “Infinitely Weak Image Recognition Stitching Program” (see related link 14 at the end of the article, for easy reference, all related links about image processing are listed), which was very practical, and the editor tested it with their own photos, achieving good results. Today’s “Image Overlay” is similar, with technical content, including unifying the size of two images and writing a program (myconv2) to implement the convolution function of the library function conv2, which is worth sharing. The program also has some imperfections, such as saving the processed image file; image processing programs generally require saving the processing results to disk (just like saving numerical computation results as disk files).
1. Experiment Report

1. Image Overlay

The original purpose of the program was to process blurred images, meaning that there are two images with different degrees of blur, which can be synthesized into a clearer image through this program. However, after improvements, this program can achieve the fusion of any two images.

2. Design Idea:

First, unify the size of the two images and determine the number of layers in the Gaussian pyramid based on the image size; then determine the Gaussian kernel, use the Gaussian pyramid to compress the images and calculate the residuals using the Laplacian pyramid; compare the data points of the two images and select the clearer data points; finally, overlay them to obtain the combined image.

3. Functionality:

comsize: Unifies the size of the two images, taking the larger number of rows and columns, because subsequent processing using the pyramid will scale the image by a factor of 2, so the number of pyramid layers is determined based on the image size; myconv2: Performs convolution, a self-written program to implement the function of the library function conv2. When using conv2, the array size was not quite right, so I wrote myconv2; get_lap: Gaussian pyramid and Laplacian pyramid; fuzzy_image_process: Main program for processing blurred images.

2. Program Operation Status
1. The interface after starting the program, waiting for the user to insert two images
Image Overlay Using MATLAB for Blurred Image Processing
2. Click the “Insert Image 1” button and the “Insert Image 2” button in sequence to select two original images in the open file dialog, which will be displayed on two coordinate axes
Image Overlay Using MATLAB for Blurred Image Processing
3. After clicking the “Image Overlay” button, wait a moment, and the result will appear in the third coordinate axis
Image Overlay Using MATLAB for Blurred Image Processing
4. There can also be other uses, such as “encryption” processing, achieving the fusion of any two images
Image Overlay Using MATLAB for Blurred Image Processing
If the original image has a large pixel size, the processing time may be longer, please be patient; at the same time, it will occupy more memory, pay attention to monitor memory usage.
3. Program Design Interface (iamge_combine2.fig)
Image Overlay Using MATLAB for Blurred Image Processing
4. Program Code
1. Main Program Code (iamge_combine2.m)
function varargout = iamge_combine2(varargin)% IAMGE_COMBINE2 MATLAB code for iamge_combine2.fig% Last Modified by GUIDE v2.5 10-May-2023 23:09:07% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name',       mfilename, ...                   'gui_Singleton',  gui_Singleton, ...                   'gui_OpeningFcn', @iamge_combine2_OpeningFcn, ...                   'gui_OutputFcn',  @iamge_combine2_OutputFcn, ...                   'gui_LayoutFcn',  [] , ...                   'gui_Callback',   []);if nargin && ischar(varargin{1})    gui_State.gui_Callback = str2func(varargin{1});endif nargout    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});else    gui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT
2. fuzzy_image_process.m Function Code
function combine_image=fuzzy_image_process% Image Readingglobal image1;global image2;img1 = imread(image1);img2 = imread(image2);[cimg1,cimg2,level]=comsize(img1,img2);im1 = im2double(cimg1);im2 = im2double(cimg2);% Get Laplacian PyramidL1 = get_lap(im1,level);L2 = get_lap(im2,level);ksize = 5;sigma = 3;Image = cell(1,level);for k=1:level    % Store the points with larger absolute values in Image, i.e., the clearer points    a = abs(L1{k}) > abs(L2{k});    Image{k} = a.*L1{k} + ~a.*L2{k};endgauss = zeros(ksize, ksize);for i = 1 : ksize    for j = 1 : ksize        gauss(i, j) = exp(-((i - ceil(ksize/2))^2 + (j - ceil(ksize/2))^2)/(2 * sigma^2));    endendgauss = gauss / sum(gauss(:));%% Image integration, overlaying Image and Laplacian, i.e., overlaying clearer points and actual pointsfor i = level-1: -1 : 1     [rows, cols, ~] = size(Image{i+1});    upsampled = zeros(2*rows,2*cols,3);    upsampled(2:2:end,1:2:end,1) = Image{i+1}(:,:,1);    upsampled(2:2:end,1:2:end,2) = Image{i+1}(:,:,2);    upsampled(2:2:end,1:2:end,3) = Image{i+1}(:,:,3);    upsampled(:,:,1) = 4*myconv2(upsampled(:,:,1), gauss);     upsampled(:,:,2) = 4*myconv2(upsampled(:,:,2), gauss);     upsampled(:,:,3) = 4*myconv2(upsampled(:,:,3), gauss);     Image{i} = Image{i} + upsampled; end combine_image=Image{1}; 
3. comsize.m Function Code
function [sz1,sz2,level]=comsize(origin1,origin2)[row1,colon1,layer1] = size (origin1);[row2,colon2,layer2] = size (origin2);if row1>row2    crow=row1;else    crow=row2;endif colon1>colon2    ccolon=colon1;else     ccolon=colon2;endsz1=imresize(origin1,[crow,ccolon]);sz2=imresize(origin2,[crow,ccolon]);n=1;while rem(crow,2^n)==0&&rem(ccolon,2^n)==0    n=n+1;endlevel=n-1;
4. get_lap.m Function Code
function laplacian = get_lap(img,level)%% Gaussian Kernel Generationsigma = 3; % Standard deviation of Gaussian kernelksize = 5; % Kernel sizeGauss = zeros(ksize, ksize);for i = 1 : ksize    for j = 1 : ksize        Gauss(i, j) = exp(-((i - ceil(ksize/2))^2 + (j - ceil(ksize/2))^2)/(2 * sigma^2));    endendGauss = Gauss / sum(Gauss(:));%% Gaussian Pyramidpyramid = cell(1, level);pyramid{1} = img;for i = 2 : level    % Reduce image    [rows,cols,~] = size(pyramid{i-1});     pyramid{i} = pyramid{i-1}(1:2:rows,1:2:cols,:);     % Smooth the image using Gaussian filtering    pyramid{i}(:,:,1) = myconv2(pyramid{i}(:,:,1), Gauss);    pyramid{i}(:,:,2) = myconv2(pyramid{i}(:,:,2), Gauss);    pyramid{i}(:,:,3) = myconv2(pyramid{i}(:,:,3), Gauss);end%% laplacian stores the residuals, which are the missing data pointslaplacian = cell(1, level); laplacian{level} = pyramid{level}; % First upsample, then expand to suitable sizefor i = level-1: -1 : 1     [rows, cols, ~] = size(pyramid{i+1});    upsampled = zeros(2*rows,2*cols,3);    upsampled(2:2:end,1:2:end,1) = pyramid{i+1}(:,:,1);    upsampled(2:2:end,1:2:end,2) = pyramid{i+1}(:,:,2);    upsampled(2:2:end,1:2:end,3) = pyramid{i+1}(:,:,3);    upsampled(:,:,1) = 4*myconv2(upsampled(:,:,1), Gauss);     upsampled(:,:,2) = 4*myconv2(upsampled(:,:,2), Gauss);     upsampled(:,:,3) = 4*myconv2(upsampled(:,:,3), Gauss);     laplacian{i} = pyramid{i} - upsampled; end 
5. myconv2.m Function Code
function output = myconv2(input, kernel)    [m, n] = size(input);    [k, l] = size(kernel);    pad_input = padarray(input, [floor(k/2), floor(l/2)], 'replicate', 'both');    output = zeros(m, n);    for i = 1 : m        for j = 1 : n            output(i, j) = sum(sum(pad_input(i:i+k-1, j:j+l-1) .* kernel));        end    endend
5. Design Report
Image Overlay Using MATLAB for Blurred Image Processing
6. File Directory List
Image Overlay Using MATLAB for Blurred Image Processing
Related Links:
1. MATLAB Major Assignment – Image Processing [January 13, 2025]
2. MATLAB Major Assignment – Image Processing and Demonstration [December 4, 2024]
3. MATLAB Major Assignment – Digital Image Processing Program Based on MATLAB [November 28, 2024]
4. MATLAB Major Assignment – Image Processing [November 26, 2024]
5. MATLAB Major Assignment – Lissajous Curve Image Drawing [November 24, 2024]
6. MATLAB Major Assignment – Image Processing [May 2, 2024]
7. MATLAB Major Assignment – Function Integral Image Demonstration [April 30, 2024]
8. MATLAB Major Assignment – Image Processing [April 29, 2024]
9. MATLAB Major Assignment – Image Processing System [April 18, 2024]
10. MATLAB Major Assignment – Image Processing [April 16, 2024]
11. MATLAB Major Assignment – Digital Image Processing Tools [November 19, 2022]
12. MATLAB Assignment – Image Processing [May 15, 2021]
13. MATLAB Assignment – Image Processing [May 7, 2021]
14. MATLAB Assignment – Infinitely Weak Image Recognition Stitching Program [December 3, 2020]
15. MATLAB Assignment – Image Processing System [May 9, 2020]
16. MATLAB Assignment – Image Special Effects Processing System [May 2, 2020]
17. MATLAB Assignment – Image Encryption & Decryption [April 30, 2019]
18. MATLAB Assignment – Image Processing Software [April 12, 2019]
Image Overlay Using MATLAB for Blurred Image Processing

Leave a Comment