The below code is not running in matlab. I am trying an anonymous function in matlab.Can you pls tell me whats wrong in this line :
Inv_Y_Quant = blockproc(BB,[8 8], InvQuant);
and the related code is below:
clear all
clc
I = imread('cameraman.tif');
% convert it to double
I = im2double(I);
% "Trim by 128"
I = I-128;
% Generate the DCT matrix
T = dctmtx(8);
% Generate Function handler for DCT
MyFun1 = #(block_struct) T * block_struct.data * T';
% BlockProcess the DCT the function for 8 by 8 blocks
B = blockproc(I,[8 8],MyFun1);
% Form the Quantization matrix
Q = [ 16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99];
% now generate function handler for the quantization
MyFun2 = #(block_struct)block_struct.data ./Q;
% perform the quantization
BB = blockproc(B,[8 8],MyFun2);
InvQuant = #(block_struct)round(block_struct.data.*Q);
Inv_Y_Quant = blockproc(BB,[8 8], InvQuant);
InvDct = #(block_struct)dct_matrix'*block_struct.data*dct_matrix;
Z = blockproc(Inv_Y_Quant, [8 8], InvDct);
Z = Z+128;
figure, imshow(Z)
Z = uint8(Z);
figure, imshow(Z)
%imwrite(Z, 'Mar7.tif');
%b = imread('Mar7.tif');
%imshow(b)
As you wrote in your comment, the problem is that you try to use array dct_matrix in anonymous function InvDct. This array was never defined in your code.
Did you meant to use matrix T (init as dctmtx(8))?
edit:
When using parameters in anonymous functions (as dct_matrix in this example), these parameters should be defined before the anonymous function is defined.
Related
Consider the matrix below:
A = randi([0 100], 5, 7)
62 59 20 42 49 41 83
37 34 13 65 82 47 24
93 33 19 93 30 37 20
42 49 74 96 26 41 21
14 85 14 90 77 4 96
first I want to find the maxima of each row and the column index:
83 7
82 5
93 1
96 4
96 7
then I need to find the row with the lowest maxima:
82 5 2
so far, I have implemented this:
close all;
clear all;
clc;
A = randi([0 100], 5, 7);
[M1, I1] = max(A, [], 2);
[M1, I1]
[M2, I2] = min(M1);
[M2, I1(I2), I2]
Though it feels like a workaround, I wondered if there is a more canonical way to do this? Maybe a built-in function or min/max syntax for this specific purpose?
P.S. Shared this question also on the MATLAB Discord channel.
What about this with find?
M = min(max(A,[],2));
[i,j] = find(A == M);
[M,j,i]
Based on a previous question (read coordinate text file for regionprop usage - Matlab) I wish to plot the object with an extended boundaries (zeros+10) but it does not work. Any reason why?
Code:
clc;
clear;
filename = fullfile('E:/outline.txt');
fileID = fopen(filename);
C = textscan(fileID,'%d %d');
fclose(fileID);
xMax = double(max(C{1})-10)
yMax = double(max(C{2})+10)
bw=roipoly(zeros(xMax ,yMax ),C{1},C{2});
imshow(bw);
stats = regionprops(bw);
coordinate text file content is as follow:
88 10
87 11
87 12
88 13
88 14
92 21
93 22
93 23
94 24
95 25
100 33
101 34
102 34
103 34
103 33
103 32
103 31
103 30
103 29
103 28
103 27
102 26
102 25
101 24
101 23
100 22
100 21
100 20
99 19
99 18
94 12
93 12
92 12
91 11
90 11
xMax = double(max(C{1})-10)
That should probably be +10. This way you make the image smaller than your polygon.
If you want to extend the image on the left side also, add an offset to your polygon coordinates:
bw = roipoly(zeros(yMax, xMax), C{1}+5, C{2}+5);
Note also that I swapped xMax and yMax from your code, this might be another issue you're seeing. Matrix dimensions are specified as (height, width), as are indices into the matrix. But some functions such as roipoly take coordinates with x first and y second. This is a common pitfall with MATLAB syntax.
I just learned that the above is the same as
bw = roipoly(yMax, xMax, C{1}+5, C{2}+5);
I am trying to apply dct and quantization to 8*8 blocks like this.
img = imread('frames/frm001.png');
ycbcr = rgb2ycbcr(img);
y = ycbcr(:,:,1);
func = #dct2;
whos func;
d1 = blkproc(y,[8 8],func);
qmtx = [16 11 10 16 24 40 51 61;
12 12 14 19 26 58 60 55;
14 13 16 24 40 57 69 56;
14 17 22 29 51 87 80 62;
18 22 37 56 68 109 103 77;
24 35 55 64 81 104 113 92;
49 64 78 87 103 121 120 101;
72 92 95 98 112 100 103 99];
d1 = ceil(d1);
whos d1;
c = #(block_struct) (block_struct.data) ./ qmtx;
d2 = blkproc(d1,[8,8], c );
whos d2;
For this I'm getting the following error
Attempt to reference field of non-structure array.
Error in #(block_struct)(block_struct.data)./qmtx
Error in blkproc (line 103)
firstBlock = feval(fun,x,params{:});
Error in dct (line 28)
d2 = blkproc(d1,[8,8], c );
What might have gone wrong..?
I need to combine each column in a row into a single number
A = [8 1 6
3 5 7
4 9 2];
My goal is to have this form:
B = [816; 357; 492]
There are a few ways that you can do this. The fastest is likely to consider that each row of your matrix contains digits in base-10 and to combine these digits together we should multiply the first column by 10^2 (100), the second column by 10^1 (10) and the third column by 10^0 (1) and then sum across each row.
We can easily accomplish that with this one-liner which constructs all the powers of 10, and then performs matrix multiplication with A to perform the multiplication and summation.
A = [8 1 6; 3 5 7; 4 9 2];
B = A * flip(10.^(0:size(A, 2)-1))';
% 816
% 357
% 492
Another slower but possible solution would be to convert your rows to a string and then convert the string back to a number.
B = str2double(reshape(sprintf('%d', A), size(A, 2), []));
#Suever: I don't understand why did you delete the discussion, I am new in this group; any way, i put all the tests that i did and the result:
`function testtttt ()
%%%% Solution1 stackoverflow %%%%
%A=importdata('file.mat');
%B= (A * flip(10.^(0:size(A, 2)-1))')
%Result: B =
% 2.1194e+17
% 2.4989e+17
% 3.5458e+16
% 2.3669e+17
% 1.7582e+17
%%%%%%%%% Solution2 Matlab Forum %%%%%%%%%%%
% A=importdata('file.mat');
% B= num2str (A * flip(10.^(0:size(A, 2)-1))')
%Result: B =
% 211935227421357568
% 249886223928308032
% 35457727150655748
% 236691335688358080
% 175820284169194336
%%%%%%%%% Solution3 Matlab Forum %%%%%%%%%%%
% A=importdata('file.mat');
% B= dec2hex(str2num(strcat(num2str(A)')'))
%Result: B =
% D0
% E7
% 20
% E9
% A1
% 18
% 47
% 8C
% F5
% F7
% 28
% F8
.....
%%%%%%%%% Solution4 Matlab Forum %%%%%%%%%%%
A = importdata('file.mat'); % file.mat contains my matrix
[l c ] = size (A) ;
%B = cell (l,1);
for i =1 : l
B{i} = A(i, [1:16]) %however b{i} = a(i, :)
end
%Result: B =
{
[1,1] =
208 15 217 252 128 35 50 252 209 120 97 140 235 220 32 251
[1,2] =
231 174 143 43 125 66 49 143 48 139 81 103 154 229 93 229
[1,3] =
32 10 237 65 224 22 83 238 31 15 252 27 179 48 173 221
[1,4] =
233 18 178 101 90 109 225 184 210 168 183 185 190 169 96 205
[1,5] =
161 133 149 18 115 65 120 123 163 227 105 157 98 240 221 142
........
endfunction `
the best solution is the third that i found in matlab forum. Then the result found in solution 2 are caused by the using of octave. Thank you for your help.
I would like to plot a topographic map from EEG network. The electrodes (nodes) have a associated networks metric and from these values I want to interpolate between them and plot in a head shape. Here is the code that i have reseached and the result that I am getting...
=========================================
%The position X and Y as integers (electrodes position) and the value of Z (network metric)
X = [36 51 66 11 22 51 79 91 3 16 51 86 99 1 14 51 88 101 3 16 51 86 99 11 22 51 79 91 36 51 66];
Y = [99 101 99 80 85 87 85 80 66 69 70 69 66 51 51 51 51 51 36 33 32 33 36 22 17 15 17 22 3 1 3];
Z = [-404 -566 -379 -71 -102 -119 -87 9 -62 -160 -104 -81 -26 12 -120 -176 -85 -13 0 -118 -288 -159 -36 -115 -145 -292 -215 -266 -235 -364 -192];
%Making the meshgrid
for dd = 1:31
I(Xd(dd),Yd(dd))=Zd(dd);
end
Zd = [Zd; zeros(70,1)];
Xd = [Xd; zeros(70,1)];
Yd = [Yd; zeros(70,1)];
[XX,YY] = meshgrid(1:101,1:101);
z = griddata(Xd,Yd,Zd,XX,YY,'cubic');
contourf(z)
=========================================
The resulting plot of this code is
http://s16.postimg.org/s7a627s5h/Graph.jpg
I would like some help to remove this "tail" from my graph and a sugggestion of how to draw a head + nose on the same picture (only if it is possible to plot this kind of graph).
I don't have enough reputation to comment above but you can set your own custom locations in EEGlab as far as I remember. Have you looked at the function writelocs? Maybe that helps. EEGlab's topoplots include nose and ears.
http://sccn.ucsd.edu/eeglab/allfunctions/writelocs.html