How do I use my smart phone camera as a webcam in MATLAB?

Asked by Ashish Uthama on 20 Jul 2011
Latest activity Commented on by Walter Roberson on 9 Mar 2013

I have an smartphone device with a camera. I would like to obtain and process this image data in MATLAB.

(Specifically, I have an Android device).

0 Comments

Ashish Uthama

Products

No products are associated with this question.

2 Answers

Answer by Ashish Uthama on 20 Jul 2011
Accepted answer

The general solution would need two parts, one to broadcast the data from the device and another part to read this data into MATLAB.

A specific solution for Android:

  • Install the free IP Webcam app. (Make sure you read the corresponding permissions and understand any security issues therein)
  • Open the app, set the desired resolution (will impact the speed!)
  • Scroll to the bottom and tap on 'Start Server'
  • In the camera preview window, note the url at the bottom of the screen.
  • Open MATLAB and use this code snippet to obtain a live preview window. Note that this uses JPG files for discrete frames, which is probably not the fastest way. The app can stream the video and/or audio in multiple ways.
url = 'http://<ip address>/shot.jpg';
ss  = imread(url);
fh = image(ss);
while(1)
    ss  = imread(url);
    set(fh,'CData',ss);
    drawnow;
end

Example:

(If you find faster ways/solutions for other devices, post it here!)

6 Comments

Ashish Uthama on 20 Jul 2011

http://itunes.apple.com/us/app/ip-cam/id333208495?mt=8 appears close enough. Care to try and post another answer?

mirbaz on 9 Mar 2013

hi, i want to know that can we change this rgb image into grayscale or binary image. i have tried but failed. so any suggestion plz

Walter Roberson on 9 Mar 2013
set(fh, 'CData', rgb2gra(ss));

And be sure that you have done

 colormap(gray(256))

after the original image() call.

Ashish Uthama
Answer by Chirag Gupta on 20 Jul 2011

With the iPhone, using the App suggested by Ashish above (IP Cam), you can achieve the same pretty easily! The code is exactly the same:

url = 'http://<ipaddress>:8020/image.jpg';
ss  = imread(url);
fh = image(ss);
while(1)
    ss  = imread(url);
    set(fh,'CData',ss);
    drawnow;
end  

3 Comments

Sean de Wolski on 21 Jul 2011

Nice Fractal, +1

Francisco Canto on 4 Mar 2012

Hi, Excuse me for my bad English. I'm using IP webcam for Android.I used
url = 'http://158.124.30.101:8080/shot.jpg';
ss = imread(url);
fh = image(ss);
The video is excellent but my problem is that I want to use a bounding box to sellect a red object in the video. I don't know if it's possible. Can anyone help me?thanks

Ashish Uthama on 21 Mar 2012

Francisco, you'll get a better answer if you ask this as a new question: http://www.mathworks.com/matlabcentral/answers/questions/new

(also look at http://www.mathworks.com/matlabcentral/fileexchange/28512-simple-color-detection-by-hue for example)

Chirag Gupta

Contact us