i am doing vision gudied robot with arduino and android through bluetooth communication . i have done the image processing part using andriod.But i am facing problems in image processing. The colored image is getting transformed to black and white image. but the code that is written to detect the black tape is not working properly.. Could some one help me to debug.
public class DrawOnTop extends View
{
BtConn btcon;
//Display
Bitmap mBitmap;
Paint mPaint;
int mImageWidth, mImageHeight;
//Frame data
byte[] mYUVData;
int[] mRGBData;
int[] mBWData;
//FPS
long lastFrame = System.currentTimeMillis();
//PID
float proportional;
float integral;
float derivative;
float last_proportional;
int Kp=1;
int Kd=2;
int Ki=0;
//Motor Controls
int right_speed=0;
int left_speed=0;
int max_speed = 255;
//Line Tracking
static int tracking;
int minPixels = 10;
int threshold = 60;
int tapeCenter;
public DrawOnTop(Context context) {
super(context);
// Start off PAUSED
tracking = 0;
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL);
mPaint.setColor(Color.RED);
mPaint.setTextSize(25);
mBitmap = null;
mYUVData = null;
mRGBData = null;
}
@Override
protected void onDraw(Canvas canvas) {
if (mBitmap != null)
{
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
grayscaleToBlackWhite(mRGBData, mYUVData, mImageWidth, mImageHeight,canvas);
int tapeCenter=DetectingArea(canvas,mRGBData, mImageWidth, mImageHeight);
//PID
proportional = tapeCenter - mImageHeight/2;
integral = integral + proportional;
derivative = proportional - last_proportional;
last_proportional = proportional;
int lineError = (int)(proportional * Kp + integral * Ki + derivative * Kd);
// Restricting the error value between +/- max_speed.
if (lineError< -max_speed)
{
lineError = -max_speed;
}
if (lineError> max_speed)
{
lineError = max_speed;
}
// If error_value is less than zero calculate right turn speed values
if (lineError> 0)
{
right_speed = max_speed + lineError;
left_speed = max_speed;
}
// If error_value is greater than zero calculate left turn values
else
{
right_speed = max_speed;
left_speed = max_speed - lineError;
}
int[] data = new int[3];
data[0] = 0; //Forward
data[1] = right_speed;
data[2] = left_speed;
long curFrame = System.currentTimeMillis();
float fps = 1000/(curFrame - lastFrame);
lastFrame = curFrame;
// Draw bitmap
mBitmap.setPixels(mRGBData, 0, mImageWidth, 0, 0, mImageWidth, mImageHeight);
Rect src = new Rect(0, 0, mImageWidth, mImageHeight);
Rect dst = new Rect(0, 0, canvasWidth, canvasHeight);
canvas.drawBitmap(mBitmap, src, dst, mPaint);
canvas.rotate(-90, canvasWidth/2, canvasHeight/2);
mPaint.setTextSize(25);
canvas.drawText(mImageWidth + "x" + mImageHeight, 200, -140, mPaint);
canvas.drawText("fps: " + fps, 500, -100, mPaint);
}
super.onDraw(canvas);
}
public void grayscaleToBlackWhite(int[] rgb, byte[] yuv420sp, int width, int height, Canvas canvas)
{
final int frameSize = width * height;
mPaint.setTextSize(25);
for (int pix = 0; pix < frameSize; pix++)
{
int pixVal = (0xff & ((int) yuv420sp[pix])) - 16;
if (pixVal < threshold)
{
pixVal = 0;
}
else
pixVal = 255;
rgb[pix] = 0xff000000 | (pixVal << 16) | (pixVal << 8) | pixVal;
}
}
public int DetectingArea(Canvas canvas,int rgb[],int height, int width)
{
int start=-1;
int stop=-1;
int tmpStart=-1;
int tmpStop=-1;
int scanline=-1;
for(int i=0;i<width;i+=10)
{
for(int j=0;j<height;j++)
{
//check for black line only.
//find all black line centres and compare with the centre of the screen
int pixVal = (0xff & (int) rgb[i+j*width]);
if (pixVal == 0)
{
if(tmpStart==-1)
{
tmpStart=j;
}
else
{
tmpStop=j;
if(tmpStop-tmpStart>minPixels &&
(Math.abs((tmpStop-tmpStart)/2 - height/2)
< Math.abs(stop-start)/2 - height/2))
{
start=tmpStart;
stop=tmpStop;
scanline=i;
}
else
{
tmpStart=-1;
tmpStop=-1;
}
}
}
}
}
if (start > -1 && stop > -1 && scanline >-1) {
for (int pix = start; pix <= stop; pix++) {
rgb[scanline+pix*width] = 0xff000000 | (0 << 16) | (255 << 8) | 0;
}
}
return (start+stop)/2;
}
}