gaussian filter kernel values [closed] - c++

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
can anyone tell me how to generate a 2d gaussian filter kernel using the gaussian filter equation? how does the x and y value vary?
ref: http://en.wikipedia.org/wiki/Gaussian_function

To generate the kernel is quite simple. If your problem is in applying the kernel, you need to update the question.
The kernel is simply a square matrix of values, generally an odd number size so that there's a clearly defined center. To fill it, the x and y values go from -(n-1)/2 to (n-1)/2 where n is the size of the matrix.
double half_n = (n - 1) / 2.0;
for (i = 0; i < n; ++i)
{
double x = i - half_n;
for (j = 0; j < n; ++j)
{
double y = j - half_n;
kernel[i][j] = // use formula with x and y here
}
}

Related

Is there a (fast enough) workaround for multiplying matrices exceeding memory limit? [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 have have two distance matrices d_X: n x n and d_Y: m x m.
set.seed(1)
n <- 2
m <- 3
d_X <- as.matrix(dist(runif(n)))
d_Y <- as.matrix(dist(runif(m)))
From matrices d_X and d_Y matrix G: nm x nm is formed:
G <- matrix(nrow = n*m,ncol = n*m)
for(i in 1:n) {
for (j in 1:m) {
for(ii in 1:n) {
for(jj in 1:m) {
G[(i-1)*m+j,(ii-1)*m+jj] = abs(d_X[i, ii] - d_Y[j, jj])
}
}
}
}
There is also matrix U: nm*1:
U <- runif(m*n)
My goal is to calculate G%*%U. Now, when n and m are 200, we need 6GB to allocate G. Since G is symmetric we could save half the space needed by restoring it properly.
In practice n and m sizes are up to 5000 which makes allocating G impossible. Since I only need the value of G%*%U, it would be sufficient to calculate it piece by piece. I'm struggling to find an effective way to do it.
*Time also matters
Since I have to run these calculations thousands of times, it is also important, that computing G%*%U takes reasonable time. I have used following function to speed up computing G in cases where n and m are less than a hundred:
Rcpp::cppFunction('NumericMatrix G_mat(NumericMatrix d_X, NumericMatrix d_Y) {
NumericMatrix G(d_X.nrow()*d_Y.nrow(),d_X.nrow()*d_Y.nrow());
for (int i = 0; i <d_X.nrow(); i++) {
for (int j = 0; j < d_Y.nrow(); j++) {
for (int ii = 0; ii < d_X.nrow(); ii++) {
for (int jj = 0; jj < d_Y.nrow(); jj++) {
G(i*d_Y.nrow()+j,ii*d_Y.nrow()+jj) = fabs(d_X(i, ii) - d_Y(j, jj));
};
};
};
};
return(G);
}
')
So I guess this workaround should be also implemented in C++ to get best results (speed wise)? How to do it?
Maybe this
A <- numeric(m*n)
for(i in 1:n) {
for (j in 1:n) {
A[((i-1)*m+1):(i*m)]= A[((i-1)*m+1):(i*m)] + abs(d_Y-d_X[i,j])%*%U[((j-1)*m+1):(j*m)]
}
}

How can I do this more elegantly? [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 7 years ago.
Improve this question
I have a 2D point A inside the [0,1]² square.
The square is divided into 9 subsquares (of equal dimensions)
http://www.noelshack.com/2015-23-1433689273-capture.png
I want to know which subsquare the point A belongs to.
I can do a if elseif else on the first coordinate, then inside each branch, another if else if else on the second coordinate.
There is a lot of code repeating (the check on the second coordinate)
Is there a better way ?
The trouble is, it is not clear what your 2D point is, what you want the sub-square value as, etc. So a definitive answer is difficult. But anyway, I will make some assumptions and see if I am right about what you are asking...
Assuming you had a point A with a coordinate such as:
float point[2] = {0.1224, 0.4553}
Then to work out where it is inside the square you can do some simple maths:
float x = point[0] * 3;
float y = point[1] * 3; //Multiply both by 3
int xIdx = floor(x);
int yIdx = floor(y); //Floor the result - this gives a number 0 to 2
int cell = yIdx * 3 + xIdx + 1; // Calculate the cell index (based on your diagram)
Now you could generalise this for any point - for example the point might be (1.23343, 2.6768) - by simply removing the integer part from the point - leaving a number between 0 and 1. The integer part would be the super cell, and the fractional part would be converted into the sub cell as above.

How to find the centroid of an object in the point cloud? [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
Can anyone tell me how to find the centroid of an object in point cloud?.
I haven't tried any code yet because I have no slight idea as to how to go about it.
If you have the point locations, you should be able to just average the x and y positions.
int x = 0; y = 0;
for ( i = 0; i < num_pts; i++ )
{
x += pt[i].x;
y += pt[i].y;
}
centroid.x = x / num_pts;
centroid.y = y / num_pts;

How to compute a definite Integral in C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Needs more focus Update the question so it focuses on one problem only by editing this post.
Improve this question
How can you define a function to calculate the value of a definite integral in C++? For example to solve the integral of the function x^2 * cos(x)?
Interestingly enough, I ran across this article a little while ago explaining one method for calculating numerical integrals using function pointers.
https://helloacm.com/c-function-to-compute-numerical-integral-using-function-pointers/
For something like x^2 * cos(x):
You would need an overloaded integral function:
double integral(double(*f)(double x), double(*g)(double x, double y), double a, double b, int n)
{
double step = (b - a)/n; // width of rectangle
double area = 0.0;
double y = 0; // height of rectangle
for(int i = 0; i < n; ++i)
{
y = f(a + (i + 0.5) * step) * g(a + (i + 0.5) * step, y);
area += y * step // find the area of the rectangle and add it to the previous area. Effectively summing up the area under the curve.
}
return area;
}
To call:
int main()
{
int x = 3;
int low_end = 0;
int high_end = 2 * M_PI;
int steps = 100;
cout << integral(std::powf, std::cosf, low_end, high_end, steps);
return 0;
}

How to print R, G and B matrices [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'm having the following code and the number of my channels are 3
IplImage* img_crop_mat = cvLoadImage("....", 1);
...
int b = 0;
uchar* rgb = (uchar*) img_crop_mat->imageData;
I would like to have R, G and B matrices in a loop, skimming the entire image:
for (int y = b; y < height - b; y++)
{
???
for (int x = b; x < width - b; x++)
{
????
}
}
The previous forums regarding my question deal with CvMat but not with pointers as my code.
What are the indexes that I must take into account?
You can use the following macro to access an arbitrary pixel of a 3-channel, 8U-image:
CV_IMAGE_ELEM(myImage, unsigned char, y, x*3 + ChannelOfInterest)
This is an lvalue so you can take and use its value, or you can change the pixel's value.
By default,
ChannelOfInterest = 0, blue
ChannelOfInterest = 1, green
ChannelOfInterest = 2, red
The actual data structure is pretty straightforward, look up the definition of CV_IMAGE_ELEM.