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);
Related
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have 4 positions, with X, Y and Z component. The first 2 vectors represent the 2 extents, and the other vector represent the other 2 extents of the other cuboid.
An example:
Vector3 firstCubeMax = Vector3(10, 10, 10);
Vector3 firstCubeMin = Vector3(-10, -10, -10);
Vector3 secondCubeMax = Vector3(0, 0, 0);
Vector3 secondCubeMin = Vector3(-30, -60, -30);
(first cuboid starts at 0,0,0 with size (20,20,20). The second one starts at (15, 30, 15) and has a size of (30,60,30))
What I want to do is to check if 2 cuboids are colliding (touching or going through) giving those vectors. Also, I am using C++
If the right side of one cuboid is before the left side of other, or the top of one cuboid is below the bottom of the other, or the front of one cuboid is behind the back of the other, then they do not intersect. Otherwise, they do.
So negate the "ors" into a bunch of "ands" to get:
return firstCubeMin.x() < secondCubeMax.x()
&& secondCubeMin.x() < firstCubeMax.x()
&& firstCubeMin.y() < secondCubeMax.y()
&& secondCubeMin.y() < firstCubeMax.y()
&& firstCubeMin.z() < secondCubeMax.z()
&& secondCubeMin.z() < firstCubeMax.z()
Put another way, they intersect if and only if they intersect when projected onto all three axes. The "only if" direction is obvious; you can prove the "if" direction by thinking about what it means for a point to be inside a cuboid.
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.
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
}
}
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 11 years ago.
This post was edited and submitted for review 9 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Let define a circle in 2D:
struct Vec2D
{
float x;
float y;
};
// Assume dot, normalize, length, sum, subtract, and scale to exist.
struct Circle2D
{
Vec2D center;
float radius;
};
Given two circles, it is needed to determine the 0, 1 or 2 intersection points between the circles:
bool circleVsCircleIntersection( const Circle& a, const Circle& b,
std::array<std::optional<Vec2D>,2>& intersPos);
How to determine the 0, 1 or 2 intersection points of the circles in C++?
Assuming you have a circles center and it's radius
you can setup an equation like
(x-x1)²+(y-y1)²=r1²
you can do this for both circles
I x²-2xx1+x1²+y²-yy1+y1²=r1²
II x²-2xx2+x2²+y²-yy2+y2²=r2²
then you can insert II in I and there you go :)