问题
I am reading a picture and filtering out the colors and this is the result I get in the following images. The code is from the example folder in the opencv
module.
I am trying to feed the picture back to an A.R Drone 2.0
and have the drone follow the color white. How would I do the second part?
var cv = require('C:/users/danny/codes/node_modules/opencv/lib/opencv');
// (B)lue, (G)reen, (R)ed
var lower_threshold = [220, 220, 220];
var upper_threshold = [255, 255, 255];
//var lower_threshold = [46, 57, 83];
//var upper_threshold = [80, 96, 115];
cv.readImage('C:/users/danny/codes/node_modules/opencv/examples/files/gutter.jpg',
function(err, im) {
if (err) throw err;
if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size');
im.inRange(lower_threshold, upper_threshold);
im.save('C://users/danny/codes/coin_detected.jpg');
console.log('Image saved to C://users/danny/codes/coin_detected.jpg');
});
回答1:
Since your drone can move in 3D-space, I will suggest some pseudo-code steps in the 2D realm to get you started with a simple line follower. Then you can extrapolate to 3D and add more degrees of freedom to meet your needs.
- Perform Erosion/Dilation Operations on your image to get rid of extra space, if necessary.
- Call cv::findContours() to get a trace around the edges of the white regions in your image.
- Sort your found contours by pixel area to find the contour you want to follow. I'd guess that you'd most often want to follow the largest pixel area. Try using the contour moments.
- Fit a line to the contour using cv::fitline() or your own approach.
- Take the angle of the line and map it to your drone controller to adjust the yaw.
From here, you can do several other basic things to control the motion of your drone:
- Set a contour pixel mass threshold. If the contour area > threshold, move up.
- Look at the shape of the threshold area. If it is more like a trapezoid than a rectangle, you can adjust your roll/pitch.
来源:https://stackoverflow.com/questions/46200804/color-filter-technique-opencv