Wavelet Transform for N dimensions - matlab

I came across this amazing response Applying MATLAB's idwt2 several times which I executed to understand it myself. However, I am unable to get how to use the same with work with an RGB image. So, I have 3 Questions.
How would the code be applied to an RGB image with only the transformed image displayed in the output that is along with the high and low frequency components along row and column,is it possible to view the fusion of all the components as a single image? I am aware that I have to put cat operator, but I cant understand how to go about it.
Secondly, I am also getting a mazed image! I am perplexed since I cannot seem to follow the reason. I have also attached the same code with the statement showing how this image has been generated.
3.What does the term db1 in the function signature of dwt imply?
CODE:
load woman; % Load image data
%startImage=imread('pic_rgb.jpg'); % IF I WANT TO WORK WITH RGB IMAGE
nLevel = 3; % Number of decompositions
nColors = size(map,1); % Number of colors in colormap
cA = cell(1,nLevel); % Approximation coefficients
cH = cell(1,nLevel); % Horizontal detail coefficients
cV = cell(1,nLevel); % Vertical detail coefficients
cD = cell(1,nLevel); % Diagonal detail coefficients
startImage = X;
for iLevel = 1:nLevel,
[cA{iLevel},cH{iLevel},cV{iLevel},cD{iLevel}] = dwt2(startImage,'db1');
startImage = cA{iLevel};
end
figure;colormap(map);
imagesc(dwt2(startImage,'db1')); %THIS GIVES THE MAZED IMAGE INSTEAD OF THE TRANSFORMED IMAGE
figure;
tiledImage = wcodemat(cA{nLevel},nColors);
for iLevel = nLevel:-1:1,
tiledImage = [tiledImage wcodemat(cH{iLevel},nColors); ...
wcodemat(cV{iLevel},nColors) wcodemat(cD{iLevel},nColors)];
end
figure;
imshow(tiledImage,map);
%reconstruct
fullRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
fullRecon = idwt2(fullRecon,cH{iLevel},cV{iLevel},cD{iLevel},'db1');
end
partialRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
partialRecon = idwt2(partialRecon,[],[],[],'db1');
end
figure;
imshow([X fullRecon; partialRecon zeros(size(X))],map,...
'InitialMagnification',50);

The sample image used in my answer to that other question was an indexed image, so there are a few changes that need to be made to get that code working for an RGB image.
I'll first address your question about the 'db1' argument passed to DWT2. This specifies the type of wavelet to use for the decomposition (in this case, a Daubechies wavelet). More information about available wavelets can be found in the documentation for the functions WFILTERS and WAVEINFO.
I'll address your first two questions by showing you how to modify the code from my other answer to work for an RGB image. I'll use the sample 'peppers.png' image. You'll first want to load your image and define the number of values each color component has. Since the sample image is an unsigned 8-bit integer type (the most common situation), nColors will be 256:
X = imread('peppers.png'); %# Load sample image
nColors = 256; %# Number of values per color component
If your images are larger unsigned integer types (e.g. 'uint16'), a general way to find the number of color values is to use the function INTMAX like so:
nColors = double(intmax(class(X)))+1;
For the ensuing code, an image type of 'uint8' is assumed.
Applying the decompositions is no different than in the indexed image case. The coefficient matrices will simply be M-by-N-by-3 matrices instead of M-by-N matrices:
nLevel = 3; %# Number of decompositions
cA = cell(1,nLevel); %# Approximation coefficient storage
cH = cell(1,nLevel); %# Horizontal detail coefficient storage
cV = cell(1,nLevel); %# Vertical detail coefficient storage
cD = cell(1,nLevel); %# Diagonal detail coefficient storage
startImage = X;
for iLevel = 1:nLevel, %# Apply nLevel decompositions
[cA{iLevel},cH{iLevel},cV{iLevel},cD{iLevel}] = dwt2(startImage,'db1');
startImage = cA{iLevel};
end
The code to create the tiled image to show the horizontal, vertical, and diagonal components for each decomposition will change due to the fact that we are now working with 3-D matrices and must use the CAT function instead of the concatenation operator []:
tiledImage = wcodemat(cA{nLevel},nColors);
for iLevel = nLevel:-1:1
tiledImage = cat(1,cat(2,tiledImage,...
wcodemat(cH{iLevel},nColors)),...
cat(2,wcodemat(cV{iLevel},nColors),...
wcodemat(cD{iLevel},nColors)));
end
figure;
imshow(uint8(tiledImage-1)); %# Convert to unsigned 8-bit integer to display
This will give the following image showing the horizontal (top right), vertical (bottom left), and diagonal (bottom right) components for each decomposition step, along with the reduced image (top left):
The reconstruction steps are unchanged from the other answer. Only the code for displaying the final images needs to be modified:
fullRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
fullRecon = idwt2(fullRecon,cH{iLevel},cV{iLevel},cD{iLevel},'db1');
end
partialRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
partialRecon = idwt2(partialRecon,[],[],[],'db1');
end
figure;
tiledImage = cat(1,cat(2,X,uint8(fullRecon)),...
cat(2,uint8(partialRecon),zeros(size(X),'uint8')));
imshow(tiledImage,'InitialMagnification',50);
And you will get an image showing the original RGB image (top left), the fully-reconstructed image using all of the stored detail coefficient matrices (top right), and the partially-reconstructed image using none of the stored detail coefficient matrices (bottom left):

