I tried to apply 'emd' function on image to get output image with IMFs selected. But got stuck at the errors. Thanks in Advance.

3 views (last 30 days)
Applied EMD function to image and tried to get first Two IMFs and reconstruct the image with them. But got error in reshape step. Thanks in Advance if i solution in this.
%% Calculate First 2 IMFs from Image by 'emd' and reconstruct the image
clc
clear all
close all
% Test on 03.05.2023
% Read the image
img = imread('D:\Search Works\356-inputs.png');
% Convert the image to grayscale
img_gray = rgb2gray(img);
img_grey = im2double(img_gray);
% Reshape image into a 1D vector
img_vec = img_grey(:);
% Perform EMD
imf = emd(img_vec);
% Take only first two IMFs
imf = imf(1:2,:);
% Reshape the IMFs back into a 2D matrix
imf_mat = permute(imf, [2 1]);
imf_mat = reshape(imf_mat, [size(img_grey), size(imf,1)]);
% Display the first two IMFs
subplot(1,3,1), imshow(img_gray), title('Original Image');
subplot(1,3,2), imshow(imf_mat(:,:,1)), title('IMF 1');
subplot(1,3,3), imshow(imf_mat(:,:,2)), title('IMF 2');

Answers (1)

Milan Bansal
Milan Bansal on 13 Sep 2024
Hi D.Regan,
The error you are encountering in the "reshape" fucntion is due to the mismatch between size of imf_mat and the shape you want to achieve. The size of imf_mat is 10 x 2 where as the shape you are trying to achieve is 512 x 512 x 2 which is not possible.
The size of imf_mat is not as expected because the 2 imfs were selected along the 1st dimesion instead of 2nd. Change the dimension of selection to ensure the correct IMFs are selected. Also make sure to correct the dimension if reshape function. Here is how you can modify your code.
%% Calculate First 2 IMFs from Image by 'emd' and reconstruct the image
clc
clear all
close all
% Test on 03.05.2023
% Read the image
img = imread('356-inputs.png');
% Convert the image to grayscale
img_gray = rgb2gray(img);
img_grey = im2double(img_gray);
% Reshape image into a 1D vector
img_vec = img_grey(:);
% Perform EMD
imf = emd(img_vec);
% Take only first two IMFs
imf = imf(:,1:2);
% Reshape the IMFs back into a 2D matrix
% imf_mat = permute(imf, [2 1]);
imf_mat = reshape(imf, [size(img_grey), size(imf,2)]);
% Display the first two IMFs
subplot(1,3,1), imshow(img_gray), title('Original Image');
subplot(1,3,2), imshow(imf_mat(:,:,1)), title('IMF 1');
subplot(1,3,3), imshow(imf_mat(:,:,2)), title('IMF 2');
Hope this helps!

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!