quadtree decomposition of wavelet coefficients - matlab

After retrieving image coeffients (both approximate and detailed) in wavelet decomposition, i want quadtree structure for appoximate and detailed coefficients. but every time i am getting same error. Please help.
I tried to make it power of 3 instead of 2 but it coudnt help. may be i am going wrong somehwhere.
code for image decomposition
X=imread('abc.tif');
close all
clf
image(X)
colormap('default')
axis ('image'); set(gca,'XTick',[],'YTick',[]); title('Original')
pause
dwtmode('sym')
wname = 'bior4.4'
t = wtree(X,2,'bior4.4');
plot(t)
pause
close(2)
[wc,s] = wavedec2(X,5,wname);
a1 = appcoef2(wc,s,wname,1);
h1 = detcoef2('h',wc,s,1);
v1 = detcoef2('v',wc,s,1);
d1 = detcoef2('d',wc,s,1);
a2 = appcoef2(wc,s,wname,2);
h2 = detcoef2('h',wc,s,2);
v2 = detcoef2('v',wc,s,2);
d2 = detcoef2('d',wc,s,2);
a3 = appcoef2(wc,s,wname,3);
h3 = detcoef2('h',wc,s,3);
v3 = detcoef2('v',wc,s,3);
d3 = detcoef2('d',wc,s,3);
a4 = appcoef2(wc,s,wname,4);
h4 = detcoef2('h',wc,s,4);
v4 = detcoef2('v',wc,s,4);
d4 = detcoef2('d',wc,s,4);
a5 = appcoef2(wc,s,wname,5);
h5 = detcoef2('h',wc,s,5);
v5 = detcoef2('v',wc,s,5);
d5 = detcoef2('d',wc,s,5);
For quadtree i am using:
S = qtdecomp(I,.27); %I is image in greyscale.
blocks = repmat(uint8(0),size(S));
for dim = [512 256 128 64 32 16 8 4 2 1];
numblocks = length(find(S==dim));
if (numblocks > 0)
values = repmat(uint8(1),[dim dim numblocks]);
values(2:dim,2:dim,:) = 0;
blocks = qtsetblk(blocks,S,dim,values);
end
end
blocks(end,1:end) = 1;
blocks(1:end,end) = 1;
imshow(I), figure, imshow(blocks,[])
the error it is showing is:
??? Error using ==> qtdecomp>ParseInputs at 229
MAXDIM / MINDIM is not a power of 2
Error in ==> qtdecomp at 88
[A, func, params, minDim, maxDim] = ParseInputs(varargin{:});
or
??? Error using ==> qtdecomp>ParseInputs at 145
A must be two-dimensional
Error in ==> qtdecomp at 88
[A, func, params, minDim, maxDim] = ParseInputs(varargin{:});

The error is pretty clear. Your image must have dimensions that belong to a power of two. As such, you need to resize your image so that it follows this. qtdecomp computes the quadtree decomposition algorithm, and a pre-condition for this function is that it NEEDS to have an image that has each dimension as a power of two as input.
You asked something similar in another question that I provided an answer to. What is curious is that this other question you asked was 6 days ago before this one, and you accepted my answer for that other one. The issue that you were experiencing is more or less the same thing.
Quadtree decomposition

Related

how to generate 64 or 128 qam in matlab

