How bright is part of UIImage? - ios

Have a label over part of an UIImageView. If image is too bright white text can not read. Any easy way to detect how bright portion of the image?

There is no "easy" way of doing it at least for processor. For developer it is easiest to get access to raw RGBA buffer of image data and find out what is the average color. Then convert that color to HSV and check saturation to determine the brightness. You can even use GPU to make things a bit quicker; openGL should be perfect for that.
But before you get too far: The result will most likely not be what you are looking for. There are always cases that will make your text unreadable no matter what color they are. Consider you have a white text and will convert it to black once image is too bright. But then the image consists purely of black and white stripes so that every odd letter is over white stripe and the rest are on black. The text is simply unreadable.
I suggest you try with stroke, dropping shadows or adding a background. You can for instance have white text on a label and use semitransparent black background color with some layer corner radius. The background will barely be visible on all but the brightest images and text will always be readable.

Related

Is it possible to use imagemagick-convert's histogram visualisation option without counting transparent pixels?

Apologies if this is a duplicate. I've been Googling this for a day.
Goal
I have a cartoon-like image of a character on a transparent background (mix of transparent black and white pixels). (Think something like a Pikachu or a logo - a solid, non-square shape on a blank background.) The only data I'm interested in analysing are the colour distributions of the pixels within this shape. I want to do this for multiple reasons, including assessing the palette of the character. I would prefer to use ImageMagick's generated histograms alongside it right now, rather than manipulating the text output.
Issue
Using convert image.png -alpha off histogram:histogram.gif or similar results in a histogram where the RGB channels are very short due to huge spikes on the left and right. Example ImageMagick can, say, replace the transparent pixels with a given opaque colour, but that predictably replicates the issue in another channel. This is the result of filling the transparent pixels with #008000. It seems to me this is because the image is at least 50% black/white pixels with opacity 0, way more than any other single colour.
Alternatives Tried
Quantising does not produce a remotely desired result, because the averaged colours are so much blander than the ones used in the picture.
I know I can take the metadata output of the histogram and exclude #FFFFFF, #000000, and so on.
However, I would ideally be able to use ImageMagick's inbuilt visualisation simply because it would make my life a lot easier at this stage and I would not have to build my own visualisations. So what I want is the visualisation without it having counted transparent (or specified colour) pixels towards the number of pixels in the image.
Is this possible within ImageMagick? If not, I'll just manipulate the text histogram.

How to detect transparent area in image?

i research about merge many image into a image in iphone. But i have some problem about that. I want to detect transparent areas, which has a white background. I think it's possible to get a CGRect rectangle around the area during this and after i will drag my image into transparent area, but I do not know how I can identify it. So if i detected all transparent area in this image, i will have a CGRect Array.
You can see my image:
Please help me, thank you very much!!
In terms of detecting transparent pixels, you can access the pixel buffer as described in Technical Q&A QA1509 and then iterate through the pixel buffer looking for pixels with an alpha channel value of less than 1.0.
But to extrapolate from that to the programmatic building an array of CGRect corresponding to contiguous transparent pixels is non-trivial. If you make simplifying assumptions about the nature of the transparent regions (e.g. circular), it's quite a tractable little problem, though your thin rounded rectangle that intersects many of the circles complicates the problem.
If your image with transparent areas is predefined, though, I'd probably just define them manually rather than determining it programmatically.

Image shape detection in JavaScript

I'm looking to write a script to look over a series of images that are essentially white canvas with a few black rectangles on them.
My question is this: what's the best modus operandi that would identify each of the black rectangles in turn.
Obviously I'd scan the image pixel by pixel and work out if it's colour was black or white. So far so good, Identifying and isolating each rectangle - now that's the tricky part :) A pointer in the right direction would be a great help, thank you.

Cleaning scanned grayscale images with ImageMagick

I have a lots of scans of text pages (black text on white background).
My usual approach is to clean those in Gimp using the Curves dialog using a pretty simple curve with only four points: 0,0 - 63,0 - 224,255, 255,255
This makes all the greyish text pitch black plus makes the text sharper and turns most of the whitish pixels pure white.
How can I achieve the same effect in a script using ImageMagick or some other Linux tool that runs completely from the command line?
-normalize or -contrast-stretch don't work because they operate with pixel counts. I need an operator which can make the colors 0-63 (grayscale) pitch black, everything above 224 pure white and the rest should be normalized.
The Color Modifications page shows many color manipulation algorithms by ImageMagick.
In this specific case, two algorithms are interesting:
-level
-sigmoidal-contrast
-level gives you perfect black/white pixels near the ends of the curve and a linear distribution between.
The sigmoidal option creates a smoother curve between the extremes, which works better for color photos.
To get a similar result like in GIMP, you can try to apply one after the other (to make text and black areas really black).
In all cases, you will want to run -normalize first (or even -contrast-stretch to merge most of the noise) to make sure no black/white levels are wasted. Without this, the darkest color could be lighter than rgb(0,0,0) and the brightest color could be below pure white.
[magick-users] Curves in ImageMagick
The first link in that archived message is a shell script that I think does what you're looking for.

Adding additional anti-alias - to_do

How do I anti-alias text properly? Here is the image ...normal text saved to a .png is jaggedy..even if you select anti-alias when creating it. I don't know how they create this smoothness
The "canonical" form of anti-aliasing is done by rasterizing at higher resolution than your target (e.g., at double the resolution in each dimension).
You then (in a virtual sense) overlay a grid on that at the target resolution. You count up the number of cells that have been filled, and pick your color based on that -- if all the cells are filled, you use the background color. If you none of the cells is filled, you use the background color. The crucial part: if some of the cells are filled, you pick a color in between the two, based on what percentage of those cells are filled. e.g., at 2x in each direction, with a black foreground and white background, you could pick black (all cells filled), white (no cells filled) or any of three intermediate shades of grey (1, 2, or 3 cells filled).
Those intermediate levels of grey (or whatever color then end up being, based on the foreground and background colors) are perceived as filling in the "jagged" edges. For example, a pixel that's 75% of the way from white to black will be perceived almost like a smaller pixel that's entirely black.
There are, of course, other anti-aliasing methods, mostly aiming to reduce the computational overhead of rasterizing at higher resolution. They're highly relevant for things like animation, but for producing a PNG file, this method is relatively simple and probably plenty fast.

Resources