Algorithm for Antialiasing Rastor Images - algorithm

I am looking for an efficient algorithm that will look at a four channel image (RGBA), find an object (a group of pixels with Alpha >0 surrounded by pixels with alpha = 0) and then attempt to antialias the edges of the object. Suggestions?

I am guessing that you're trying to describe a situation where the object's image with alpha has been pasted onto an alphaless background, replacing pixels wherever it had alpha > 0, rather than blending properly. In the resulting image, every pixel with alpha=0 is background, every pixel with alpha > 0 is from the object. There's been no blending, just over pasting.
So - an algorithm for guessing what color the pixels where alpha is now non zero were before, would solve the problem. If you had that, you could do the blending properly, at every place where 0% < alpha < 100% to get a proper anti-aliased edge.
One way to guess the missing pixel values is to make each unknown pixel the average of the pixels around it, and keep repeating this until there is little change. This is pretty much the algorithm used for repairing scratches in old photographic images using pixels nearby. You asked for an efficient method. A multigrid method will get you there a little faster when there are larger unknown pixel areas.
Once you have a plausible unknown background, blend back in the pixels with alpha > 0, this time properly taking into account their alpha.

Related

Crop to exclude any transparency on a ragged edge

I have a square image with a ragged edge: the transparent pixels outside the image "weave" in and out towards the image center, within some unknown range. This range may be different for each side.
Is there an algorithm that would crop the image to the largest size possible with no transparent pixels remaining? I can think of an iterative one: start with a small cropping square in the center. If no transparent pixels are detected, start again but enlarge the cropping square by 1 pixel. Then repeat. Once you detect transparent pixels after cropping, go back one step and save the result.
There is an obvious algorithm that comes to mind:
Find y* = min_y {(x,y) : P(x,y) is transparent }, where P(x,y) is the pixel at coord (x,y) then crop the image [0,y*] (assuming the image starts at zero at the bottom, and that transparent pixels always happen at the top of the image.)
Note that this algorithm has serious downsides, if y* happens to be very close to 0 because of an errant transparent pixel, you will end up cropping almost your entire image.
If you want a more robust solution, I believe you will have to frame this problem as an optimization problem and solve it, allowing for some errant transparent pixels to be masked instead of cropped. The algorithm that would do well for you would be an energy-based formulation which could be solved using graph-cuts. For example, see the GrabCut algorithm.
If you know your requirements and how bad your data can get you can make a judgment call for how involved you want to make your solution, but at this point I would highly recommend clarifying the requirements of your solution as well as how bad your data can get.

Removing semi-transparent overlay on flattened image

A solution to this problem seems to not exist but I find it hard to believe it is not possible.
Imagine you have an image with a semi-transparent overlay (color=black, transparency=50%), whether over the whole image or just a portion, doesn't matter. How could one convert the pixels underneath to their original color, in essence removing the black overlay.
Just like a simple algebra equation we should be able to rearrange the variables to solve for the "original pixels" under the overlay. Something along the lines of -
original pixels * semi-transparent overlay = new pixelsoriginal pixels = semi-transparent overlay / new pixels
Obviously such an equation over simplifies the problem but I think that gets my point across. Since we know the color and percent transparency, why couldn't we "retrieve" the colors of the underlying pixels?
EDIT: Mark Ransom in the comments is correct, if you know the transparency is 50% then simply multiplying by 2 gets you to the original color. Any recommendations on how to apply this to a whole region in Photoshop or GIMP? Certainly doing it pixel by pixel is out of the question.
Thank you!
The "divide" layer mode will do what you want. In the case of semi-transparent black, use a gray with the value equal to the opacity value of the overlayed layer.

Anti Aliasing - Alpha Image [Unity3D]

