Home Color Filter Technique OpenCV
Reply: 1

Color Filter Technique OpenCV

Danny
1#
Danny Published in 2017-09-13 14:58:10Z

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');
  });
Mepix
2#
Mepix Reply to 2017-09-13 20:35:53Z

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.
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.662775 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO