all i've a little trouble understanding some concepts while making game please give your suggestions on my questions.
I have a animated sprite image this i want to move this image according to given path. I can get path from
Path path = new Path();
Point s = new Point(150, 5);
Point cp1 = new Point(140, 125);
Point cp2 = new Point(145, 150);
Point e = new Point(200, 250);
path.moveTo(s.x, s.y);
path.cubicTo(cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
canvas.drawPath(path, paint);
these points are hard coded and give different results on different screen.
Q1. How can i make path which is similar in all screen size?
Q2. How can i move my animated sprite image according to given path with the image has it's head according to path?
Related
Not sure if this is the right way to ask, but please help. I have an image of a dented car. I have to process it and highlight the dents and return the number of dents. I was able to do it reasonably well with the following result:
The matlab code is:
img2=rgb2gray(i1);
imshow(img2);
img3=imtophat(img2,strel('disk',15));
img4=imadjust(img3);
layer=img4(:,:,1);
img5=layer>100 & layer<250;
img6=imfill(img5,'holes');
img7=bwareaopen(img6,5);
[L,ans]=bwlabeln(img7);
imshow(img7);
I=imread(i1);
Ians=CarDentIdentification(I);
However, when I try to do this using opencv, I get this:
With the following code:
Imgproc.cvtColor(source, middle, Imgproc.COLOR_RGB2GRAY);
Imgproc.equalizeHist(middle, middle);
Imgproc.threshold(middle, middle, 150, 255, Imgproc.THRESH_OTSU);
Please tell me how can I obtain better results in opencv, and also how to count the dents? I tried findcontour() but it gives a very large number. I tried on other images as well, but I'm not getting proper results.
Please help.
So you basically from the MATLAB site, imtophat does - Top-hat filtering computes the morphological opening of the image (using imopen) and then subtracts the result from the original image.
You could do this in OpenCV with the following steps:
Step 1: Get the disk structuring element
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15, 15))
Step 2: Compute opening of the image and then subtract the result from the original image
tophat = cv2.morphologyEx(v, cv2.MORPH_TOPHAT, kernel)
This gives following result -
Step 3 - Now you could just manually threshold it or use Otsu -
ret, thresh = cv2.threshold(tophat, 17, 255, 0)
which gives you the following image -
Since the OP wants the code in Java, here is the probable code in Java:
private Mat topHat(Mat image)
{
Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, new Size(15, 15), new Point (0, 0));
Mat dst = new Mat;
Imgproc.morphologyEx(image, dst, Imgproc.MORPH_TOPHAT, element, new Point(0, 0));
return dst;
}
Make sure you do this on a gray scale image (CvType.8UC1) and then you can threshold suitably.
I have a problem filling a path drawn on a canvas.
I read all those previous questions saying
Paint red = new Paint();
red.setColor(Color.RED);
red.setStyle(Paint.Style.FILL_AND_STROKE);
should be the way it works. Then I drew my Path like
Path p = new Path();
p.moveTo(100,100); //point1
p.lineTo(200,200);
p.moveTo(200,200); //point2
p.lineTo(100,200);
p.moveTo(100,200); //point3
p.lineTo(100,100);
p.close();
Which should be a closed path in my opinion.
After canvas.drawPath(p, red);
the triangle/path is drawn but not filled although style is FILL_AND_STROKE.
What am I getting wrong?
There is no path to fill because your path consists of multiple lines, but no coherent polygon. See moveTo documentation, which reads:
Set the beginning of the next contour to the point (x,y).
So by calling moveTo you are only drawing lines.
Only use lineTo(), which also "moves" to the target position. And you can skip the last lineTo() going to the point of origin, close() will do this automatically.
// create a triangle
Path p = new Path();
p.moveTo(100,100); //p1
p.lineTo(200,200); //p2
p.lineTo(100,200); //p3
p.close();
I follow many blogs but didn't get the proper answer. Main problem is to create the circular progress which is showing with the help of some graphics. However not able to move towards success.
Path p = new Path();
p.moveTo(70, 10);
p.lineTo(25, 100);
p.lineTo(100, 50);
p.lineTo(0, 50);
p.lineTo(75, 100);
p.lineTo(50, 0);
// chk
// //complete code
ShapeDrawable progress1 = new ShapeDrawable(new ArcShape(180, -45));
progress1.setIntrinsicHeight(50);
progress1.setIntrinsicWidth(50);
progress1.getPaint().setColor(Color.BLUE);
progress1.getPaint().setStyle(Style.STROKE);
progress1.getPaint().setStrokeWidth(5);
ShapeDrawable progress2 = new ShapeDrawable(new ArcShape(0, 180));
progress2.setIntrinsicHeight(50);
progress2.setIntrinsicWidth(50);
progress2.getPaint().setARGB(50, 200, 54, 54);
progress2.getPaint().setStyle(Style.STROKE);
progress2.getPaint().setStrokeWidth(5);
iView.setImageDrawable(progress2);
iView1.setImageDrawable(progress1);
Without knowing which part you're stuck on, what you'd probably do is make a custom view where you define the drawing code in its onDraw method.
You can probably get away with drawing arced lines using a thick stroke in the corresponding target color. To make the ends of the arc rounded, configure the Paint object's properties to suit your needs, iirc the right method to use is Paint#setStrokeCap
I am working on andengine and i have two sprite one is plate and the other is an apple . My plate sprite move form point 1 to point 2 and my apple sprite is jumping up and down.
Now i want to make apple jump on plate. I tried it with attched child apple with plate but the apple not place on the plate. Apple place below the plate i used zindex but its not working.
Actually problem is to move apple and plate at the same time. Any help would be appriciated. I am stuck with that that why this is happening and what will be solution .Here is my code:
plateDisplay = new Sprite( 250, 300, this.plate, this.getVertexBufferObjectManager());
appleDisplay = new Sprite( 250, 140, this.apple, this.getVertexBufferObjectManager());
plateDisplay.registerEntityModifier(new LoopEntityModifier(new PathModifier(20, path, EaseLinear.getInstance())));
appleDisplay.registerEntityModifier(new LoopEntityModifier(new ParallelEntityModifier(new MoveYModifier(1, appleDisplay.getY(),
(appleDisplay.getY()+70), EaseBounceInOut.getInstance()))));
this.appleDisplay.setZIndex(1);
plateDisplay.setZIndex(0);
plateDisplay.attachChild(this.appleDisplay);
scene.attachChild(plateDisplay);
The issue you are having is that there are different coordinate systems for each object. The Plate sprite has its own X and Y in the scene coordinates. But when you add the apple to the plate object you are now using the plates local coordinates. So if the apple was on the scene's 50,50, when you add it to the plate, it will now be 50,50 as measured from the transform center point of the plate.
There are LocaltoScene and ScenetoLocal coordinate utilities in andengine to help you make this conversion. But underneath they are not super complex - they just add the transforms of all the nested sprites. Both utilites are part of the Sprite class, so you call them from the sprite in question. In your case probably
// Get the scene coordinates of the apple as an array.
float[] coodinates = [appleDisplay.getX(), appleDisplay.getY()];
// Convert the the scene coordinates of the apple to the local corrdinates of the plate.
float[] localCoordinates = plateDisplay.convertSceneToLocalCoordinates(coordinates);
// Attach and set position of apple
appleDisplay.setPosition(localCoordinates[0], localCoordintates[1]);
plateDisplay.attachChild(appleDisplay);
In Android, I have a Path object which I happen to know defines a closed path, and I need to figure out if a given point is contained within the path. What I was hoping for was something along the lines of
path.contains(int x, int y)
but that doesn't seem to exist.
The specific reason I'm looking for this is because I have a collection of shapes on screen defined as paths, and I want to figure out which one the user clicked on. If there is a better way to be approaching this such as using different UI elements rather than doing it "the hard way" myself, I'm open to suggestions.
I'm open to writing an algorithm myself if I have to, but that means different research I guess.
Here is what I did and it seems to work:
RectF rectF = new RectF();
path.computeBounds(rectF, true);
region = new Region();
region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
Now you can use the region.contains(x,y) method.
Point point = new Point();
mapView.getProjection().toPixels(geoPoint, point);
if (region.contains(point.x, point.y)) {
// Within the path.
}
** Update on 6/7/2010 **
The region.setPath method will cause my app to crash (no warning message) if the rectF is too large. Here is my solution:
// Get the screen rect. If this intersects with the path's rect
// then lets display this zone. The rectF will become the
// intersection of the two rects. This will decrease the size therefor no more crashes.
Rect drawableRect = new Rect();
mapView.getDrawingRect(drawableRect);
if (rectF.intersects(drawableRect.left, drawableRect.top, drawableRect.right, drawableRect.bottom)) {
// ... Display Zone.
}
The android.graphics.Path class doesn't have such a method. The Canvas class does have a clipping region that can be set to a path, there is no way to test it against a point. You might try Canvas.quickReject, testing against a single point rectangle (or a 1x1 Rect). I don't know if that would really check against the path or just the enclosing rectangle, though.
The Region class clearly only keeps track of the containing rectangle.
You might consider drawing each of your regions into an 8-bit alpha layer Bitmap with each Path filled in it's own 'color' value (make sure anti-aliasing is turned off in your Paint). This creates kind of a mask for each path filled with an index to the path that filled it. Then you could just use the pixel value as an index into your list of paths.
Bitmap lookup = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
//do this so that regions outside any path have a default
//path index of 255
lookup.eraseColor(0xFF000000);
Canvas canvas = new Canvas(lookup);
Paint paint = new Paint();
//these are defaults, you only need them if reusing a Paint
paint.setAntiAlias(false);
paint.setStyle(Paint.Style.FILL);
for(int i=0;i<paths.size();i++)
{
paint.setColor(i<<24); // use only alpha value for color 0xXX000000
canvas.drawPath(paths.get(i), paint);
}
Then look up points,
int pathIndex = lookup.getPixel(x, y);
pathIndex >>>= 24;
Be sure to check for 255 (no path) if there are unfilled points.
WebKit's SkiaUtils has a C++ work-around for Randy Findley's bug:
bool SkPathContainsPoint(SkPath* originalPath, const FloatPoint& point, SkPath::FillType ft)
{
SkRegion rgn;
SkRegion clip;
SkPath::FillType originalFillType = originalPath->getFillType();
const SkPath* path = originalPath;
SkPath scaledPath;
int scale = 1;
SkRect bounds = originalPath->getBounds();
// We can immediately return false if the point is outside the bounding rect
if (!bounds.contains(SkFloatToScalar(point.x()), SkFloatToScalar(point.y())))
return false;
originalPath->setFillType(ft);
// Skia has trouble with coordinates close to the max signed 16-bit values
// If we have those, we need to scale.
//
// TODO: remove this code once Skia is patched to work properly with large
// values
const SkScalar kMaxCoordinate = SkIntToScalar(1<<15);
SkScalar biggestCoord = std::max(std::max(std::max(bounds.fRight, bounds.fBottom), -bounds.fLeft), -bounds.fTop);
if (biggestCoord > kMaxCoordinate) {
scale = SkScalarCeil(SkScalarDiv(biggestCoord, kMaxCoordinate));
SkMatrix m;
m.setScale(SkScalarInvert(SkIntToScalar(scale)), SkScalarInvert(SkIntToScalar(scale)));
originalPath->transform(m, &scaledPath);
path = &scaledPath;
}
int x = static_cast<int>(floorf(point.x() / scale));
int y = static_cast<int>(floorf(point.y() / scale));
clip.setRect(x, y, x + 1, y + 1);
bool contains = rgn.setPath(*path, clip);
originalPath->setFillType(originalFillType);
return contains;
}
I know I'm a bit late to the party, but I would solve this problem by thinking about it like determining whether or not a point is in a polygon.
http://en.wikipedia.org/wiki/Point_in_polygon
The math computes more slowly when you're looking at Bezier splines instead of line segments, but drawing a ray from the point still works.
For completeness, I want to make a couple notes here:
As of API 19, there is an intersection operation for Paths. You could create a very small square path around your test point, intersect it with the Path, and see if the result is empty or not.
You can convert Paths to Regions and do a contains() operation. However Regions work in integer coordinates, and I think they use transformed (pixel) coordinates, so you'll have to work with that. I also suspect that the conversion process is computationally intensive.
The edge-crossing algorithm that Hans posted is good and quick, but you have to be very careful for certain corner cases such as when the ray passes directly through a vertex, or intersects a horizontal edge, or when round-off error is a problem, which it always is.
The winding number method is pretty much fool proof, but involves a lot of trig and is computationally expensive.
This paper by Dan Sunday gives a hybrid algorithm that's as accurate as the winding number but as computationally simple as the ray-casting algorithm. It blew me away how elegant it was.
See https://stackoverflow.com/a/33974251/338479 for my code which will do point-in-path calculation for a path consisting of line segments, arcs, and circles.