Converting realtime video into an array using Matlab - arrays

I have a problem with realtime video processing in Matlab for gesture recognition. I have tried
n = 20; % 'n' is the number of frames i want to capture
%preallocating frame()
for i=1:n
frame(:,:,:,i) = getsnapshot(vid);
end
But when I try to access each array using the following loop
for i=1:n
image=frame(:,:,:,i);
y=imresize(image,[50, 50]);
b=rgb2gray(y);
%%processing for features
end
the array b always contains the value 1. Is there anything wrong with my implementation? Do I always have to convert each frame into an image file?

The problem is most likely in the only code snippet you haven't provided :P
When you preallocate the frame array make sure it's something like:
frame = zeros(720,1280,3,N,'uint8');
Only calling zeros() with no cast will allocate it as an array of doubles. It seems that using 'int8' also gives unwanted results. Maybe this is different on your machine.

Related

How to use iddata type structure

I want to resample an array of elements using the command idresamp(). The input arguments for idresamp function is an array x. So I should get the output as an array. However, I am getting a structure iddata. I don't know how to access the elements /result of the resampling. Can somebody please show how to access the resampled values? Thank you.
x=rand(4000,1); %create some arbitrary data
x_resamp =idresamp(x,2); %resampling factor is 2
Here x_resamp is of iddata type. So, I am unable to access the result. On clicking the variable x_resamp this is what I got
How does one access the resampled values (output). Where is the array? The next step is to calculate the power after resampling and hence I need to use the resampled values.
I am using Matlab R2018a.
If you just want to resample by a factor 2, and have access to the Signal Processing Toolbox, use resample:
y = resample(x,2,1);
If you are insistent on using idresamp, you need to know that it returns an object of type iddata, which comes with a long documentation on usage. I think this complicates things more than you are looking for. It seems you should be able to do:
x_resamp = idresamp(x,2);
y = x_resamp.OutputData;
(but I can't test this because I don't have access to this toolbox.)

Strange behavior with Matlab array

I am having some trouble manually creating a histogram of intensity values from a grayscale image. Below is the code that I am using the create the bins for the plot that I want to create. The code works fine for every bin except for the last two. For some reason if the intensity is 254 or 255 it puts both values into the 254 bin and no values are accumulated in the 255 bin.
bins= zeros(1,256);
[x,y]=size(grayImg);
for i = 1:x
for j = 1:y
current = grayImg(i,j);
bins(current+1) = bins(current+1) + 1;
end
end
plot(bins);
I do not understand why this behavior is happening. I have printed out the count of 254 intensities and 255 intensities and they are both correct. However, when using the above code to accumulate the intensity values it does not work correctly.
Edit: Added the image I am using, the incorrect graph(the one I get with above code), and the correct one
A. The first problem with your code is the initial definition of bins. It seems that you come from C or somthing like that, but the definition should be- bins=zeros(1,256);
B. The second point is that you don't need the nested loop, you have a matlab function especially for that:
bins=hist(grayImg(:),1:256); % now, you don't need the pre-definition for 'bins'.
plot(bins);
C. Try to use functions like bar or imhist or hist(grayImg(:)), it may save you all this, and give a nice plot.

MATLAB feedforwardnet

I am still new in using MATLAB. I am trying to create a feed-forward neural network with MATLAB custom function feedforwardnet, I got my own training set with dimension 2 x 100, which is a multiple-input array.
Now I ran into the problem of how to feed this array into the feedforwardnet function. I tried
[NNINputs, NNTargets] = [n_xk, target] ;
where both n_xk and target are 2 x 100 "double" arrays. It shows the error:
??? Too many output arguments.
I also tried wrapping the array into a cell array as follows
myTrainSet = num2cell([n_xk, target]) ;
This time, it shows
??? Undefined function or method 'feedforwardnet' for input arguments of type 'double'.
But how? the neural network runs like a charm with the default dataset
load house_dataset ;
which is also a 13 x 251 arrays both its input and output. How come it can't work with my cell array or numeric matrix? I've searched through the internet and no much literature covers this topic, and if it does, the discussion is still unclear. Thanks for the help in advance!

Storing realcolor images in an array for MATLAB

I am working on an m file that would take out single frames from a bigger image and play them as an animation. So far I managed to create the algorithm to locate and crop individual frames.
I can also store them in cell arrays. Almost everything is already done really.
My problem is that I can't get them to animate. I used the animation functions but they do not work. The reason being is that they are in cell arrays instead of just 4D arrays.
I want to store each frame in a nXmX3X(frame_number) array. How can I do that? How can I replace only the nXm part of an array?
Thank you.
if you have a cell array cFrames with n cells each storing the k-th frame of size m-by-n-by-3, you can use cat to create the desired 4D array
>> frames4d = cat(4, cFrames{:} );
Note: all frames in cFrames must have the same size for this to work.

Image/2D array resampling in C

I am looking to implement a resampling algorithm for a 2D array(it could be grayscale image or some 2D array of floating point values).
The steps involved in this particular operation are:
Given a 2D array, I first downsample it to size of 8x8 or 16x16, using some down-sampling method(preferably with a preceeding anti-aliasing filtering).
Some nuemrical operation on this.
Then upsample it back to its original size by doing, bilinear interpolation.
As a prototype I coded it as shown below in Octave. It gives decent results. I am looking to get some reference on C implementation.
fid = fopen("anti_vig_gain_map.txt","r");
fid2 = fopen("ds_us_anti_vig_gain_map.txt","w");
for i=1:1968
for j=1:2592
map(i,j) = fscanf(fid,'%f\n',1);
end
end
%downsample
ds_map = imresize(map,[8 8],'linear');
%% some processing on ds_map
%upsample
us_map = imresize(ds_map,[1968 2592],'linear');
I tried to see the code in imresize.m but it gets complicated after sometime and could not extract C code out of it.
Any pointers to reference C code for bilinear interpolation to perform the upsampling.
Also looking to get some pointers for the the anti-aliasing filter and down-sampling method using bilinear method.
I think what you are looking for is contained in the NetPBM suite. Specifically, pamscale which handles both up and down sampling with multiple possible filtering schemes for both directions. The code is both well-written and self-contained.

Resources