i am making image compression in matlab.
After i applied DCT on image and i had img matrix, i want to apply a threshold mask on that matrix.
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
maskedImg = blkproc(img,[8 8],mask );
I used that function but it didnt work and i get error message:
Error in ==> blkproc at 67
[a, block, border, fun, params, padval] = parse_inputs(varargin{:});
According to latest Matlab documentation; closest blockproc syntax (for your case) is B = blockproc(A,[M N],fun). So apparently your mask really should be a function!
However, I recall that blkproc has been a valid Matlab function for a while ago, thus double check the proper way to call it by typing (in the command line) > help blkproc. (Al tough I'm quite confident that it will share the calling signature with blockproc [in this case]).
Related
I have a binary image. I have several single pixels in images. Single pixels are white (1) and all of their neighborhoods are black (0). for example image below shows a single pixel (at center) and two pixels (at left-bottom):
0 0 0 0 0
0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
1 1 0 0 0
How can I remove single pixels with morphological operations in Matlab?
I give you another option without loop, using a 2D convolution with conv2:
M = [0 0 0 0 0
0 0 1 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0]
C = [0 1 0
1 1 1
0 1 0]; % The matrice that is going to check if a `1` is alone or not.
%if you also want to consider the neibhbors on the diagonal choose:
%C = ones(3);
R = M.*conv2(M,C,'same')>1 %Check for the neighbors.
RESULT
R =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
Upon request by the OP, I'm converting my short comment into a reply:
Since you explicitly asked for morphological operations: bwmorph has a 'clean' option which is described as "Removes isolated pixels (individual 1s that are surrounded by 0s)" with an example close to yours. Have a look at the bwmorph documentation page.
As in your previous question, you can use bwboundaries:
if P is the binary image, than:
B = bwboundaries(P,8);
for k = 1:numel(B)
if size(B{k})<=2
P(B{k}(1,1),B{k}(1,2)) = 0;
end
end
So for the example above P becomes:
P =
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
1 1 0 0 0
I have problem with this code (i reduced it before posting)
epsilon=0.60; t=-3.0; n=11; E=+0.34; I=eye(n,n); eta=1.0e-10;
DB = gallery('tridiag',t*ones(1,n-1),epsilon*ones(1,n),t*ones(1,n-1));
ODB1 = sparse(1:n,1:n,t*heaviside((-1).^(2:n+1)));
ODB2 = sparse(1:n,1:n,t*heaviside((-1).^(1:n)));
a=(E*I-DB)\ODB2';b=(E*I-DB)\ODB1;
p0=(E*I-DB)\ODB1';q0=(E*I-DB)\ODB2;
%initial
p=(I-a*q0-b*p0)\(a*p0);q=(I-a*q0-b*p0)\(b*q0);
tmp1=p; tmp2=q;
while norm([p q]) > 1.0e-8
A=(1+eta*1i)*I-p*q-q*p; (1)
x=A\I; (2)
p=x*p*p; q=x*q*q; (3)
%p=((1+eta*1i)*I-p*q-q*p)\(p*p); (4)
%q=((1+eta*1i)*I-p*q-q*p)\(q*q); (5)
tmp1=tmp1+tmp2*p;
tmp2=tmp2*q;
end
Matlab shows an error on RCOND if i replace (1-3) lines by (4-5). So what's difference between them? Can i trust the result?
Your matrix p*p is:
ans =
39.2012 0 -28.3944 0 10.1201 0 10.6411 0 -28.4320 0 38.6426
16.3606 0 -11.9292 0 4.3186 0 4.4104 0 -11.9335 0 16.2645
-28.3944 0 20.9268 0 -7.6333 0 -7.6709 0 20.8517 0 -28.4320
-28.2897 0 20.6792 0 -7.5188 0 -7.6149 0 20.6748 0 -28.1980
10.1201 0 -7.6333 0 3.1358 0 2.5773 0 -7.6709 0 10.6411
32.6084 0 -23.8794 0 8.7457 0 8.7457 0 -23.8794 0 32.6084
10.6411 0 -7.6709 0 2.5773 0 3.1358 0 -7.6333 0 10.1201
-28.1980 0 20.6748 0 -7.6149 0 -7.5188 0 20.6792 0 -28.2897
-28.4320 0 20.8517 0 -7.6709 0 -7.6333 0 20.9268 0 -28.3944
16.2645 0 -11.9335 0 4.4104 0 4.3186 0 -11.9292 0 16.3606
38.6426 0 -28.4320 0 10.6411 0 10.1201 0 -28.3944 0 39.2012
on the first run. its reciprocal condition number (something like condition number but scaled to 1-0 range) , rcon(p*p) is equal to zero, meaning its unsolvable algebraically.
I do not know what your matrices mean, but you can not solve the system you proposed in your code with the values of this equation.
how can i count number of particles in each grid box in this code
here is my code below:
xyRange=[1,5];
P=3;
vx=0.6;
vy=0.4;
X=[];
Y=[];
for day=1:5
X=[X;randi(xyRange,P,1)];
Y=[Y;randi(xyRange,P,1)];
X=X+vx;
Y=Y+vy;
end
plot(X,Y,'kd');
grid on;
axis([1,50,1,50]);
j = floor(X/5)+1;
k = floor(Y/5);
box = k*10+j;
If you have the Statistics Toolbox, the easiest way is to use hist3.
In your case, when I plotted the grid, it looks like each box was separated in units of 5. As such, the command is very simply this:
cnt = hist3([X,Y], {0:5:50 - 2.5, 0:5:50 - 2.5});
X and Y are your 2D data points, and the second element is a cell array of X and Y values which denote the centres of each of the points in each grid. Take note that the points that are defined are with respect to the origin being at the top left corner. If you want to make sure that the origin is at the bottom left corner, you would actually need to do this:
cnt = flipud(cnt.');
On my run I get this:
cnt =
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 1 6 0 0 0 0 0 0 0 0
0 5 3 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
When producing your plot, I get this:
If you compare the counts in comparison to the grid produced at the top, you'll see that the match. However, because of the way I specified the centre of the bins, the first and last row, and the first and last column are meaningless, so you can safely eliminate these from your analysis.
If you want a nice pictorial example of this, call hist3 without any output arguments:
%// Plot 2D histogram with some transparency
hist3([X,Y], {(0:5:50) - 2.5, (0:5:50) - 2.5}, 'FaceAlpha', 0.65);
%// Set height of each bar coloured according to height
set(get(gca,'child'),'FaceColor','interp','CDataMode','auto');
view(-34,68); %// Change camera view for better look
We get this:
I am trying to solve the linear system of equations A'*x = B using Matlab's "mldivide" (the backslash operator) in the form:
x_transp = A'\b;
A is a square sparse matrix and that is all I know about it.
The problem is that the transpose has no effect at all, so the result of the previous line of code is the same than:
x = A\b;
So, x = x_transp. However, either if I use a new variable such that:
A_transp = A';
x_transpOK1 = A_transp\b;
or simply use:
x_transpOK2 = transp(A)\b;
the result is different (x_transpOK1 = x_transpOK2 ≠ x = x_trans).
This behavior occurs in Matlab version 7.9.0 (R2009b) but it does not happen in 7.12 (R2011a).
This, however, does not happen with silly examples I have tried (the behavior then is correct). The matrices that make this behavior arise are:
A =[0.01 -0.495 0 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 -0.495 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1];
b = [8
4
0
0
0
0
0
0
0
0];
Is it some kind of precision issue? Am I making any fundamental error I cannot see?
The guys at Mathworks replied: it is a bug in the interpreter, which have been fixed in the next versions. There is no fix for 7.9.0 and they recommend the following workaround:
A_transp = A';
x = A_transp\b;
I guess this is a great example of the typical advice to always be up-to-date...
My original post on Matlab Answers
The bug report
After all the discussion, here is my answer:
#Mario_Exec.bat, it seems to me that you might want to take this to the Matlab Answers (mathworks.com/matlabcentral/answers) as maybe someone with knowledge of the actual code (ie a Matlab employee) might be able to help you more specifically. It is an interesting question but it seems there is more going on that might need more knowledge of the actual code and decision trees.
Please do post back here when you hear back. I am curious what they say!
I have a 480-by-640 matrix A. For each pixel, I want to check its neighbors. The neighbors of the pixel are determined by a value N. For example, this is a part of matrix A where all the zeros are the neighbours of pixel X when N=3:
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 X 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
As shown, because N=3, all these zeros are pixel X's neighbors. The problem is if X is located before the index N=3. Here the neighbors will be pixels with one values:
X 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
Could anyone advise on how to handle this?
The simplest way to proceed is just to pad your array with with values that do not return true for whatever you are checking (say, if you're looking for nonzeros, pad with zeros, or if you're looking for finite values, pad with NaN.) The padarray function can do this for you, but requires the Image Processing Toolbox*. Otherwise, you can pad arrays yourself. For example, an unoptimized way to proceed might be
A = rand(m,n);
Apadded = [zeros(N,2*N+n); [zeros(m,N), A, zeros(m,N)]; zeros(N,2*N+n)];
for i = N+1:N+m+1
for j = N+1:N+n+1
% Process neighborhood of A(i,j)
end
end
*Also note that these sorts of "sliding neighborhood" operations, being common in image processing, are implemented for you in the Image Processing Toolbox.