How to create single image from cell array of matrices in Matlab? - arrays

I needed to split a grayscale image in equal parts so I used the function mat2cell. Then I had to equalize each of the parts separatelly, for this purpose I used the function histeq. I reused the same cell array variable for this. Here is the code:
height=round(size(img,1)/number_of_divisions);
length=round(size(img,2)/number_of_divisions);
M=zeros(number_of_divisions,1);
N=zeros(1,number_of_divisions);
M(1:number_of_divisions)=height;
N(1:number_of_divisions)=length;
aux=mat2cell(img,M,N);
for i=1:size(aux,1)
for j=1:size(aux,2)
aux{i,j}=histeq(aux{i,j},256);
end
end
So now how do I merge each cell into one single image?

Use cell2mat
img2=cell2mat(aux);
For a better performance, replace your code with blockproc
blocksize=ceil(size(img)./number_of_divisions);
img2=blockproc(img,blocksize,#(block_struct)histeq(block_struct.data));

Related

Using N-D interpolation with a generic rank?

I'm looking for an elegant way of useing ndgrid and interpn in a more "general" way - basically for any given size of input and not treat each rank in a separate case.
Given an N-D source data with matching N-D mesh given in a cell-array of 1D vectors for each coordinate Mesh={[x1]; [x2]; ...; [xn]} and the query/output coordinates given in the same way (QueryMesh), how do I generate the ndgrid matrices and use them in the interpn without setting a case for each dimension?
Also, if there is a better way the define the mesh - I am more than willing to change.
Here's a pretty obvious, conceptual (and NOT WORKING) schematic of what I want to get, if it wasn't clear
Mesh={linspace(0,1,10); linspace(0,4,20); ... linsapce(0,10,15)};
QueryMesh={linspace(0,1,20); linspace(0,4,40); ... linsapce(0,10,30)};
Data=... (whatever)
NewData=InterpolateGeneric(Mesh,QueryMesh,Data);
function NewData=InterpolateGeneric(Mesh,QueryMesh,Data)
InGrid=ndgrid(Mesh{:});
OutGrid=ndgrid(QueryMesh{:});
NewData=interpn(InGrid{:},Data,OutGrid{:},'linear',0.0)
end
I think what you are looking for is how to get multiple outputs from this line:
OutGrid = ndgrid(QueryMesh{:});
Since ndgrid produces as many output arrays as input arrays it receives, you can create an empty cell array in this way:
OutGrid = cell(size(QueryMesh));
Next, prove each of the elements of OutGrid as an output argument:
[OutGrid{:}] = ndgrid(QueryMesh{:});

Using jsonencode with length 1 array

When using the MATLAB jsonencode function it seems very difficult to convert size 1 arrays into the correct JSON format i.e. [value]. For example if I do:
jsonencode(struct('words', [string('hello'), string('bye')]))
Then this produces:
{"words":["hello","bye"]}
which is correct. If however I do:
jsonencode(struct('words', [string('hello')]))
Then it produces:
{"words":"hello"}
losing the square brackets, which it needs because it is in general an array. The same happens when using a cell rather than an array, although using a cell does work if it's not inside a struct.
Any idea how I can work around this issue?
It seems this can be solved by using a cell rather than an array and then not creating the struct inline. Like
s.words = {'hello'};
jsonencode(s)
Output:
{"words":["hello"]}
I presume when created inline the cell functionality of matlab is actually trying to make multiple structs rather than multiple strings. Note that this still won't work with arrays as matlab treats a size one array as a scalar.

Copying part of array to a second array in C

I'm writing an image processing code to perform a median filter with a variable sized window.
The greyscale image has been read into an array image1, and I'm trying to copy a window selection of the array into a second array window. This is easy for a fixed sized window (3x3 window shown) as you can just say:
window[1]=image1[m-((win_size-1)/2)][n-((win_size-1)/2)];
window[2]=image1[m][n-((win_size-1)/2)];
window[3]=image1[m+((win_size-1)/2)][n-((win_size-1)/2)];
window[4]=image1[m-((win_size-1)/2)][n];
window[5]=image1[m][n];
window[6]=image1[m+((win_size-1)/2)][n];
window[7]=image1[m-((win_size-1)/2)][n+((win_size-1)/2)];
window[8]=image1[m][n+((win_size-1)/2)];
window[9]=image1[m+((win_size=1)/2)][n+((win_size-1)/2)];
In MATLAB you can generalise this to any sized window easily by using a vector in the array call:
window = image1(m-((win_size-1)/2):m+((win_size-1)/2),n-((win_size-1)/2):n+((win_size-1)/2));
I can't work out a way to do this in C, can anyone help me with this please?
Solved by using nested for loops with a pre-defined int outside the loop. Assigned to 0 at start of first loop then +1 on each iteration.
You will have to dynamically allocate memory for an image, whatever image may be, for an array then add it to your array. I don't know exactly how to do it in C, but in C++ it would look something like:
image = new Image [5];

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.

How to copy cell to array in Matlab

Alright let me explain detailed my question
this below image is displaying my matrix where i want to copy my data
Alright now what i want to do that is as you can see 1x4 cell
i want to copy it as an array to another variable such as
input_values=ones(1,4);%init
input_values=input_matrix_training(1);
So at the above i am trying to copy the elements in that cell array which is row 1 to the input_values array. But if i do as i above i am getting this instead of the values that array contains. ty
instead of above it should be like
The other values are a cell, and are thus best referenced with {} instead of (). Also, sometimes they need to be wrapped into [], depending on the format. Plus the fact that you don't need to initialize input_values, and what you should do becomes this:
input_values=[input_matrix_training{1}];
Or you can just use cell2mat
input_values=cell2mat(input_values(1));

Resources