i have problem with detection road lanes with my phone.
i wrote some code for road lanes detection, but him not working for me.
From camera get modifications from normal view to BGR colors and try use GausianBlur and Canny, but i think i not good draw lanes for detection.
Maybe some people have another idea how detection road lanes with OpenCV?
Mat mYuv = new Mat(height + height / 2, width, CvType.CV_8UC1);
Mat mRgba = new Mat(height + height / 2, width, CvType.CV_8UC1);
Mat thresholdImage = new Mat(height + height / 2, width, CvType.CV_8UC1);
mYuv.put(0, 0, data);
Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420p2BGR, 4);
//convert to grayscale
Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_mRGBA2RGBA, 4);
// Perform a Gaussian blur (convolving in 5x5 Gaussian) & detect edges
Imgproc.GaussianBlur(mRgba, mRgba, new Size(5,5), 2.2, 2);
Imgproc.Canny(mRgba, thresholdImage, VActivity.CANNY_MIN_TRESHOLD, VActivity.CANNY_MAX_THRESHOLD);
Mat lines = new Mat();
double rho = 1;
double theta = Math.PI/180;
int threshold = 50;
//do Hough transform to find lanes
Imgproc.HoughLinesP(thresholdImage, lines, rho, theta, threshold, VActivity.HOUGH_MIN_LINE_LENGTH, VActivity.HOUGH_MAX_LINE_GAP);
for (int x = 0; x < lines.cols() && x < 1; x++){
double[] vec = lines.get(0, x);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Core.line(mRgba, start, end, new Scalar(255, 0, 0), 3);
}
This approach is fine and I've done something similar, not for road line detection but I did notice that it could be used for that purpose. Some comments:
Not sure why you do:
Imgproc.cvtColor(mRgba, thresholdImage, Imgproc.COLOR_mRGBA2RGBA, 4);
as 1. the comment say convert to greyscale, which is a single channel and 2. thresholdImage will get overwritten with the call to Canny later. You just need to dimension thresholdImage with:
thresholdImage = new Mat(mRgba.size(), CvType.CV_8UC1);
What are your parameter values to the call to Canny? I played about with mine considerably and ended up with values like: threshold1 = 441, threshold2 = 160, aperture = 3.
Likewise Imgproc.HoughLinesP: I use Imgproc.HoughLines rather than Imgproc.HoughLinesP with parameters: threshold = 80, minLen = 30, maxLen = 10.
Also have a look at:
for (int x = 0; x < lines.cols() && x < 1; x++){
&& x < 1 means you will only take the first line that the call to HoughLinesP returns. I'd suggest you remove this and use some other criteria to reduce the number of lines; for example, I was interesting in only horizontal and vertical lines so I used atan2 to calculate line angles and exclude those that deviate too much.
UPDATE
Here is how I get the angle of a line. Assuming coordinates of one point is (x1,y1) and the other (x2, y2) then to get the angle:
double lineAngle = Math.atan2(y2 - y1, x2 - x1);
this should return an angle in radians between -PI/2 and PI/2
With regard Canny parameters then I would experiment - I set up onTouch so that I could adjust the threshold values by touching certain parts of the screen to see the effects in realtime. Note that aperture is rather disappointing parameter: it seems to only like odd values 3, 5 and 7 and 3 is the best that I've found.
Something like in the onTouch method:
int w = mRgba.width();
int h = mRgba.height();
float x = event.getX();
float y = event.getY();
if ((x < w / 3) && (y < h / 2)) t1 += 20;
if ((x < w / 3) && (y >= h / 2)) t1 -= 20;
if ((x > 2 * w / 3) && (y < h / 2)) t2 += 20;
if ((x > 2 * w / 3) && (y >= h / 2)) t2 -= 20;
t1 and t2 being the threshold values passed to the Canny call.
Related
Good day.I am creating a siri like wave for android and i encounter an big issue.I need the wave to be in 4 colors.Lets assume i only have one single line which is drawing on the screen accordingly to the voice decibels.Anyway i am able to do it but no way i am able to give 4 different colors for same path.Assume it is 1 single path which moves from screen start to screen end,i need that line to have 4 different colors,mainly i had to divide the path into 4 parts and draw the color for each parts,but neither google,nor any other source give me anything (not even found anything similar to what i want).
Meanwhile i am posting the code where actually i am drawing the lines.
for (int l = 0; l < mWaveCount; ++l) {
float midH = height / 2.0f;
float midW = width / 2.0f;
float maxAmplitude = midH / 2f - 4.0f;
float progress = 1.0f - l * 1.0f / mWaveCount;
float normalAmplitude = (1.5f * progress - 0.5f) * mAmplitude;
float multiplier = (float) Math.min(1.0, (progress / 3.0f * 2.0f) + (1.0f / 3.0f));
if (l != 0) {
mSecondaryPaint.setAlpha((int) (multiplier * 255));
}
mPath.reset();
for (int x = 0; x < width + mDensity; x += mDensity) {
float scaling = 1f - (float) Math.pow(1 / midW * (x - midW), 2);
float y = scaling * maxAmplitude * normalAmplitude * (float) Math.sin(
180 * x * mFrequency / (width * Math.PI) + mPhase) + midH;
// canvas.drawPoint(x, y, l == 0 ? mPrimaryPaint : mSecondaryPaint);
//
// canvas.drawLine(x, y, x, 2*midH - y, mSecondaryPaint);
if (x == 0) {
mPath.moveTo(x, y);
} else {
mPath.lineTo(x, y);
// final float x2 = (x + mLastX) / 2;
// final float y2 = (y + mLastY) / 2;
// mPath.quadTo(x2, y2, x, y);
}
mLastX = x;
mLastY = y;
}
if (l == 0) {
canvas.drawPath(mPath, mPrimaryPaint);
} else {
canvas.drawPath(mPath, mSecondaryPaint);
}
}
I tried to change color on if (l == 0) {
canvas.drawPath(mPath, mPrimaryPaint);
} but if i change it here,no result at all,either the line is separate and not moving at all,but it should,either the color is not applied,propably because i am doing it in loop as i had to and everytime the last color is picked to draw.Anyway can you help me out?Even an small reference is gold for me because really there is nothing at all in the internet.
Anyway even though Matt Horst answer fully correct,i found the simplest and easiest solution...i never thought it would be so easy.Anyway if in world there is someone who need to make an path divided into multiple colors,here is what you can do
int[] rainbow = getRainbowColors();
Shader shader = new LinearGradient(0, 0, 0, width, rainbow,
null, Shader.TileMode.REPEAT);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
mPrimaryPaint.setShader(shader);
Where getRainbowColors() is an array of colors you wish your line to have and width is the length of the path so the Shader knows how to draw the colors in right way to fit the length of path.Anyway .....easy isnt it?and pretty purged me a lot to get into this simple point.Nowhere in internet you could find only if you are looking for something completelly different,than you might come across this.
It seems to me like you could set up one paint for each section, each with a different color. Then set up one path for each section too. Then as you draw across the screen, wherever the changeover point is between sections, start drawing with the new path. And make sure first to use moveTo() on the new path so it starts off where the old one left off.
For my solution, I tried changing the color of the linePaint in the onDraw Call. But it was drawing a single color.
So i used two different paints for two different colors and draw path on the canvas.
And it worked. Hope it helps someone out there.
I am trying to make a perspective correction for quadrilateral objects using opencv3. I managed to show the lines and also implemented the Houghlines using Imgproc.HoughLinesP() and tried to highlight the lines using Imgproc.lines() but output was no success. Below is my code and also I have attached my output image.
Please let me know what is wrong happening and what should be done...
Mat initImg; // initial image
Mat greyImg; // converted to grey
Mat lines = new Mat();
int threshold = 50;
int minLineSize = 20;
int lineGap = 10;
initImg = Imgcodecs.imread(imgLoc, 1);
greyImg = new Mat();
Imgproc.cvtColor(initImg, greyImg, Imgproc.COLOR_BGR2GRAY);
Bitmap bitm = Bitmap.createBitmap(greyImg.cols(), greyImg.rows(),Bitmap.Config.ARGB_8888);
Imgproc.blur(greyImg, greyImg, new Size(3.d, 3.d));
Imgproc.adaptiveThreshold(greyImg, greyImg, 255, Imgproc.ADAPTIVE_THRESH_MEAN_C, Imgproc.THRESH_BINARY_INV, 15, 4);
Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
minLineSize, lineGap);
// lines returns rows x columns and rows is always 1. I dont know why please help me to understand
for (int x = 0; x < lines.cols(); x++) {
double[] vec = lines.get(0, x);
double[] val = new double[4];
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
System.out.println(TAG+"Coordinates: x1=>"+x1+" y1=>"+y1+" x2=>"+x2+" y2=>"+y2);
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
Imgproc.line(greyImg, start, end, new Scalar(0,255, 0, 255), 3);
}
Utils.matToBitmap(greyImg, bitm);
if(bitm!=null){
Toast.makeText(getApplicationContext(), "Bitmap not null", Toast.LENGTH_SHORT).show();
iv.setImageBitmap(bitm);
}else{
Toast.makeText(getApplicationContext(), "Bitmap null", Toast.LENGTH_SHORT).show();
}
My Output:
I finally managed to get the houghlines. The houghlines was not showing in the grey images and I extracted the houghlines in grey image but plotted the lines in color image and it worked.
Imgproc.HoughLinesP(greyImg, lines, 1, Math.PI/180, threshold,
minLineSize, lineGap);
for (int x = 0; x < lines.rows(); x++)
{
double[] vec = lines.get(x, 0);
double x1 = vec[0],
y1 = vec[1],
x2 = vec[2],
y2 = vec[3];
Point start = new Point(x1, y1);
Point end = new Point(x2, y2);
double dx = x1 - x2;
double dy = y1 - y2;
double dist = Math.sqrt (dx*dx + dy*dy);
if(dist>300.d) // show those lines that have length greater than 300
Imgproc.line(initImg, start, end, new Scalar(0,255, 0, 255),5);// here initimg is the original image.
}
I am developing an android application where I need to detect the blinking of eyes. So far I have been able to detect the face and eyes using OpenCV. But now I need to check if the eyes are open or close. I read somewhere that one of the ways I can do that is by measuring the pixel intensities (grey levels). But it was not properly explained as in how to do that step by step. I am actually new to OpenCV. So can anyone please help me how can I do that. It is really very important.
Here is my onCameraFrame method:
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
mGray = inputFrame.gray();
if (mAbsoluteFaceSize == 0) {
int height = mGray.rows();
if (Math.round(height * mRelativeFaceSize) > 0) {
mAbsoluteFaceSize = Math.round(height * mRelativeFaceSize);
}
}
if (mZoomWindow == null || mZoomWindow2 == null)
CreateAuxiliaryMats();
MatOfRect faces = new MatOfRect();
if (mJavaDetector != null)
mJavaDetector.detectMultiScale(mGray, faces, 1.1, 2,
2, // TODO: objdetect.CV_HAAR_SCALE_IMAGE
new Size(mAbsoluteFaceSize, mAbsoluteFaceSize),
new Size());
Rect[] facesArray = faces.toArray();
for (int i = 0; i < facesArray.length; i++) {
Core.rectangle(mRgba, facesArray[i].tl(), facesArray[i].br(),
FACE_RECT_COLOR, 2);
xCenter = (facesArray[i].x + facesArray[i].width + facesArray[i].x) / 2;
yCenter = (facesArray[i].y + facesArray[i].y + facesArray[i].height) / 2;
Point center = new Point(xCenter, yCenter);
Rect r = facesArray[i];
// compute the eye area
Rect eyearea = new Rect(r.x + r.width / 20,
(int) (r.y + (r.height / 20)), r.width - 2 * r.width / 20,
(int) (r.height / 9.0));
// split it
Rect eyearea_right = new Rect(r.x + r.width / 6,
(int) (r.y + (r.height / 4)),
(r.width - 2 * r.width / 16) / 3, (int) (r.height / 4.0));
Rect eyearea_left = new Rect(r.x + r.width / 11
+ (r.width - 2 * r.width / 16) / 2,
(int) (r.y + (r.height / 4)),
(r.width - 2 * r.width / 16) / 3, (int) (r.height / 4.0));
// draw the area - mGray is working grayscale mat, if you want to
// see area in rgb preview, change mGray to mRgba
Core.rectangle(mRgba, eyearea_left.tl(), eyearea_left.br(),
new Scalar(255, 0, 0, 255), 2);
Core.rectangle(mRgba, eyearea_right.tl(), eyearea_right.br(),
new Scalar(255, 0, 0, 255), 2);
if (learn_frames < 5) {
teplateR = get_template(mJavaDetectorEye, eyearea_right, 24);
teplateL = get_template(mJavaDetectorEye, eyearea_left, 24);
learn_frames++;
} else {
// Learning finished, use the new templates for template
// matching
match_eye(eyearea_right, teplateR, method);
match_eye(eyearea_left, teplateL, method);
}
}
return mRgba;
}
Thanks in advance.
I already worked on this problem and this algorithm. It's implemented (C++) here: https://github.com/maz/blinking-angel with algorithm here: http://www.cs.bu.edu/techreports/pdf/2005-012-blink-detection.pdf .
As far as I can remember:
You get B&W current and 100ms ago frames
You do new - old (see 154 in github code)
You apply a threshold then a dilatation filter
You compute contours
If you have a blob with area > to a threshold at the eye location, it means that user blinked eyes
Give a look at the is_blink function at line 316. In his case, he do w * h of blob surrounding box > to a threshold.
In fact it use difference between eye/skin color. In Github implementation, threshold > 5 is used.
What I did was that I converted the eye region from RGB to HSV and applied skin detection. I found out the range of skin color in HSV. If the percentage of skin pixels is greater than threshold value that means Eye is close otherwise it is open. Though there is still some accuracy issue due to amount of light present. Thank you all for giving me a start :)
Generally there is no obvious solution for this problem, but the number of approaches is quite big, so i'm sure that after a bit of serching you will find something good enough for you. For example you can use algorithm which i mentioned here (there is a link to this question in "Related" btw - check other link from this group as well).
I have a rectangle with known size and position. (flag)
I have to fill this rectangle with 4 other rectangles. (stripes)
Each stripe must have 1/4 of the total width of the flag and his position is near the previous.
I have to draw this stripes with a random angle that goes from 0° to 90°.
0° = Vertical stripes (stripe width = flag width / 4)
90° = Horizontal stripes (stripe width = flag height / 4)
How can I calculate the width of each stripe for other angles?
int stripes = 4;
RectF rect = new RectF(0, 0, 100f, 75f);
float angle = new Random.nextInt(90);
float stripeSize;
if (angle == 0) {
stripeSize = rect.width() / stripes;
} else if (angle == 90) {
stripeSize = rect.height() / stripes;
} else {
stripeSize = ?
}
canvas.save();
canvas.rotate(angle, rect.centerX(), rect.centerY());
float offset = 0;
for (int i = 0; i < stripes; i++) {
if (angle == 0) {
reusableRect.set(offset, rect.top, offset + stripeSize, rect.bottom);
} else if (angle == 90) {
reusableRect.set(rect.left, offset, rect.right, offset + stripeSize);
} else {
reusableRect.set(?, ?, ?, ?);
}
canvas.drawRect(reusableRect, paint);
offset += stripeSize;
}
canvas.restore();
Let's pretend you have one stripe. Depending on the angle, the stripe width is going to be a value between the shorter dimension (the height in your case) and the longer dimension (the width in your case). The formula for the stripe width calculation should look something like this:
height + ((width - height) * ?)
where ? varies between 0 and 1 based on the angle of rotation. To me that sounds like the sine function might be a good candidate: sine(0) = 0 and sine(90) = 1. You can use Math.sin(), but be aware that the argument it takes is in radians, not degrees, so you need to use Math.toRadians() on your angle first. Then just divide by the number of stripes:
double radians = Math.toRadians(angle);
float stripeTotal = height + ((width - height) * Math.sin(radians));
float stripeWidth = stripeTotal / 4; // or however many stripes you have
If it's not perfect, you can adjust the formula. One last point, since these values only need to be calculated once, I would do that separately every time the angle changes (if it ever changes), not inside of onDraw().
I have a screen (480x800), M(mx, my) is a static point, and N(nx,ny) is a dynamic point on screen. Position of N(nx, ny) depends position of touch. I want to determine position of P(?,?) and Q(?,?) to draw line 1 and line 2. line 2 is reflective line 1.
This is my code:
private Line l2;
#Override
public boolean onSceneTouchEvent(final Scene pScene,
final TouchEvent pSceneTouchEvent) {
if (this.mPhysicsWorld != null) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
// Get position
p1x = pSceneTouchEvent.getX();
p1y = pSceneTouchEvent.getY();
return true;
case TouchEvent.ACTION_MOVE:
// Remove instance of the old line
mScene.detachChild(l2);
p3x = pSceneTouchEvent.getX();
p3y = pSceneTouchEvent.getY();
Rectangle testR = new Rectangle(CAMERA_WIDTH / 2,
CAMERA_HEIGHT / 2, 20, 20,
getVertexBufferObjectManager());
l2 = new Line(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, p3x, p3y,
getVertexBufferObjectManager());
l2.setColor(new Color(223f / 255f, 118f / 255f, 43f / 255f));
l2.setLineWidth(5);
mScene.attachChild(l2);
return true;
}
return false;
}
return false;
}
If you have other ways to resolve my issue. Please share with me. Thanks.
I didn't program with AndEngine for a while, but this isn't about AndEngine so I can give you a psedu code to solve your problem.
1) Check that nx < mx
2) Calculate the slope of line 1 :
line1slope = (my-ny)/(mx-nx)
3) Find P coordinates using the equation : y-y1 = m(x-x1)
Where m = line1slope
y1 = my
x1 = mx
y = 480 (P's y coordinate) ( if line1slope > 0 then y = 0)
And then you can find your x ( which is P's x coordinate)
line2slope = -1 * line1slope because they are reflective
Now again you need to find Q (You know the X = 0 so you need to find only the Y coordinate)
using the equation : y-y1 = m(x-x1)
Where m = line2slope
y1 = py
x1 = px
x = 0
And then you can find your y coordinate(which is Q's y coordinate)
Hope it helps.