C++ Marching Cubes Algorithm Explanation [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am trying to understand how the marching cubes algorithm works.
Source:
http://paulbourke.net/geometry/polygonise/
What i don't understand is how do you calculate the "GRIDCELL" values. To be exact the
double val[8];
part is not clear for me what it actually supposed to contain.
typedef struct {
XYZ p[8];
double val[8];
} GRIDCELL;
As i understand XYZ p[8]; are the vertex coordinates for the output cube. But what val[8]; is?

The marching cubes algorithm is -- as explained in the linked description -- an algorithm to build a polygonal representation from sampled data. The
double val[8];
are the samples for the 8 vertices of the cube. So they are not computed they are measurements from e.g. MRI scans. So the algorithm is the other way around: take a set of measured numbers and construct a surface representation for visualization from it.

Te val is the level of "charge" for each vertex of the cell, it depends of the tipe of shape that you want to creae.
f.e.: if you want to made a ball you can sample the values with the formula:
for (int l = 0; l < 8; ++l){
float distance = sqrtf(pow(cell.p[l].x - chargepos.x, 2.0) + pow(cell.p[l].y - chargepos.y, 2.0) + pow(cell.p[l].z - chargepos.z, 2.0));
cell.val[l] = chargevalue /pow(distance, 2.0);}

After further reading and research the explanation is quite simple.
First off all:
A voxel represents a value on a regular grid in three-dimensional space.
This value simply represents the so called "isosurface". Or in other words the density of the space.
double val[8];
To simplify:
Basically this should be a value between -1.0f to 0.0f.
Where -1.0f means solid and 0.0f empty space.
For ISO values a perlin/simplex noise can be used for example.

Related

Bluring an image in C++/C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So I'm staring a project about image processing with C++.
The thing is that everything I find online about this matter (blurring an image in C++) comes either with CUDA or with OpenCV.
Is there a way to blur an image with C++ only? (for starters)
If yes, can somebody please share the code or explain?
Thanks!
Firstly you need the image in memory.
Then you need a second buffer to use as a workspace.
Then you need a filter. A common filter would be
1 4 1
4 -20 4
1 4 1
For each pixel, we apply the filter. So we're setting the image to a weighted average of the pixels around it, then subtracting to avoid the overall image going lighter or darker.
Applying a small filter is very simple.
for(y=0;y<height;y++)
for(x=0;x<width;x++)
{
total = image[(y+1)*width+x+1];
for(fy=0; fy < 3; fy++)
for(fx = 0; fx < 3; fx++)
total += image[(y+fy)*width+x+fx] * filter[fy*3+x];
output[(y+1)*width+x+1] = clamp(total, 0, 255);
}
You need to special case the edges, which is just fiddly but doesn't add any theoretical complexity.
When we use faster algorithms that the naive one it becomes important to set up edges correctly. You then do the calculations in the frequency domain and it's a lot faster with a big filter.
If you would like to implement the blurring on your own, you have to somehow store the image in memory. If you have a black and white image, an
unsigned char[width*height]
might be sufficient to store the image; if it is a colour image, perhaps you will have the same array, but three or four times the size (one for each colour channel and one for the so-called alpha-value which describes the opacity).
For the black and white case, you would have to sum up the neighbours of each pixel and calculate its average; this approach transfers to colour images by applying the operation to each colour channel.
The operation described above is a special case of the so-called kernel filter, which can also be used to implement different operations.

Linear Gradient with Angle & Strength [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want implement a function in C++/RealBasic to create a color gradient by the parameters:
Width and height of the image
2 colors of the gradient
Angle (direction) of the gradient
Strength of the gradient
The following links show some examples of the desired output image:
http://www.artima.com/articles/linear_gradients_in_flex_4.html, http://i.stack.imgur.com/4ssfj.png
I have found multiple examples but they give me only vertical and horizontal gradients, while I want to specify the angle and strength too.
Can someone help me please?
P.S.: I know only a little about geometry!! :(
Your question is very wide and as is, this is a pretty complex exercise with a lot of code, including image rendering, image format handling, writing file to disk, etc. These are not the matter of a single function. Because of this, I focus on making an arbitrary linear color gradient of 2 colors.
Linear color gradient
You can create a linear color "gradient" by linear interpolation between 2 colors. However simple linear interpolation makes really harsh-looking transitions. For visually more appealing results I recommend to use some kind of S-shaped interpolation curve like the Hermite interpolation based smoothstep.
Regarding the angle, you can define a line segment by the start (p0) and end (p1) points of the color gradient. Let's call the distance between them d01, so d01 = distance(p0, p1). Then for each pixel point p of the image, you have to compute the closest point p2 on this segment. Here is an example how to do that. Then compute t = distance(p0, p2) / d01. This will be the lerp parameter t in the range [0, 1].
Interpolate between the 2 gradient color by this t and you got the color for the given point p.
This can be implemented multiple ways. You can use OpenGL to render the image, then read the pixel buffer back to the RAM. If you are not familiar with OpenGL or the rendering process, you can write a function which takes a point (the 2D coordinates of a pixel) and returns an RGB color - so you can compute all the pixels of the image. Finally you can write the image to disk using an image format, but that's an another story.
The following are example C++14 implementations of some functions mentioned above.
Simple linear interpolation:
template <typename T, typename U>
T lerp(const T &a, const T &b, const U &t)
{
return (U(1) - t)*a + t*b;
}
, where a and b are the two values (colors in this case) you want to interpolate between, and t is the interpolation parameter in the range [0, 1] representing the transition between a and b.
Of course the above function requires a type T which supports multiplication by a scalar. You can simply use any 3D vector type for this purpose, since colors are actually coordinates in color space.
Distance between two 2D points:
#include <cmath>
auto length(const Point2 &p)
{
return std::sqrt(p.x*p.x + p.y*p.y);
}
auto distance(const Point2 &a, const Point2 &b)
{
Point delta = b - a;
return length(delta);
}
Image from https://developer.mozilla.org/en-US/docs/Web/CSS/linear-gradient

Is there any C++ opencv code to compute velocity of key feature points in each frame of video? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I would like to compute velocity of key feature points and set thresholding for motion detection in a video.
I have a python code with me as follows:
def compute_vel_kf(self, fps):
if ((len(self.features) == 0) or (self.features == None)):
return;
test_image = self.current_frame.copy();
time_diff_in_sec = 1/fps;
self.v = [];
for i, p1 in enumerate(self.features):
p2 = self.features_prev[i];
# speed = dist/time
vx, vy = [(p1[0][0] - p2[0][0]), (p1[0][1] - p2[0][1])];
v = sqrt(vx * vx + vy * vy)*fps;
ang = math.atan2(vy, vx);
self.v.append(array([v, ang]));
i += 1;
return self.v;
I have to port it to cpp code. In cpp code i have used points[1] and points[2] that holds current frame & previous frame detected points respectively. I need to calculate velocity of the detected key feature points.
As Tobbe mentioned, you should first try to get some results with sample data, and then ask for help with what you have, about what you need next.
To give a brief answer, you should first install an image processing library like OpenCV, and then writ some sample code to load and process frames from your video. Then you can segment objects in the first frame, track them in the coming frames, and use the stats to calculate the velocity.
Edit: Now we can see that you already have the positions in the previous and the current frame. The usual method to get the velocity in pixels/second is to calculate the distance (Euclidian or separate axes, depending on your need) between to the two locations, and then multiply it by the frame rate. However, since the video is most likely taken at a many frames a second, you can also do a weighted averaging with the velocity from the previous frame pair.

opengl :how to compute transformation matrix? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I need to compute the opengl transformation matrix that transforms a rectangle A,B,C,D into the polygon A',B,C,D (that differs from the first one for 1 point).
How can i do that?
First you need to formalize the problem. You have a matrix M and 4 points that get transformed to another 4 points.
M*A = A'
M*B = B
M*C = C
M*D = D
Every line can be written as 4 equations. For example:
M11*A1 + M12*A2 + M13*A3 + M14*A4 = A'1
M21*A1 + M22*A2 + M23*A3 + M24*A4 = A'2
...
As a result you get 16 linear equations that can be solved with the Guassian elimination. http://en.wikipedia.org/wiki/Gaussian_elimination
Thanks for your answer.
I implemented your solution, but unfortunately it finds a general transformation matrix, and not always an affine transofmation (which i need).
I finally solved my problem using opencv::estimateAffine3D http://docs.opencv.org/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#estimateaffine3d

Imitating html's flexible rectangle [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to imitate the well-known HTML's great great RECTANGLE. I mean all of the characteristic of the rectangles like borders, border-radius, triangulated quad on corners, etc. I don't like to use any other libraries except mine. I would like to create this one for the sake of learning and experience, and also to use it for the future as a GUI system. I am working on this concept of shuffled rectangles.
It is composed of:
4 GL_TRIANGLES as quadrilateral on corners
4 arcs on corners
4 borders on all sides
And one front big rectangle on the front
And these are the outputs I made so far :)
w/o border-radius
w/ border-radius
So the things I am really really confused with are
Border-sizes
Border-locations
Is it the X, Y's or the W, H's
When to draw or not front-most rectangle
Anything I don't know yet.
...Please comment other things I should include here for clarification. Thanks!
Edit:
Hmm..., Okay as for a minimal question only. I just wanted to implement this stuffs and programmatically compute their values as I change a single attributes of the rectangle.
border-radii-sizes
border-sides
I'm putting too much images here, please please understand me :(
*left-border
*corner
I was thinking of that kind of rectangles positioning and it's really difficult in my head to compute for their coordinates or the sizes base on the set of attributes I'm gonna define on the designing part. For example, If I define the border-radius-top-left to have a value of 50% with its counter part of border-size-left with a certain value, what would be the formula I must consider. Or, must I need to add any component/ private attributes in order to make this thing happen?
Yey!! I have figured it out!!
Please let me SO to discuss my [problem solved] here.
Sorry for my unclassified art :) I made it colorful for property separation.
Explanation:
Arcs w/c serves as corner-radii.
The formula for points (x, y) will be automatically generated here
corner-radii-points (x, y) are the target.
points (x, y) Should be the only one adjusting based on the given radii values.
Curved part of this should be static in position.
Fake borders these are the inner-side-borders.
Properties of this such as [x, y, width, height] will depend on corner-radii-points(x, y) and points(x, y).
Inner quad w/c is the inner-rectangle
This will just serves as cover
Properties of this such as [x1, y1, x2, y2](this is a polygon so I labeled it like that) will depend on points (x, y)
Now I can simply do this:
Designing Part:
int w3 = rect.width >> 3;
int h3 = rect.height >> 3;
rect.setBorderRadius(C_NW, w3, h3);
rect.setBorderRadius(C_NE, w3, h3);
rect.setBorderRadius(C_SE, w3, h3);
rect.setBorderRadius(C_SW, w3, h3);
rect.setColors(C_NW, cc::getColor(COLORS::RED));
rect.setColors(C_NE, cc::getColor(COLORS::GREEN));
rect.setColors(C_SE, cc::getColor(COLORS::BLUE));
rect.setColors(C_SW, cc::getColor(COLORS::YELLOW));
rect.setBorderColor(B_TOP, cc::getColor(COLORS::WHITE));
rect.setBorderColor(B_RIGHT, cc::getColor(COLORS::WHITE));
rect.setBorderColor(B_BOTTOM, cc::getColor(COLORS::GRAY3));
rect.setBorderColor(B_LEFT, cc::getColor(COLORS::GRAY3));
rect.setBorderSize(B_TOP, 20);
rect.setBorderSize(B_RIGHT, 20);
rect.setBorderSize(B_BOTTOM, 20);
rect.setBorderSize(B_LEFT, 20);
Results:
rect is the one with Lightning McQueen image.
Those are the formulas I evaluate base on trial and error.
Also thanks to Sir Mark Garcia for helping me by demonstrating some diagrams, and suggested to use MS Paint for visualization :)
Next problem is masking as you can see that there are non-curved borders and corner radius at the same time, but I won't focus on that at this moment.
If ever someone is interested in this kind of rectangle implementation, you can PM me here and I'll give you the source code.