How to print R, G and B matrices [closed] - c++

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.

Related

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

Image Borders & Corners C++ [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'm working on openCV C++ project ,
Part of my project requires to point at any pixel of image with my mouse, get it's x, and y coordinates then I should copy a 8*8 Block of pixels around this pixel to apply some image processing functions for this block.
This is a part of my code that take 8*8 block around pixel:
cv::Mat foo = Mat(8, 8, CV_8UC3);
foo = img3.colRange(x-4, x + 4).rowRange(y-4, y + 4);
But now I have a problem with image borders; if the mouse on a pixel near one of image borders or corners I have an exception because the range of col & rows (The Block size becomes bigger than existing image).
How can I solve this problem?
Just clamp the x and y values so that there are always 4 pixels around them:
x = max(4, min(img3.cols - 5, x))
y = max(4, min(img3.rows - 5, x))
cv::Mat foo = img3.colRange(x-4, x + 4).rowRange(y-4, y + 4);

gaussian filter kernel values [closed]

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
}
}