Arduino + MATLAB

I’m using VC0706 serial camera for taking pictures. Now i have to count how many fingers are shown on that picture. I used matlab for image processing and have found some code that works for what i need to do but i don’t konw how to connect arduino and matlab code together. Also i need to switch LED on/off(if 2 fingers are shown then ON, if 3 finges then OFF).Can someone help me? Here are codes that i’m using.

#define IM_SIZE_640x480
//#define IM_SIZE_320x240
//#define IM_SIZE_160x120

#include <Adafruit_VC0706.h>

#include <SD.h>

#include <SPI.h>
#include <SoftwareSerial.h>

SoftwareSerial cameraconnection = SoftwareSerial(2, 3); // Rx (camera Tx), Tx (camera Rx)
Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

void setup()

  pinMode(10, OUTPUT); // SS on Uno, etc.

  Serial.println("VC0706 Camera snapshot test");
  if (!SD.begin(10)) {
    Serial.println("Card failed, or not present");
  if (cam.begin()) {
    Serial.println("Camera Found:");
  } else {
    Serial.println("No camera found?");
  char *reply = cam.getVersion();
  if (reply == 0) {
    Serial.print("Failed to get version");
  } else {

  // Set the picture size - you can choose one of 640x480, 320x240 or 160x120 
  // Remember that bigger pictures take longer to transmit!

  #if defined(IM_SIZE_640x480)
    cam.setImageSize(VC0706_640x480);        // biggest
    uint8_t imgsize = cam.getImageSize();
  #elif defined(IM_SIZE_320x240)
    cam.setImageSize(VC0706_320x240);        // medium
    cam.setImageSize(VC0706_160x120);        // small
  // You can read the size back from the camera (optional, but maybe useful?)
  //uint8_t imgsize = cam.getImageSize();

  Serial.println("Snap in 3 secs...");

  if (! cam.takePicture()) 
    Serial.println("Failed to snap!");
    Serial.println("Picture taken!");
  // Create an image with the name IMAGExx.JPG
  char filename[13];
  strcpy(filename, "IMAGE00.JPG");
  for (int i = 0; i < 100; i++) {
    filename[5] = '0' + i/10;
    filename[6] = '0' + i%10;
    // create if does not exist, do not open existing, write, sync after write
    if (! SD.exists(filename)) {
  // Open the file for writing
  File imgFile =, FILE_WRITE);

  // Get the size of the image (frame) taken  
  uint16_t jpglen = cam.frameLength();
  Serial.print("Storing ");
  Serial.print(jpglen, DEC);
  Serial.print(" byte image.");

  int32_t time = millis();
  pinMode(8, OUTPUT);
  // Read all the data up to # bytes!
  byte wCount = 0; // For counting # of writes
  while (jpglen > 0) {
    // read 32 bytes at a time;
    uint8_t *buffer;
    uint8_t bytesToRead = min(32, jpglen); // change 32 to 64 for a speedup but may not work with all setups!
    buffer = cam.readPicture(bytesToRead);
    imgFile.write(buffer, bytesToRead);
    if(++wCount >= 64) { // Every 2K, give a little feedback so it doesn't appear locked up
      wCount = 0;
    //Serial.print("Read ");  Serial.print(bytesToRead, DEC); Serial.println(" bytes");
    jpglen -= bytesToRead;

  time = millis() - time;
  Serial.print(time); Serial.println(" ms elapsed");

void loop(){}
>> x=imread('IMAGE01.jpg');
>> imshow(x)
>> x=rgb2gray(x);
>> imshow(x)
>> x2=im2bw(x,graythresh(x));
>> imshow(x2)
>> x2=~x2;
>> imshow(x2)
>> centerIndex=round(size(x2,1)/2);
>> x2(centerIndex:end,:)=cast(0,class(x2));
>> imshow(x2);
>> B=bwboundaries(x2);
>> imshow(x2)
>> text(10,10,strcat('\color{green}Objects found:',num2str(length(B))))
>> C=length(B)

Two problems limited memory and no one wants to wait for an 8-bit processor to even do simple image processing.

The MATLAB web site has extensive information on how to use MATLAB to communicate with and control the Arduino.

This is a pretty good resource:

If you manages to send images directly to matlab then you might skip the SD card usage altogether. That will free significant program space on a low-level 32kbyte arduino