Related

Matlab - Plotting Specific Pixels (image treatment)

I'm currently struggling with an image treatment\ data plotting issue and was hoping to get some feedback from people with more experience than myself on this matter.
I'll try and breakdown the problem as to make it more understandable:
I have an original image (figureB - which is the blue chanel of the original image) of size NxM, from this image I select a specific area to study (NewfigureB), size 120x170;
I then divide this area into what I called macropixels which are 10x10 arrays of data points (pixels);
I then apply a mask to the selected area to select only the points meeting certain luminescence conditions;
So far so good. My problem comes when I try to plot a histogram of each of these macropixels when applying the luminescence mask. The final objective is to then find the peaks in these histograms.
so far this is what I've come up with. Any help would be greatly appreciated.
Many thanks
%Make the number of pixels in the matrix divisible
Macropixel = 10; %determine the size of the macropixel
[rows,columns] = size(figureB); %determine dimentions of the matrix used in the calculations
MacropixRows = floor(rows/Macropixel); %determine how many macropixels are in a row of the original matrix
MacropixColumn = floor(columns/Macropixel); %determine how many macropixels are in a column of the original matrix
%define new dim for the matrix
rows = MacropixRows * Macropixel;
columns = MacropixColumn * Macropixel;
NewfigureB = figureB(1:rows,1:columns); %divisible by the size of the macropixels created
%select area
NewfigureB = NewfigureB(1230:1349,2100:2269);
%create luminescence mask
Lmin=50;
hmax=80;
mask=false(size(NewfigureB));
mask(NewfigureB <Lmin)=true;
mask=mask & (NewfigureB<hmax);
%Apply mask
NewfigureB=NewfigureB(mask);
for jj = 1:Macropixel:120
for ii =1:Macropixel:170
histogram( NewfigureB(jj:jj+Macropixel-1, ii:ii+Macropixel-1))
end
end'''
The code you have posted has too many issues.
I tried to correct it the best I could.
I modified some parameters to feat the sample image I used.
I couldn't find your sample image, so I used the following
Here is a corrected code (please read the comments):
I = imread('Nikon-D810-Image-Sample-7.jpg');
figureB = I(:,:,3);
%Make the number of pixels in the matrix divisible
Macropixel = 10; %determine the size of the macropixel
[rows,columns] = size(figureB); %determine dimentions of the matrix used in the calculations
MacropixRows = floor(rows/Macropixel); %determine how many macropixels are in a row of the original matrix
MacropixColumn = floor(columns/Macropixel); %determine how many macropixels are in a column of the original matrix
%define new dim for the matrix
rows = MacropixRows * Macropixel;
columns = MacropixColumn * Macropixel;
NewfigureB = figureB(1:rows,1:columns); %divisible by the size of the macropixels created
%select area
NewfigureB = NewfigureB(1230:1349,2100:2269);
%create luminescence mask
Lmin=90;%50; %Change to 90 for testing
hmax=200;%80; %Change to 200 for testing
mask=false(size(NewfigureB));
mask(NewfigureB > Lmin)=true; %I think it should be > Lmin. %mask(NewfigureB <Lmin)=true;
mask=mask & (NewfigureB<hmax);
%This is not the right way to apply a mask, because the result is a vector (not a matrix).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Apply mask
%NewfigureB=NewfigureB(mask);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Assuming there are no zeros in the image, you can set masked elements to zero:
NewfigureB(~mask) = 0;
for jj = 1:Macropixel:120
for ii =1:Macropixel:170
%Copy NewfigureB(jj:jj+Macropixel-1, ii:ii+Macropixel-1) into temporary matrix MB:
MB = NewfigureB(jj:jj+Macropixel-1, ii:ii+Macropixel-1);
%Remove the zeros from MB (zeros are masked elements).
%The result is a vector (not a matrix).
MB = MB(MB ~= 0);
%histogram( NewfigureB(jj:jj+Macropixel-1, ii:ii+Macropixel-1))
figure; %Open new figure for each histogram. (I don't know if it's a good idea).
histogram(MB); %Plot the histogram of vector MB.
end
end
It's probably not exactly matches intentions.
I hope it gives you a lead...

Histogram of binary image in MATLAB

I'm trying to do a vertical histogram of a binary image. I don't want to use MATLAB's functions. How to do it?
I have tried this code but I don't know if it's correct or not:
function H = histogram_binary(image)
[m,n] = size(image);
H = zeros(1,256);
for i = 1:m
for j = 1:n
H(1,image(i,j)) = H(1,image(i,j))+1;
end
end
The image is:
The result:
Why can't I see the value of black pixels in the histogram?
% Read the binary image...
img = imread('66He7.png');
% Count total, white and black pixels...
img_vec = img(:);
total = numel(img_vec);
white = sum(img_vec);
black = total - white;
% Plot the result in the form of an histogram...
bar([black white]);
set(gca(),'XTickLabel',{'Black' 'White'});
set(gca(),'YLim',[0 total]);
Output:
For what concerns your code, it is not counting black pixels since they have a value of 0 and your loop start from 1... rewrite it as follows:
function H = histogram_binary(img)
img_vec = img(:);
H = zeros(1,256);
for i = 0:255
H(i+1) = sum(img_vec == i);
end
end
But keep in mind that counting all the byte occurrences on a binary image (that can only contain 0 or 1 values) is kinda pointless and will make your histogram lack readability. On a side note, avoid using image as a variable name, since this would override an existing function.
As mentioned by #beaker in the comments above, vertical histogram in such cases generally refers to a vertical projection. Here is a way to do this :
I = imread('YcP1o.png'); % Read input image
I1 = rgb2gray(I); % convert image to grayscale
I2 = im2bw(I1); % convert grayscale to binary
I3 = ~I2; % invert the binary image
I4 = sum(I3,1); % project the inverted binary image vertically
I5 = (I4>1); % ceil the vector
plot([1:1:size(I,2)],I5); ylim([0 2])
You can further check for 0->1 transitions to count the number of characters using sum(diff(I5)>0) which gives 13 as answer in this case.

Find the real time co-ordinates of the four points marked in red in the image

To be exact I need the four end points of the road in the image below.
I used find[x y]. It does not provide satisfying result in real time.
I'm assuming the images are already annotated. In this case we just find the marked points and extract coordinates (if you need to find the red points dynamically through code, this won't work at all)
The first thing you have to do is find a good feature to use for segmentation. See my SO answer here what-should-i-use-hsv-hsb-or-rgb-and-why for code and details. That produces the following image:
we can see that saturation (and a few others) are good candidate colors spaces. So now you must transfer your image to the new color space and do thresholding to find your points.
Points are obtained using matlab's region properties looking specifically for the centroid. At that point you are done.
Here is complete code and results
im = imread('http://i.stack.imgur.com/eajRb.jpg');
HUE = 1;
SATURATION = 2;
BRIGHTNESS = 3;
%see https://stackoverflow.com/questions/30022377/what-should-i-use-hsv-hsb-or-rgb-and-why/30036455#30036455
ViewColoredSpaces(im)
%convert image to hsv
him = rgb2hsv(im);
%threshold, all rows, all columns,
my_threshold = 0.8; %determined empirically
thresh_sat = him(:,:,SATURATION) > my_threshold;
%remove small blobs using a 3 pixel disk
se = strel('disk',3');
cleaned_sat = imopen(thresh_sat, se);% imopen = imdilate(imerode(im,se),se)
%find the centroids of the remaining blobs
s = regionprops(cleaned_sat, 'centroid');
centroids = cat(1, s.Centroid);
%plot the results
figure();
subplot(2,2,1) ;imshow(thresh_sat) ;title('Thresholded saturation channel')
subplot(2,2,2) ;imshow(cleaned_sat);title('After morpphological opening')
subplot(2,2,3:4);imshow(im) ;title('Annotated img')
hold on
for (curr_centroid = 1:1:size(centroids,1))
%prints coordinate
x = round(centroids(curr_centroid,1));
y = round(centroids(curr_centroid,2));
text(x,y,sprintf('[%d,%d]',x,y),'Color','y');
end
%plots centroids
scatter(centroids(:,1),centroids(:,2),[],'y')
hold off
%prints out centroids
centroids
centroids =
7.4593 143.0000
383.0000 87.9911
435.3106 355.9255
494.6491 91.1491
Some sample code would make it much easier to tailor a specific solution to your problem.
One solution to this general problem is using impoint.
Something like
h = figure();
ax = gca;
% ... drawing your image
points = {};
points = [points; impoint(ax,initialX,initialY)];
% ... generate more points
indx = 1 % or whatever point you care about
[currentX,currentY] = getPosition(points{indx});
should do the trick.
Edit: First argument of impoint is an axis object, not a figure object.

Measuring weighted mean length from an electrophoresis gel image

Background:
My question relates to extracting feature from an electrophoresis gel (see below). In this gel, DNA is loaded from the top and allowed to migrate under a voltage gradient. The gel has sieves so smaller molecules migrate further than longer molecules resulting in the separation of DNA by length. So higher up the molecule, the longer it is.
Question:
In this image there are 9 lanes each with separate source of DNA. I am interested in measuring the mean location (value on the y axis) of each lane.
I am really new to image processing, but I do know MATLAB and I can get by with R with some difficulty. I would really appreciate it if someone can show me how to go about finding the mean of each lane.
Here's my try. It requires that the gels are nice (i.e. straight lanes and the gel should not be rotated), but should otherwise work fairly generically. Note that there are two image-size-dependent parameters that will need to be adjusted to make this work on images of different size.
%# first size-dependent parameter: should be about 1/4th-1/5th
%# of the lane width in pixels.
minFilterWidth = 10;
%# second size-dependent parameter for filtering the
%# lane profiles
gaussWidth = 5;
%# read the image, normalize to 0...1
img = imread('http://img823.imageshack.us/img823/588/gele.png');
img = rgb2gray(img);
img = double(img)/255;
%# Otsu thresholding to (roughly) find lanes
thMsk = img < graythresh(img);
%# count the mask-pixels in each columns. Due to
%# lane separation, there will be fewer pixels
%# between lanes
cts = sum(thMsk,1);
%# widen the local minima, so that we get a nice
%# separation between lanes
ctsEroded = imerode(cts,ones(1,minFilterWidth));
%# use imregionalmin to identify the separation
%# between lanes. Invert to get a positive mask
laneMsk = ~repmat(imregionalmin(ctsEroded),size(img,1),1);
Image with lanes that will be used for analysis
%# for each lane, create an averaged profile
lblMsk = bwlabel(laneMsk);
nLanes = max(lblMsk(:));
profiles = zeros(size(img,1),nLanes);
midLane = zeros(1,nLanes);
for i = 1:nLanes
profiles(:,i) = mean(img.*(lblMsk==i),2);
midLane(:,i) = mean(find(lblMsk(1,:)==i));
end
%# Gauss-filter the profiles (each column is an
%# averaged intensity profile
G = exp(-(-gaussWidth*5:gaussWidth*5).^2/(2*gaussWidth^2));
G=G./sum(G);
profiles = imfilter(profiles,G','replicate'); %'
%# find the minima
[~,idx] = min(profiles,[],1);
%# plot
figure,imshow(img,[])
hold on, plot(midLane,idx,'.r')
Here's my stab at a simple template for an interactive way to do this:
% Load image
img = imread('gel.png');
img = rgb2gray(img);
% Identify lanes
imshow(img)
[x,y] = ginput;
% Invert image
img = max(img(:)) - img;
% Subtract background
[xn,yn] = ginput(1);
noise = img((yn-2):(yn+2), (xn-2):(xn+2));
noise = mean(noise(:));
img = img - noise;
% Calculate means
means = (1:size(img,1)) * double(img(:,round(x))) ./ sum(double(img(:,round(x))), 1);
% Plot
hold on
plot(x, means, 'r.')
The first thing to do to is convert your RGB image to grayscale:
gr = rgb2gray(imread('gelk.png'));
Then, take a look at the image intensity histogram using imhist. Notice anything funny about it? Use imcontrast(imshow(gr)) to pull up the contrast adjustment tool. I found that eliminating the weird stuff after the major intensity peak was beneficial.
The image processing task itself can be divided into several steps.
Separate each lane
Identify ('segment') the band in each lane
Calculate the location of the bands
Step 1 can be done "by hand," if the lane widths are guaranteed. If not, the line detection offered by the Hough transform is probably the way to go. The documentation on the Image Processing Toolbox has a really nice tutorial on this topic. My code recapitulates that tutorial with better parameters for your image. I only spent a few minutes with them, I'm sure you can improve the results by tuning the parameters further.
Step 2 can be done in a few ways. The easiest technique to use is Otsu's method for thresholding grayscale images. This method works by determining a threshold that minimizes the intra-class variance, or, equivalently, maximizes the inter-class variance. Otsu's method is present in MATLAB as the graythresh function. If Otsu's method isn't working well you can try multi-level Otsu or a number of other histogram based threshold determination methods.
Step 3 can be done as you suggest, by calculating the mean y value of the segmented band pixels. This is what my code does, though I've restricted the check to just the center column of each lane, in case the separation was off. I'm worried that the result may not be as good as calculating the band centroid and using its location.
Here is my solution:
function [locations, lanesBW, lanes, cols] = segmentGel(gr)
%%# Detect lane boundaries
unsharp = fspecial('unsharp'); %# Sharpening filter
I = imfilter(gr,unsharp); %# Apply filter
bw = edge(I,'canny',[0.01 0.3],0.5); %# Canny edges with parameters
[H,T,R] = hough(bw); %# Hough transform of edges
P = houghpeaks(H,20,'threshold',ceil(0.5*max(H(:)))); %# Find peaks of Hough transform
lines = houghlines(bw,T,R,P,'FillGap',30,'MinLength',20); %# Use peaks to identify lines
%%# Plot detected lines above image, for quality control
max_len = 0;
imshow(I);
hold on;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%# Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
%# Determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if ( len > max_len)
max_len = len;
end
end
hold off;
%%# Use first endpoint of each line to separate lanes
cols = zeros(length(lines),1);
for k = 1:length(lines)
cols(k) = lines(k).point1(1);
end
cols = sort(cols); %# The lines are in no particular order
lanes = cell(length(cols)-1,1);
for k = 2:length(cols)
lanes{k-1} = im2double( gr(:,cols(k-1):cols(k)) ); %# im2double for compatibility with greythresh
end
otsu = cellfun(#graythresh,lanes); %# Calculate threshold for each lane
lanesBW = cell(size(lanes));
for k = 1:length(lanes)
lanesBW{k} = lanes{k} < otsu(k); %# Apply thresholds
end
%%# Use segmented bands to determine migration distance
locations = zeros(size(lanesBW));
for k = 1:length(lanesBW)
width = size(lanesBW{k},2);
[y,~] = find(lanesBW{k}(:,round(width/2))); %# Only use center of lane
locations(k) = mean(y);
end
I suggest you carefully examine not only each output value, but the results from each step of the function, before using it for actual research purposes. In order to get really good results, you will have to read a bit about Hough transforms, Canny edge detection and Otsu's method, and then tune the parameters. You may also have to alter how the lanes are split; this code assumes that there will be lines detected on either side of the image.
Let me add another implementation similar in concept to that of #JohnColby's, only without the manual user-interaction:
%# read image
I = rgb2gray(imread('gele.png'));
%# middle position of each lane
%# (assuming lanes are somewhat evenly spread and of similar width)
x = linspace(1,size(I,2),10);
x = round( (x(1:end-1)+x(2:end))./2 );
%# compute the mean value across those columns
m = mean(I(:,x));
%# find the y-indices of the mean values
[~,idx] = min( bsxfun(#minus, double(I(:,x)), m) );
%# show the result
figure(1)
imshow(I, 'InitialMagnification',100, 'Border','tight')
hold on, plot(x, idx, ...
'Color','r', 'LineStyle','none', 'Marker','.', 'MarkerSize',10)
and applied on the smaller image:

Applying MATLAB's idwt2 several times

I am using MATLAB to apply the Discrete Wavelet Transform on an image. I am applying it several times (3) in order to get a 3 level transform. I am using the dwt2 function provided by MATLAB in order to compress and idwt2 to make the decompression. The problem is that I do not know how to decompress several times, as in apply idwt2 several times to the previous received output, as it returns a matrix. Take for example:
x = idwt2(scaled3, vertical3, horizontal3, diagonal3, Lo_R, Ho_R);
How should idwt2 be applied to x?
Looking at the documentation for dwt2 and idwt2, it appears that you have 2 general options for reconstructing your multiply-decomposed images:
Store all of the horizontal, vertical, and diagonal detail coefficient matrices from each decomposition step and use them in the reconstruction.
Enter an empty matrix ([]) for any detail coefficient matrices that you didn't save from previous decomposition steps.
Since it was a slow day, here's some code showing how to do this and what the results look like for each case...
First, load a sample image and initialize some variables:
load woman; % Load image data
nLevel = 3; % Number of decompositions
nColors = size(map, 1); % Number of colors in colormap
cA = cell(1, nLevel); % Approximation coefficients
cH = cell(1, nLevel); % Horizontal detail coefficients
cV = cell(1, nLevel); % Vertical detail coefficients
cD = cell(1, nLevel); % Diagonal detail coefficients
Now, apply the decompositions (in this case 3) and store the detail coefficient matrices from each step in a cell array:
startImage = X;
for iLevel = 1:nLevel,
[cA{iLevel}, cH{iLevel}, cV{iLevel}, cD{iLevel}] = dwt2(startImage, 'db1');
startImage = cA{iLevel};
end
To see what the final decomposed image looks like, along with all the detail coefficient matrices along the way, run the following code (which makes use of wcodemat):
tiledImage = wcodemat(cA{nLevel}, nColors);
for iLevel = nLevel:-1:1,
tiledImage = [tiledImage wcodemat(cH{iLevel}, nColors); ...
wcodemat(cV{iLevel}, nColors) wcodemat(cD{iLevel}, nColors)];
end
figure;
imshow(tiledImage, map);
You should see something like this:
Now it's time to reconstruct! The following code performs a "full" reconstruction (using all of the stored detail coefficient matrices) and a "partial" reconstruction (using none of them), then it plots the images:
fullRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
fullRecon = idwt2(fullRecon, cH{iLevel}, cV{iLevel}, cD{iLevel}, 'db1');
end
partialRecon = cA{nLevel};
for iLevel = nLevel:-1:1,
partialRecon = idwt2(partialRecon, [], [], [], 'db1');
end
figure;
imshow([X fullRecon; partialRecon zeros(size(X))], map, ...
'InitialMagnification', 50);
Notice that the original (top left) and the "full" reconstruction (top right) look indistinguishable, but the "partial" reconstruction (lower left) is very pixelated. The difference wouldn't be as severe if you applied fewer decomposition steps, like just 1 or 2.
% Multi-level reconstruction from DWT coefficients
% The variable "coefs" is what you get when you perform forward dwt2()
% on the image you're decomposing. It is a long row
% vector that has cA- approximation details, cH -horizontal details, cV-
% vertical details, cD-diagonal details
L=3; % For db 3-level reconstruction for example
k=size(image,1)/2^L; % I am assuming a square sized image where both
% dimensions are equal
for level=0:(L-1)
s=k*2^level;
if level==0
cA=reshape(coefs(1,1:s^2),s,s);
figure;imshow(cA,[])
end
cH=reshape(coefs(1,(s^2+1):2*s^2),s,s);
figure;imshow(cH,[])
cV=reshape(coefs(1,(2*s^2+1):3*s^2),s,s);
figure;imshow(cV,[])
cD=reshape(coefs(1,(3*s^2+1):4*s^2),s,s);
figure;imshow(cD,[])
I_rec=idwt2(cA,cH,cV,cD,"db1");
figure;imshow(I_rec,[])
cA=I_rec; % The recosntructed image is the approximation detail-cA
% for next levels of reconstruction
end