How to generate 64 or 128 QAM signal in MATLAB without making use of signal processing/Communication tool box. Is there any general method for creating higher order qams?
Let's say you want to generate a QAM for M=2^b. Then you need constellation points that form a lattice of M_I = 2^b_I by M_Q = 2^b_Q points where M = M_I*M_Q and therefore b_I+b_Q==b. Distributing evenly, we get b_I = floor(b/2); and b_Q = ceil(b/2);.
This established, you set up the lattice by forming 1-D grids for real and imaginary part and then connect them to a 2-D lattice. Like this:
b = 6;
M = 2^b; % b = 6: 64-QAM, b = 7: 128-QAM, etc.
b_I = floor(b/2);
M_I = 2^b_I;
b_Q = ceil(b/2);
M_Q = 2^b_Q;
x_I = -(M_I-1)/2:(M_I-1)/2;
x_Q = -(M_Q-1)/2:(M_Q-1)/2;
x_IQ = repmat(x_I,[M_Q,1]) + 1i*repmat(x_Q.',[1,M_I]);
x_IQ = x_IQ / sqrt((M_I^2+M_Q^2-2)/12);
Note that in R2016b, the second-to last line can be simplified to x_IQ = x_I + 1i*x_Q.'; Moreover, the last line ensures that your constellation to has average energy per symbol of 1 (note: for even b, the normalization can be simplified to sqrt((M-1)/6)).
Now, x_IQ is an array that contains all possible constellation points.
To draw random symbols, use s = x_IQ(randi(M,NSymbols,1));
Note that for odd b this script generates a rectangular QAM (e.g., for 128-QAM it is a 16x8 constellation). There are other ways to do so by placing constellation points more symmetrical. There is no unique way of doing that though so if you need one you have to specify which. Though in practice one usually uses QAM with even b.
I found the answer! If it is 64,256 or square of any number, then it can be easily generated as below.
% matlab code to generate 64 qam
m = 0; n = 0;
for k = -7:2:7
m = m+1;
for l = -7:2:7
n = n+1;
x(m,n) = k+j*l;
end
n = 0;
end

Carrying out edge detection on the ROI of image in Matlab

How can do edge detection on the ROI (only) of an image without processing the rest of the image? I have tried the following but it is not working:
h4 = #(x) edge(x,'log');
Edge_map = roifilt2(Foregound_Newframe,roi_mask,h4);
roi_mask is the binary mask that I am using and Foregound_Newframe is the gray image to be processed. Kindly provide an example. Thanks.
The error I see is that the function you are using to do the filtering requires input argument of type double, otherwise your calling syntax should work fine.
i.e. use
YourFilter = #(x) edge(double(x),'log');
When I apply this to an example fromroifilt2 docs it works fine (ok it looks weird in this case...):
clc
clear
FullImage = imread('eight.tif');
roi_col = [222 272 300 270 221 194];
roi_row = [21 21 75 121 121 75];
ROI = roipoly(FullImage,roi_col,roi_row);
YourFilter = #(x) edge(double(x),'log');
J = roifilt2(FullImage,ROI,YourFilter);
figure, imshow(FullImage), figure, imshow(J)
with following output:

Speeding up symbolic recursion in Matlab

I have a backwards recursion for a binomial tree. At each node an unknown C enters in such a way that at the starting node we get a formula, A(1,1), that depends upon C. The code is as follows:
A=sym(zeros(1,Steps));
B=zeros(1,Steps);
syms C; % The unknown that enters A at every node
tic
for t=Steps-1:-1:1
% Values needed in A and B
Lambda=1-exp(-(1./S(t,1:t).^b).*h);
Q=((1./D(t))./(1-Lambda)-d)/(u-d);
R=normcdf(a0+a1*Lambda);
% the backward recursion for A and B
A(1:t)=D(t)*C+D(t)*...
(Q.*(1-Lambda).*A(1:t) ...
+ (1-Q).*(1-Lambda).*A(2:t+1));
B(1:t)=Lambda.*(1-R)+D(t)*...
(Q.*(1-Lambda).*B(1:t)...
+ (1-Q.*(1-Lambda).*B(2:t+1)));
end
C = solve(A(1,1)==sym(B(1,1)),C);
This code takes around 4 seconds if Steps = 104. If however we remove C and set matrix A to a regular double matrix, it only takes about 0.02 seconds. Using syms thus increases the calculation time by a factor 200. This seems too much to me. Any suggestions into speeding this up?
I am using Matlab 2013b on a MacBook air 13-inch spring 2013. Furthermore, if you're interested in the code before the above part (not sure whether it is relevant):
a0 = 0.9;
a1 = -3.2557;
b = 1.2594;
S0=18.57;
sigma=0.6579;
h=1/104;
T=1;
Steps=T/h;
f=transpose(normrnd(0.04, 0.001 [1 pl]));
D=exp(-h*f); % discount values
pl=T/h; % pathlength - amount of steps in maturity
u=exp(sigma*sqrt(h));
d=1/u;
u_row = repmat(cumprod([1 u*ones(1,pl-1)]),pl,1);
d_row = cumprod(tril(d*ones(pl),-1)+triu(ones(pl)),1);
path = tril(u_row.*d_row);
S=S0*path;
Unless I'm missing something, there's no need to use symbolic math or use an unknown variable. You can effectively assume that C = 1 in your recursion relation and solve for the actual value at the end. Here's the full code with some other improvements:
rng(1); % Always seed your random number generator
a0 = 0.9;
a1 = -3.2557;
b = 1.2594;
S0 = 18.57;
sigma = 0.6579;
h = 1/104;
T = 1;
Steps = T/h;
pl = T/h;
f = 0.04+0.001*randn(pl,1);
D = exp(-h*f);
u = exp(sigma*sqrt(h));
d = 1/u;
u_row = repmat(cumprod([1 u*ones(1,pl-1)]),pl,1);
d_row = cumprod(tril(d*ones(pl),-1)+triu(ones(pl)),1);
pth = tril(u_row.*d_row);
S = S0*pth;
A = zeros(1,Steps);
B = zeros(1,Steps);
tic
for t = Steps-1:-1:1
Lambda = 1-exp(-h./S(t,1:t).^b);
Q = ((1./D(t))./(1-Lambda)-d)/(u-d);
R = 0.5*erfc((-a0-a1*Lambda)/sqrt(2)); % Faster than normcdf
% Backward recursion for A and B
A = D(t)+D(t)*(Q.*(1-Lambda).*A(1:end-1) + ...
(1-Q).*(1-Lambda).*A(2:end));
B = Lambda.*(1-R)+D(t)*(Q.*(1-Lambda).*B(1:end-1) + ...
(1-Q.*(1-Lambda).*B(2:end)));
end
C = B/A
toc
This take about 0.005 seconds to run on my MacBook Pro. There are certainly other improvements you could make. There are many combinations of variables that are used in multiple places (e.g., 1-Lambda or D(t)*(1-Lambda)), that could be calculated once. Matlab may try to optimize this a bit. And you can try moving Lambda, Q, and R out of the loop – or at least calculate parts of them outside and save the results in arrays.

image reconstruction using matlab from image coefficients

I am trying to execute this function for image reconstruction where
ra, rh, rv, rd are reconstructed coefficients. but i am facing problem in addition and subtraction.
Please help.
Xhat = ra2 + rh2 + rv2 + rd2 + rh1 + rv1 + rd1;
sprintf('Reconstruction error (using wrcoef2) = %g', max(max(abs(X-Xhat))))
OR
XXhat = waverec2(wc,s,wname);
sprintf('Reconstruction error (using waverec2) = %g', max(max(abs(X-XXhat)))
I decomposed the image using:
>> a1 = appcoef2(wc,s,wname,1);
>> h1 = detcoef2('h',wc,s,1);
>> v1 = detcoef2('v',wc,s,1);
>> d1 = detcoef2('d',wc,s,1);
>> a2 = appcoef2(wc,s,wname,2);
>> h2 = detcoef2('h',wc,s,2);
>> v2 = detcoef2('v',wc,s,2);
>> d2 = detcoef2('d',wc,s,2);
Then reconstructed using above parameters.
Now i have to comnbine them.
I'm guessing your problem is almost certainly at the abs(X-Xhat) line.
Why? You seem to be doing some sort of wavelet decomposition/reconstruction, and if you don't pass in the right parameters your output may be larger than the original image. Therefore it makes no sense to ask for X-Xhat if these are of different sizes and you will get an error message.
The best way of fixing this is to when you reconstruct (presumably using upcoef2), is to pass size as an additional parameter to truncate. A truncated (heh) and adjusted example from the docs:
load woman;
[c,s] = wavedec2(X,2,'db4');
siz = s(size(s,1),:);
ca1 = appcoef2(c,s,'db4',1);
a = upcoef2('a',ca1,'db4',1,siz);
a2 = upcoef2('a',ca1,'db4',1);
You'll see that size(X) and size(a) are both 256 x 256, but size(a2) is larger. Therefore a-X is fine and a2-X will give you a "Matrix dimensions must agree." error.

Equations Solver function in Matlab. How do I handle [eqns,vars] = getEqns(varargin{:}) errors?

I am using Matlab and am using its solve function. I run this code and I keep getting this error
[eqns,vars] = getEqns(varargin{:})
I realized I had some parens issues and fixed those but it still does work any ideas?
Asp = 3.90;
Arg = 12.48;
Lys = 10.54;
His = 6.04;
Glu = 7.70;
Gly = 7.50;
Val = 7.44;
Pro = 8.36;
Ser = 6.93;
Thr = 6.82;
Tyr = 10.07;
Ala = 7.59;
Met = 7.00;
Cys = 5.02;
syms pH;
solve( '55*(10.^(pH-Glu))/(1+(10.^(pH-Glu))) + 43*(1/(1+(10.^(pH-Arg))))+ 55*(10.^(pH-
Asp))/(1+(10.^(pH-Asp))) + 22*(1/(1+(10.^(pH-Lys))))+ 13*(10.^(pH-Tyr))/(1+(10.^(pH-Tyr)))
+ 6*(1/(1+(10.^(pH-His)))) + 1*(10.^(pH-Cys))/(1+(10.^(pH-Cys)))');
I believe the error is due to your equation. Whether 'pH' is positive or negative, your equation can never equal zero, since all the subtraction occurs in the exponent. Have you checked the signs in the rest of the equation?