I have a system that removes the colour white (give or take a few shades), from an image and replaces it with an alpha channel. (The image is taken from the users phone camera, and tries to remove selected colouring)
This leaves harsh edges most of the time, and I want to know if it is possible to add some type of anti-aliasing on top.
The system works by taking in the image, and searching through each pixel data. If the pixel is white (or close), it will replace it with an alpha colour.
So I guess my question is, how do I make the edges less harsh. Thanks.
Anti aliasing is not what you are looking for. This takes care of effects caused by the limited resolution of your image. However, your problem is not related to resolution, you would still have it with infinite resolution.
What you need to do is when you find a white pixel, increase the transparency of the pixel itself and the pixels around it.
You can just include the four pixels immediately above, below, left or right of your white pixel, or you an choose any other shape, e.g. all pixels which lie inside a circle of given radius around the white pixel.
Also you can choose a function which determines how transparency is distributed over that shape. You can make everything half-transparent or you can decrease the effect towards the edges of that shape (though I don't think that this will be necessary).
Thus each pixel will receive transparency from several pixels around them. The resulting transparency must be computed from all these contributions. Simply multiplying them probably won't do, because you will have a hard time ever reaching alpha=0. You may however, interpret (255-alpha) as a measure of transparency, add all contributing transparencies and then convert back into alpha. Something like max (0, 255 - (255-a1) + (255-a2) ...).
It will be difficult to do this in-place, i.e. with just ony copy of the image. You might need an intermediate "image", where each pixel is associated with all transparency contributions from the pixels around it.

Algorithm for "neon glow" graphics programming

I am searching for an article or tutorial that explains how one can draw primitive shapes (mainly simple lines) with a (neon) glow effect on them in the graphical output of a computer program. I do not want to do some sophisticated stuff like for example in modern first pirson shooters or alike. I am more in a search for a simple solution, like the lines in that picture: http://tjl.co/blog/wp-content/uploads/2009/05/NeonStripes.jpg -- but of course drawn by a computer program in my case.
The whole thing should run on a modern smart phone, so the hardware is a bit limited.
I do know a bit about OpenGL, but not too much, so unfortunately I am a bit lost here. Did some research on Google ("glow effect algoritm" and similar), but found either highly complex stuff for 3D games, or tutorials for Photoshop & co.
So what I would really need is an in-depth article on that subject, but not on a very advanced level. I hope thats even possible... I have just started with OpenGL, did some minor graphics programming in the past, but I am a long-year programmer now, so I would understand technical papers in general.
Does anyone of you know of such an article/paper/tutorial/anything?
Thanks in advance for all good advices!
Cheers!
Matthias
Its jus a bunch of lines with different brightness/transperency. Basically, if you want a glow effect for 1px line, in a size of 20 pixels, then you draw 41 lines with width of 1 px. The middle line is with your base colour, other lines get colours that gradiently go from base color to 100% transperency (like in your example) or darkest colour variant (if you have black background, no transparency).
That is it. :)
This isn't something I've ever done, but looking at your example, the basic approach I'd use to try and recreate it would be...
Start with an algorithm for drawing a filled shape large enough to include the original shape and the glow. For example, a rectangle becomes a slightly larger rectangle, but with rounded corners. An infinitessimally-wide line becomes a thickened line with semi-circular caps. Subtract out the original shape (and fill the pixels for that normally).
For each pixel in the glow, the colour depends on the shortest distance to any part of the original shape. This normally reduces to the distance to the nearest point on a line (e.g. one edge of a rectangle).
The distance is translated to a colour value using probably Hue-Saturation-Value or a similar colour scheme, as well as reducing alpha (increasing transparency). For neon glows, you probably want constant hue, decreasing brightness, maybe increasing saturation, and decreasing alpha.
Translate the HSV/whatever colour value to RGB for output. See this question.
EDIT - I should probably have said HSL rather than HSV - in HSL, if L is at it's maximum value, the resulting colour is always white. For HSV, that's only true if saturation is also at zero. See http://en.wikipedia.org/wiki/HSL_and_HSV
The real trick is that even on a phone these days, I'd guess you probably should use hardware (shaders) for this - sorry, I don't know how that's done.
The "painters algorithm" overlaying of gradually smaller shapes that others have described here is also a possibility, but (1) possibly slower, depending on implementation issues, and (2) you may need to draw to an off-screen buffer, with some special handling for the alpha channel, then blit back to the screen to handle the transparency correctly - if you need transparency, that is.
EDIT - Silly me. An alternative approach is to apply a blur to your original shape (in greyscale), but instead of writing out the blurred pixels directly, apply the colour-transformation to each blurred pixel value.
A blur is basically a weighted moving average. Technically, a finite impulse response filter is implemented using a convolution, but the maths for that is a tad awkward and if you just want "a blur" of about the right size, draw a grayscale circle of pixels as your "weights" image.
The blur in this case basically replaces the distance-from-shape calculation.
_____________________
| |
----|---------------------|-----> line
|_____________________|
gradient block
Break up your line into small non-overlapping blocks. Use whatever graphics primitive you have to draw a tilted rectangular gradient: the center is at 100% and the outer edge is at 0%.
Don't draw it on the image yet; you want to blend it with the image. Using regular transparency will just make it look like a random pipe or pole or something (unless you draw a white line, and your background is dark).
Here are two choices of blending mode:
color dodge: [blended pixel value] = (1-[overlay's pixel value]) / [bottom pixel value]
linear dodge: [blended pixel value] = max([overlay's pixel value]+[bottom pixel value], 1)
Then draw the line above the glow.
If you want to draw a curved "neon" line, simply draw it as a sequence of superimposed "neon dots" where each "neon dot" is a small circular image with transparency going from 0% at the origin to 100% at the edge of the circle.

How do I add an outline to a 2d concave polygon?

I'm successfully drawing the convex polys which make up the following white concave shape.
The orange color is my attempt to add a uniform outline around the white shape. As you can see it's not so uniform. On some edges the orange doesn't show at all.
Evidently using...
glScalef(1.1, 1.1, 0.0);
... to draw a slightly larger orange shape before I drew the white shape wasn't the way to go.
I just have a nagging feeling I'm missing a more simple way to do this.
Note that the white part is going to be mapped with a texture which has areas of transparency, so the orange part needs to be behind the white shapes too, not just surrounding them.
Also, I'm using a parallel projection matrix, that's why glScalef's z is set to 0.0 - reminds me there is no perspective scaling.
Any ideas? Thanks!
Nope, you wont be going anywhere with glScale in this case. Possible options are
a) construct an extruded polygon from the original one (possibly rounding sharp corners)
b) draw the polygon with GL_LINES and set glLineWidth to your desired outline width (in fact you might want to draw the outline with 2x width first)
The first approach will generate CPU load, the second one might slow down rendering significantly AFAIK.
You can displace your polygon in the 8 directions of the compass.
You can have a look at this link: http://simonschreibt.de/gat/cell-shading/
It's a nice trick, and might do the job
Unfortunately there is no simple way to get an outline of consistent width - you just have to do the maths:
For each edge: calculate the normal, scale to the desired width, and add to the edge vertices to get a line segment on the new expanded edge
Calculate the intersection of the lines through two adjacent segments to find the expanded vertex positions
A distinct answer from those offered to date, posted just for interest; if you're in GLES 2.0 have access to shaders then you could render the source polygon to a framebuffer with a texture bound as the colour renderbuffer, then do a second parse to write to the screen (so you're using the image of the white polygon as the input texture and running a post-processing pixel shader to every pixel on the screen) with a shader that obeys the following logic for an outline of thickness q:
if the input is white then output a white pixel
if the input pixel is black then sample every pixel within a radius of q from the current pixel; if any one of them is white then output an orange pixel, otherwise output a black pixel
In practise you'd spend an awful lot on texture sampling and probably turn that into the bottleneck. And they'd be mostly dependent reads, which are bad for the pipeline on lots of GPUs — including the PowerVR SGX that powers the overwhelming majority of OpenGL ES 2.0 devices.
EDIT: actually, you could speed this up substantially; if your radius is q then have the hardware generate mip maps for your framebuffer object, take the first one for which the output pixels are at least q by q in the source image. You've then essentially got a set of bins that'll be pure black if there were no bits of the polygon in that region and pure white if that area was entirely internal to the polygon. For each output fragment that you're considering might be on the border you can quite possibly just straight to a conclusion of definitely in or definitely out and beyond the border based on four samples of the mipmap.

Resources