Copying part of array to a second array in C - 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];

Related

How do I replace an array element in LabView? (2d array of pictures)

so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.

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

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));

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));

Confused in how to print an array hellically

I have been trying to figure out how can we print a array hellically but i am stuck on how to get started.Any algorithms or ideas will be very helpful.Thanks
HELLICALLY means printing the array in concentric circular shape
If I'm interpreting what you're saying correctly, you want to print the contents of an array, but in a spiral.
I would start by allocating a big rectangular block of memory (a 2-D array) and initializing it to zero. This represents the screen. Then devise a function for determining the coordinates of the next point in the circle and make some coordinate variables initialized to the origin point. Fill the screen by dropping array members wherever they go.
Print out the screen-array, one row at a time, but substitute space for zero.
Screen size and next-coordinate-function are left as exercises for the reader.

Resources