Issue with writeVideo/ VideoWriter MATLAB - arrays

I'm a beginner so sorry in advance for the mistakes.
I have a set of data from a camera recording saved in a 4D array with these dimensions (250x300x10603x12).
The first is the dimensions of the video (pixels). The 10603 are the FrameRatexTime. 12 are the subjects I recorded.
I extract one subject at a time for analysis in this way:
subj1 = data(:,:,:,1);
This brings me to an array containing the frames of subject 1, which I can display with implay.
Now I would like to write a video of this new array and save it in .avi format, I use this code:
v = VideoWriter('subj1.avi')
open(v)
writeVideo(v,subj1)
close(v)
but it keeps giving me this error
Error using VideoWriter/writeVideo (line 410) IMG must be an array of
either grayscale or RGB images.
In fact, looking at the shape of the array, there is nothing that points to a grayscale or RGB index. How can I get a .avi file in this case? Do I have to transform the array?
Why does it still display the video with implay?
clarification: the fact that I have to transform the array into an .avi file is because I will have to analyse it by exporting it to Python with OpenCv.
In fact, if I export the .mat file directly to Python, I can't get the list of Frames.

Matlab's documentation for writeVideo says that for a sequence of grayscale images like you have, it is expecting a "height-by-width-by-1-by-frames" array. You are only passing it "height-by-width-by-frames".
So, you need to reshape your subj1. Maybe try doing it like this:
newsubj = zeros(250, 300, 1, 10603)
newsubj(:,:,1,:) = subj1
and then save newsubj instead of subj1:
writeVideo(v,newsubj)
Finally, I think you may get some lossy compression when you save as an avi, so it may not be the best way to export it from Matlab and importing it to Python.

Related

Get Array from Nifti File using PyVista

im new to programming and im working with a MRI dataset (.nii) in PyVista
Im trying read the Nifti File and extract an array so i can compare two MRIs based on the differences in the array and visualise it with PyVista.
PyVista is mostly based on the VTK library so maybe there is a function in VTK but im a bit helpless looking through the Docs.
I found a solution in nibabel to access an array:
img = nib.load(example_filename)
a = np.array(img.dataobj)
But with that i still can't access the PyVista Array to highlight the differences.
Thank you for your help in advance!
We need to perform two distinct operations:
retrieving the intensity values from your NIfTI dataset using, for instance, nibabel
plotting the 3D numpy array using, for instance, PyVista. Take a look at the PyVista documentation here and here for further details. You will have to add origin, spacing, etc. but for the sake of simplicity I am going to omit them.
Here is the code:
import nibabel as nb
example = nb.load("/nifti/path/example.nii.gz")
intensities = example.get_fdata()
grid = pv.UniformGrid()
grid.dimensions = np.array(values.shape) + 1
grid.cell_data["intensities"] = intensities.flatten(order="F")
grid.plot(volume=True, cmap="bone")

Iterating through sequence of Images in tensorflow

I have a database with images numbered from 1 till 7500.
I need to feed these images into my model in tensorflow in the following manner:
grab the 1st 100 images, that is, from 1 till 100, then grab another 100 images such that the next batch is from 1 till 101. As well, the following batch is from 2 till 102 and so on...
The purpose for using the following behavior is that I am using a recurrent neural network where the images to be fed are faces detected from a video. Therefore, I need to feed sequences of images such that these images are directly following one another.
Any help is much appreciated!!
I don't have a perfect solution for your question, but this one might help you.
I'm assuming that you are using tfrecords to build inputs because if not, feeding numpy to model doesn't meet this problem.
supporing your image files are list like this ["image_0", ..., "imgae_N"], you can build i-th tf.example with ["image_i", ..., "image_i+100"] as a feature.
After dequeuing, you get a tensor contains the names of there images, and then unstack them, read image content from there image names with tf.read_file and decode them to images with tf.image.decode_image, and concat them back into one tensor and send it to your model as input.

