DJI iOS Development III: Step by step to build a computer vision and UAV control development platform based on DJI Phantom and iOS

Source: Internet
Author: User

    • 1 Preface

Before this, a lot of research team in the world spent a lot of time to build their own four axes, their own development of hardware, to finally be able to study the air robot flight control, vision and other aspects of the problem, great in a few years ago with AR drone to do. But we know that AR Drone flight range is too small, the camera is also very unstable, so the use of AR drone to do research has great limitations.

Now, with DJI's aircraft, it's easy to have a great research platform to study the most cutting-edge issues, or the most deserving of research, which is computer vision + machine learning + robotic control. What are the exciting things that combine these three areas?

Said some nonsense, back to the point. In the previous article, I described how to use the iOS platform to build a basic computer vision development environment, and in this article I'll show you how to build a development platform based on DJI Phantom and iOS.

    • 2 Development environment

Mac OS X 10.10 Yosemite

Xcode 6.1

DJI SDK 1.0.6

OpenCV for IOS 2.4.10

    • 3 Building Steps

For basic iOS engineering and the use of the DJI SDK, this is not described in detail here,

http://blog.csdn.net/songrotek/article/details/44408081

The above blog describes the basic use of the DJI SDK stuff.

So this article is mainly to explain how to modify the DJI SDK things so that we can directly add OPENCV code to the image processing.


The linked library, as shown above, mostly adds a lot of libraries related to image processing, some of which may not be needed.

Pull in the Viewpreviewer folder in the DJI SDK directly.

Note The search path for Lib and header:


to correspond

I made changes mainly in Videopreviewer and Movieglview two files

Step 1: Add Uiimageview for display in Videopreviewer
@property (strong,nonatomic) Uiimageview *imageview;
Step 2: Initialize this uiimageview in Setview:
-(BOOL) Setview: (UIView *) view{begin_dispatch_queue if (_glview = = nil) {_glview = [[Movieglview alloc] Initwi        Thframe:cgrectmake (0.0f, 0.0f, VIEW.FRAME.SIZE.WIDTH/2, VIEW.FRAME.SIZE.HEIGHT/2)];    _status.isglviewinit = YES; } if (Self.imageview = = nil) {Self.imageview = [[Uiimageview alloc] Initwithframe:cgrectmake (view.frame.siz    E.WIDTH/2, 0.0f, VIEW.FRAME.SIZE.WIDTH/2, VIEW.FRAME.SIZE.HEIGHT/2)]; } dispatch_async (Dispatch_get_main_queue (), ^{[_glview setframe:cgrectmake (0.0f, 0.0f, View.frame.size.widt        H/2, VIEW.FRAME.SIZE.HEIGHT/2)];        [View Addsubview:_glview];            [View AddSubview:self.imageView];    }); End_dispatch_queue return NO;}

The size of the view above can obviously be changed by itself

Step 3: Modify the Startrun code

if (_status.isglviewinit &&!_status.ispause &&!_status.isbackground) {                                          UIImage *image = [_glview Render:_renderyuvframe[_renderframeindex]]; if (self.delegate!=nil && [self.delegate respondstoselector: @selector (processimage:)]) {di                             Spatch_async (Dispatch_get_main_queue (), ^{Uiimagetomat (image,matimage);                             [Self.delegate Processimage:matimage];                         Self.imageView.image = Mattouiimage (matimage);                     }); } else {Dispatch_async (Dispatch_get_main_queue (), ^{self.imageview.i                         Mage = image;                     }); }                 }

I changed the render code to convert the Glview to UIImage, then the uiimage for the mat format, and set the delegate to be processed externally and then converted back. Use Dispatch_async to operate on the main thread.

Step 4: Modify the Render code
Grabbing image from FBO Nsinteger x = 0, y = 0;    Nsinteger datalength = _backingwidth * _backingheight * 4;        Glubyte *data = (glubyte*) malloc (datalength * sizeof (glubyte));    Glpixelstorei (Gl_pack_alignment, 4);    Glreadpixels (x, Y, _backingwidth, _backingheight, Gl_rgba, Gl_unsigned_byte, data);    Cgdataproviderref ref = Cgdataprovidercreatewithdata (null, data, datalength, NULL);    Cgcolorspaceref colorspace = Cgcolorspacecreatedevicergb (); Cgimageref iref = cgimagecreate (_backingwidth, _backingheight, 8, +, _backingwidth * 4, ColorSpace, KCGBitmapByteOrder32 Big |        Kcgimagealphapremultipliedlast, ref, NULL, True, Kcgrenderingintentdefault);    Uigraphicsbeginimagecontext (Cgsizemake (_backingwidth, _backingheight));    Cgcontextref Cgcontext = Uigraphicsgetcurrentcontext ();    Cgcontextsetblendmode (Cgcontext, kcgblendmodecopy);    Cgcontextdrawimage (Cgcontext, CGRectMake (0.0, 0.0, _backingwidth, _backingheight), iref); UIImage *image = Uigraphicsgetimagefromcurrentimagecontext ();        Uigraphicsendimagecontext ();    Free (data);    Cfrelease (ref);        Cgimagerelease (IREF); return image;

At the back of the method add the code as above to implement Glview UIImage.

After the above processing, we can be very convenient to call in the Viewcontroller.

Code for Step 5:viewcontroller
-(void) viewdidload {[Super viewdidload];        Additional setup after loading the view, typically from a nib.    Setup Drone self.drone = [[Djidrone alloc] initwithtype:djidrone_phantom];    Self.drone.delegate = self;        Self.drone.camera.delegate = self;    Set the video stream [[Videopreviewer instance] start]; [Videopreviewer instance].delegate = self;}    -(void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated];    [[Videopreviewer instance] SetView:self.view]; [Self.drone Connecttodrone];}        -(void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; [Self.drone.camera startcamerasystemstateupdates];}    -(void) Viewwilldisappear: (BOOL) animated{[Super viewwilldisappear:animated];    [Self.drone Disconnecttodrone]; [Self.drone destroy];}    -(void) didreceivememorywarning {[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} The processing of the #pragma mark-djicameradelegate//OpenCV is in the following method-(void) PROcessimage: (Cv::mat &) image{Cvtcolor (image, image, Cv_bgr2gray);} -(void) Camera: (Djicamera *) camera Didreceivedvideodata: (uint8_t *) videobuffer length: (int) length{uint8_t *pbuffer = (    uint8_t*) malloc (length);    memcpy (pbuffer, videobuffer, length); [[[Videopreviewer instance] dataqueue] Push:pbuffer length:length];}

You see, in the Viewcontroller code can be very simple, because the entire process is implemented in the Viewpreviewer, here, we can say that we just in Processimage: Inside the code to paste OpenCV OK.


DJI iOS Development III: Step by step to build a computer vision and UAV control development platform based on DJI Phantom and iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.