Color Filter Technique OpenCV

妖精的绣舞 提交于 2019-12-22 16:45:15

问题


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.

  1. Perform Erosion/Dilation Operations on your image to get rid of extra space, if necessary.
  2. Call cv::findContours() to get a trace around the edges of the white regions in your image.
  3. 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.
  4. Fit a line to the contour using cv::fitline() or your own approach.
  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!