Simple Multi-Blob Detection of a Binary Image?

If there is a given 2d array of an image, where threshold has been done and now is in binary information.
Is there any particular way to process this image to that I get multiple blob's coordinates on the image?
I can't use openCV because this process needs to run simultaneously on 10+ simulated robots on a custom simulator in C.
I need the blobs xy coordinates, but first I need to find those multiple blobs first.
Simplest criteria of pixel group size should be enough. But I don't have any clue how to start the coding.
PS: Single blob should be no problem. Problem is multiple blobs.
Just a head start ?
Have a look at QuickBlob which is a small, standalone C library that sounds perfectly suited for your needs.
QuickBlob comes with a small command-line tool (csv-blobs) that outputs the position and size of each blob found within the input image:
./csv-blobs white image.png
X,Y,size,color
28.37,10.90,41,white
51.64,10.36,42,white
...
Here's an example (output image is produced thanks to the show-blobs.py tiny Python utility that comes with QuickBlob):
You can go through the binary image labeling the connected parts with an algorithm like the following:
Create a 2D array of ints, labelArray, that will hold the labels of the connected regions and initiate it to all zeros.
Iterate over each binary pixel, p, row by row
A. If p is true and the corresponding value for this position in the labelArray is 0 (unlabeled), assign it to a new label and do a breadth-first search that will add all surrounding binary pixels that are also true to that same label.
The only issue now is if you have multiple blobs that are touching each other. Because you know the size of the blobs, you should be able to figure out how many blobs are in a given connected region. This is the tricky part. You can try doing a k-means clustering at this point. You can also try other methods like using binary dilation.
I know that I am very late to the party, but I am just adding this for the benefipeople who are researching this problem.
Here is a nice description that might fit your needs.
http://www.mcs.csueastbay.edu/~grewe/CS6825/Mat/BinaryImageProcessing/BlobDetection.htm

Cut a jpg file in C, (NOT crop)

i would like to know how can i cut a jpg file using a coordinates i want to retrieve using artoolkit and opencv, see:
Blob Detection
i want to retrieve coordinates of the white sheet and then use those coordinates to cut a jpg file I'm took before.
Find this but how can this help?
How to slice/cut an image into pieces
If you already have the coordinates, you might want to deskew the image first:
http://nuigroup.com/?ACT=28&fid=27&aid=1892_H6eNAaign4Mrnn30Au8d
This post uses cv::warpPerspective() to achieve that effect.
The references above use the C++ interface of OpenCV, but I'm sure you are capable of converting between the two.
Second, cutting a particular area of an image is known as extracting a Region Of Interest (ROI). The general procedure is: create a CvRect to define your ROI and then call cvSetImageROI() followed by cvSaveImage() to save it on the disk.
This post shares C code to achieve this task.

How to represent voxel volume in VTK file format?

I have 3D binary array which represents a volume, where a[x,y,z] = 0 indicates no object and a[x,y,z] = 1 indicates the object region.
I want to save this as a VTK file and view it in ParaView. What is the simplest way to achieve this? Suggestions for other approaches are welcome.
I looked through the VTK file format, but I have not found direct way to achieve what I need, just via other structures.
It seems Paraview accepts raw data http://paraview.org/Wiki/Data_formats#Raw_files.
So why not just write out your data in a triple for-loop to raw binary data?
How to open raw data file in Paraview (edit):
Example: Fuel from Uni Tuebingen
open .raw file
properties: Data Scalar Type: unsigned char
properties: Data Extend: 1<tab>64<tab>1<tab>64<tab>1<tab>64
properties: Apply
click on Contour (next to the calculator symbol)
properties: Apply
Now you should see something. From here you can play around a bit.
In VTK itself (i.e. calling from C++) I remember there were some nice volume render algorithms available (ray casting, 2D textures, etc) but I could not find them in paraview right now. Edit: But Robert could (see comment).

Resources