I have an image and two points,
and 开发者_如何学运维I want to read the pixels between these two points, and resample them into a small 1x40 array. I'm using EMGU which is a C# wrapper for OpenCV. thanks, SWWhat you are looking for is Bresenham's line algorithm. It will allow you to get the points in the pixel array that best approximate a straight line. The Wikipedia link also contains psuedo code to get you started.
Emgu CV includes method in the Image
class for sampling color along a line called Sample
.
Refer to the manual for the definition. Here's the link to Image.Sample
in version 2.3.0.
You will still have to re-sample/interpolate the points in array returned from Sample
to end up with a 40 element array. Since there are a number of ways to re-sample, I'll suggest you look to other questions for that.
Rotate and crop
I'd first try to do it like this:
- calculate rotation matrix with (
GetRotationMatrix2D
) - warp the image so that this line is horisontal (
WarpAffine
) - calculate new positions of two of your points (you can use
Transform
) - get image rectangle of suitable width and 1 px high (
GetRectSubPix
)
Interpolation here and there may affect the results, but you have to interpolate anyway. You may consider cropping the image before rotation.
Iterate over the 8-connected pixels of the line
Otherwise you may use the line iterator to iterate over the pixels between two points. See documentation for InitLineIterator
(Sorry, the link is to the Python version of OpenCV, I've never heard of EMGU). I suppose that in this case you iterate over pixels of a line which was not antialiased. But this should be much faster.
Interpolate manually
Finally, you may convert the image to an array, calculate which elements the line is passing through and subsample and interpolate manually.
精彩评论