Hidden surface removing in Matlab - matlab

I have a representation of 3D object as a a) cloud point and b) triangle mesh
My goal is to rotate this object and than obtain a surface which is visible from one specific view.
Then I would remove the points which are not visible from a given view. Does anyone know how to do this in MATLAB. What method is the fastest?
The file with the point cloud contains the coordinates of each point, and information about the color stored in three RGB channels.
First line:
`-35.4717 88.8637 -99.3782 97 78 46`
I will be grateful for any help.

One possible way would be to re-implement the pipeline of a graphic processor.
Transform your object and project all triangles into an image plane. In this image plane, the distances of each part of the triangle can be stored.
With that information you can check if a vertex is further away than the one you have painted into the image plane.

Related

How To Create an ROI Mask of a Freesurfer Surface

I have been working on a project extracting different cortical depths from freesurfer constructions (ie 0%, 25%,50%,75%,100%), and then projecting the intensity values onto the surfaces. My end goal is to get the intensity values within an ROI of each depth layer.
So far I have been able to create equivolumetric surfaces at each depth using surface_tools, then I projected the intensity values from the original volumetric file onto the surface vertices using the mri_vol2surf command. The end result of this is an Nx1 .mgh file that has the intensity values of each vertex within the selected depth layer. I can open up this file in matlab using the load_mgh script, but I don't think I can open it with anything else.
My question is how to create an ROI mask within this layer because I don't need the whole pial surface, just part of the layer at each depth. I have tried to draw a label mask in Freesurfer with the original volume and the pial files at each depth (to use as guide lines) loaded. Then I tried to convert the label file for the ROI I drew to a volumetric file using mri_label2vol (this way I can use mri_mask).
The problem is that when I apply mri_mask to the original .mgh file generated by vol2surf it creates a matrix that is still 2D, and all the values are 1 (rather than their normal intensities, just isolated to an ROI rather than the whole layer). I'm wondering if I should convert the .mgh file from vol2surf back into voxel space before trying to mask it with the label file.
I was thinking about using surf2vol with each surface layer, the original volume, and the -mkmask flag. Supposedly this is meant to create a volume that is comprised of all the voxels that intersect a surface file. If I had something like this I could then create ROIs for each layer volume and mask the way I would normally mask an MRI volume.
Also a suggestion about a better way to do this in general would be greatly appreciated. I think I'm overcomplicating it.
I have attached pictures of the layer files when loaded into freesurfer, and the .mgh files generated by vol2surf after mapping the intensity values to the surface vertices.

How to transform a non-planar surface on a plane using a pair of 2D and 3D control points?

I have a set of control point pairs. One part of the pair is in world coordinates (3D). The other one is in pixel corrdinates of the image (2D).
My goal is to transform a surface you can see in this image onto a flat plane. The problem is that the surface is not perfectly flat, it kinda looks like a ribbon. Otherwise I could have used OpenCV's getPerspectiveTransform() or Matlab's fitgeotrans().
I know that I can use OpenCV's solvePnP() or Matlab's estimateWorldCameraPose() to get the pose of the camera. The camera matrix is known and the image is rectified. But what is the next step then? How can I transform my ribbon shaped surface onto a flat plane, i.e. get an orthographic top view? That is the step, I'm stuck on.

Crop an image using multiple coordinates in Matlab

I used the "imfreehand" to crop an irregular shape and save its positions into a variable. This position variable is a 85*2 double matrix (85 points, X and Y coordinates). Now, I want to use the same position to crop another image (different layer of the image, but the location of the objects is the same). The functions I can find all requires rectangle positions (X1,X2,Y1,Y2). In my situation, I have 82 different (X,Y) coordinates, how can I use the position information to crop a new image?
From what I understand, you want to take the coordinates created by imfreehand(...) to create a cropable object on another image. You can use the function impoly(hparent,position) for this purpose.
The MathWorks page provides an example to guide you on its usage.

Extract contour from obj 3d object in Matlab

I have an .obj file representing a 3D object.
I need to extract from this 3D object the contour that is obtained by intersection with a plane. So for example, I have an object representing a cylinder oriented with vertical axis, then I want to extract a circle contour when the intersecting plane is horizontal or a rectangular contour when the intersection plane is vertical. Any suggestion about how to do it?
Since I didn't know how to visualise this obj file, I have converted to a patch with the following code (some function taken from loadawobj from Matlab file exchange).
modelname='file.obj';
S=loadawobj(modelname);
mtl=loadawmtl(['obj/' S.mtllib]);
p3=patch('Vertices',S.v','Faces',S.f3');
for ii=1:length(S.umat3)
mtlnum=S.umat3(ii);
fvcd3(ii,:)=mtl(1).Kd';
end
p3.FaceVertexCData=fvcd3;
p3.FaceColor='flat';
But I don't necessarily need to extract the contour from the resulting patch if this is too complex to accomplish. If there is an easier procedure starting from the obj file, it's also fine and acceptable. Thank you!
That's the way I solved the problem, after collecting information all around the web. I couldn't find anything ready on line so I had to implement an algorithm on my own. The basic idea is very simple but there are many steps required. I start from two info: one array containing the coordinates of the cloud point and another array containing a bunch of tuples about how the 3 vertex are connected to form a triangle.
First of all you need to find a representation of the plane you want to use for your cutting. That means you just use one point and the normal to the plane to represent it. That plane is required in order to identify the cutting point on the structure.
Second step is to identify the triangles on the plane. In few words you just need to scroll over all the triangles of the structure and find those having one corner above the cutting plane and another corner below the cutting plane. Also don't forget to account for the condition where one corner is on the plane or two are on the plane. All the other triangles are not needed, since they are totally above or below the cutting plane.
Now you have a subset of all your triangles. You need to extract points of the contour. So for each triangle you have 3 vertex: in general case you can imagine that one vertex is above the plane and the other two are below. Then you have two lines cutting the plane. You can extract two point by simply intersecting these lines with the cutting plane.
By repeating this operation you get a series of points on 2D space. But they have no order and if you plot them as a continuous plot, you get lines jumping up and down since the points you have extracted are randomly located in the array. So, it's required to order them in a proper way. The method I used is the very simple: start from one point and connect to the closest one. There are some bad situations where that doesn't work but you can probably avoid it by adding some more rules on the algorithm.

matlab: how do create a projection

I want to create a perspective projection of a 3D image onto the x,y plane with a focal length of 10 and a principal point (-1, -5).
I found:
view
viewmtx
But I do not get how to tell matlab that I want to use the x,y plane nor how to set the focal length? Can someone explain me how to do that?
I'm afraid you are not looking for the right functions.
The view function does only change the point of view on the current axis, while viewmtxreturn a transformation matrix.
You may want to do something similar to what it is discussed on this post How do I draw a texture-mapped triangle in MATLAB?, where maketform and imtransform are the key functions to get a plane image reprojected into another certain 3d plane.