I want to understand more about what I found about all point(s) in a polygon intersecting a bitmap in Android. Say I have a bitmap and I use the getPixels function to retrieve all of my pixel values in that bitmap. Then I draw a polygon of any shape such that part of it intersects with the bitmap. (At any part)
I want then is all of the pixel values of the intersect of the bitmap and the polygon.
Here is an example image of what I am talking about:
The shaded part is the intersection of pixels of the bitmap (larger rectangle) and the closed area of the polygon(smaller rectangle) that I want
From the post about "Point in Polygon" describes using ray casting, a straight line/ray horizontally across the shape, we find how many times the ray intersects the polygon's vertices. I think my solution is an extension of this concept but I am stuck about my approach.
I thought of having multiple rays and somehow grab the number of times that the pixels of the polygon and the pixels of the bitmap intersect but I believe this approach is not practical.
Can someone help with this issue?
Please do not write up a solution as I want to write one on my own although example code and how it works are appreicated as I am a beginner in Android.
Related
I need to fill the color inside custom shape. I know one of the pixel coordinate inside the shape and also know the color of the shape. I need to get all pixel inside the shape calculate from knowing pixel coordinate how can i write logic for this can some one please help.
For already rendered shapes
use Flood Fill or Boundary fill algorithms. For example see:
my C++ implementation of Flood&Boundary fill
beware most implementations are recursive so there is a high risk of stack/heap overflow for bigger areas. There are also iterative approaches using dynamic point lists which are safer.
For polygons in vector form
Use convex polygon filling. In case you got concave polygons you need to divide them into convex polygons first. Another options is to triangulate. Both triangle and convex polygon rasterization is the same see:
Algorithm to fill triangle
how to rasterize rotated rectangle (in 2d by setpixel)
If you need triangulation see:
Wiki: Polygon triangulation
In case of very complicated shapes and not possible triangulation for any reason you can try fill all the pixels in the bounding box of polygon that are inside polygon. For that you need:
hit-test
Let's say I have a triangular face in 3d space, and I have the 3d coordinates of each vertex of this triangle, and would also have other information about the triangle(angles, lengths of sides, etc.). In Java, if I have the viewing screen and its information, how can I draw that plane, without using libraries like LWJGL, to that image, assuming I can properly project, accounting for perspective, any 3d point to that 2d image.
Would the best course of action just be to run a loop that draws each point on the plain to a point on the image(i.e. setting the corresponding pixel), which will most likely set the same pixel multiple times? If I'd do this, what would be the best way to identify each point in an oblique triangle, or a triangle that doesn't line up nicely with the axes?
tl;dr: I have a triangular face in 3d space, a "camera" looking at the face, and an image in which I can set each pixel. Using no GL libraries, what's the best way to project and draw that face onto the image?
Projection :
won't detail as you seems to know it
Drawing a line
you can look at Bresenham algorithm if you wanna start with the basics
(hardwared in recent graphics card)
Filling
you can fill between left and right borders of the triangle while you use Bresenham on both (you could use a floodfill algorithm starting ... i don't know, maybe at the projection of the center of the triangle)
Your best bet is to check out the g.fillPolygon() function for Java. It allows you to draw polygons with as many sides as possible and theres also g.drawPolygon() if you don't want it solid. Then you can just do some simple maths for the points. Such as each point is basically it's x and y except if the polygon is further away the points move closer to the center of the polygon and if the polygon is closer they move further away from the center of the polygon.
A second idea could be using some sort of array to store pixels and then researching line drawing algorithms and drawing lines then putting all the line data in another array and using some sort of flood-fill. Then whilst it's in that array you could try and do some weird stuff to the pixels if you wanted textures or something.
While working on Projectiles I thought that it would be a good idea to rotate the sprite as well, to make it look nicer.
I am currently using a 1-Dimensional Array, and the sprite's width and height can and will vary, so it makes it a bit more difficult for me to figure out on how to do this correctly.
I will be honest and straight out say it: I have absolutely no idea on how to do this. There have been a few searches that I have done to try to find some stuff, and there were some things out there, but the best I found was this:
DreamInCode ~ Rotating a 1-dimensional Array of Pixels
This method works fine, but only for square Sprites. I would also like to apply this for non-square (rectangular) Sprites. How could I set it up so that rectangular sprites can be rotated?
Currently, I'm attempting to make a laser, and it would look much better if it didn't only go along a vertical or horizontal axis.
You need to recalculate the coordinate points of your image (take a look here). You've to do a matrix product of every point of your sprite (x, y) for the rotation matrix, to get the new point in the space x' and y'.
You can assume that the bottom left (or the bottom up, depends on your system coordinate orientation) of your sprite is at (x,y) = (0,0)
And you should recalculate the color too (because if you have a pure red pixel surrounded by blue pixel at (x,y)=(10,5) when you rotate it can move for example to (x, y)=(8.33, 7.1) that it's not a real pixel position because pixel haven't float coordinate. So the pixel at real position (x, y)=(8, 7) will be not anymore pure red, but a red with a small percentage of blue)... but one thing for time.
It's easier than you think: you only have to copy the original rectangular sprites centered into bigger square ones with transparent background. .png files have that option and I think you may use them.
I, like many before me, am trying to digitize a Sudoku board from an image. I have:
Gray-scaled, blurred and thresholded the image into a crisp, binary image
Identified the largest contour which correctly corresponds to the perimeter of the Sudoku board
However, if the paper is curved, the board's contour won't fit neatly into a boundingRect; its edges will be elliptical.
What I would really like is a way of taking some MatOfPoint maxContour;
and warping the corresponding region in the parent Mat to be a square, so that I can remove the board as a perfect square and manipulate it on its own.
However, ANY advice on getting the board or cells out of the image of Sudoku board are appreciated, I've played with Hough Transforms, the Sobel algorithm, and many other hare-brained schemes that have left me a little frazzled.
Thanks in advance!
One approach is to first get the Minimum bounding rectangle of the rectangle represented by your sudoku corners which is effectively a perspectively transformed rectangle.
Then use the sudoku corners with the resulting rectangle corners to warp the image (or just warp the corners, whatever suits you), this will result in a straight image.
I'm trying to write a graphical effect where a circle moves around an image smudging the image as it goes (like the way the smudge tool in Gimp or Photoshop would work). The basic algorithm I'm using is:
the circle moves from position A to position B on the bitmap
copy a circle of pixels from position A into a temporary bitmap
draw this circle of pixels from the temporary bitmap to position B using alpha of about 50%.
This works fine and looks like what I would expect, where the image will look like it's getting smudged if the circle moves 1 pixel at a time over the image.
I now want to add some texture to the smudge effect. I have a bitmap that contains a picture of a paint blob. The algorithm from the above is modified to the following so the smudge takes the shape of this paint blob:
as before
replace the temporary bitmap pixels with the paint blob texture then copy the circle of pixels from position A into the temporary bitmap but only keep the pixels that match up against paint blob pixels (i.e. use Porter-Duff "source in destination" mode when drawing the circle into the temporary bitmap).
as before
This almost works and it looks like it's fine initially but gradually the smudging makes the colors in my image darker! If the circle passes over the same area several times, the colors eventually change to black. Any ideas what I could be doing wrong?
I've implemented the above in Android. I happened upon this post about bitmaps in Android (like my paint blob texture) being loaded with "premultiplied alpha", where the author says it caused his images to become darker because of it:
http://www.kittehface.com/2010/06/androidbitmap-and-premultiplied-alpha.html
I suspect I'm suffering from a similar problem but I don't understand what's going on well enough and don't know how to fix it. Does anyone have hints at what might be going on?
Well from first glance the reason the image is getting darker is because #3 in the first three steps. You overlaying a pixel over an existing pixel at 50%. You might want to consider using the mean of the original pixel value and the new pixel value. You might want to research some blurring algorithms.