I've a problem with 16-bit targa file. I'm opening image and it colors are weird. It's problem with red and green channels – blue is fine.
How I can repair targa image, to look like on example? (first is original image, second is as must look.)
Image
EDIT: this answer gives results that look right, but won't be bit-identical to the original. Something weirder is going on.
You can make your corrupted image out of your original image by subjecting the red and green channels to a function that doubles the value, subject to wraparound:
F(r) = (r*2) % 255
F(g) = (g*2) % 255
These functions are not invertible, because more than one input value can map to the same output value. In particular,
F^-1(r) = {r / 2, r / 2 + 128}
But we can still try to recover the image if we're willing to tolerate some errors. We'll try to guess whether red (or green) should be high; if so, add 128.
Two pieces of information can guide our guess:
Is the blue channel high? Unless images contain strongly blue pixels, this is a hint that the red and green channels should be high, too.
Is there an adjacent pixel whose red (or green) channel is high, while this pixel's red channel is very low (say, less than 64)? This may suggest that both pixels are relatively bright, but this pixel's red channel got wrapped.
I recovered something very close to your original image using just the blue channel information to decide between r / 2 and r / 2 + 128, though it would probably be better using the neighboring pixels' red and green channels as well.
As a side note, one way this sort of problem could occur is if there were originally (say) 6 bits of red information, but only the least significant 5 bits were retained when the file was written. It would be useful to look at how these images were acquired to make sure that you're not chopping off the most significant bits of your R and G channels somehow.
Related
I'm going to draw a bunch of areas on the map. Each area should be of different color, preferably clearly different from the others. Of course, as the number of areas grows, I'm ducked. But until then I wonder where I could find or how I could generate a set of, say 15 colors that are "vastly" apart.
The first few were easy, because I used the defaults from Bootstrap:
blue
yellow
red
green
pink
But I can't stop wondering if there's a trick, tool or algorithm for generating a next, deviating color.
Googling gave me ways to generate similar schemas or colors that go well together. That's not what I'm looking for, though. Also, I discovered that there's much, muuuuch more to colors than mixing RGB, so I feel like a total looser noob.
You should indeed use the hsl 360 degrees to generate a well spread color spectrum. To have something like that in typescript/js you could do:
const length = 15;
const colors = Array.from({ length }, (_, i) => `hsl(${360 / length * i}, 50%, 50%)`);
Here you can see a working example:
stack
Result:
I did an thinning operation on vessels, and now I'm trying to reconstruct it.
How to expand them to normal vessels in ITK when I have a skeleton line and radius values for each pixel?
DISCLAIMER: This could be slow, but since no other answer has been suggested, here you go.
Since your question does not indicate this, I'm assuming that you're talking about a 2D image, but the following approach can be extended for 3D too. This is how I'd go about it:
Create a blank image with zero filled pixel values
Create multiple instances of disk/sphere ShapedNeighborhoodIterator each having a different radius on the blank image (choose the most common radii from the vessel width histogram).
Visit each pixel in the binary skeleton image. When you come upon a white (vessel skeleton) pixel, recollect the vessel radius at that pixel.
If you already have a ShapedNeighborhoodIterator for that radius value, take the iterator to the pixel location in the blank image and fill up a disk/sphere of white pixels centered about that pixel. If you don't have a ShapedNeighborhoodIterator for that radius value, create one and do the same operation.
Once you finish iterating over the skeletonized image, you will have a reconstructed tree in the other image. Note that step 2 is optional, but will help you achieve faster computation.
I'd like to generate random, non-white colors using hue, saturation and brightness that can be used as a background for black text. I've created a generator for random colors, but when I scan the numbers, I can't see any clear pattern in the colors that look too dark.
How can I generate random non-white background colors for black text? Feel free to answer with code or pseudo-code, but it's definitely not necessary.
Only generate colors with high brightness values - for example, if you were using the range 0-255 for each of {H,S,V}, you'd generate H in [0,255], S in [0,255], V in [168,255]. That should give you colors that are bright enough. You may want to restrict saturation as well (e.g. S in [0,192]), since black on full-bright, full-saturation colors may not be very readable.
You'll probably need to play with the values to get ranges that will give you usable colors.
I don't know color theory, but you can try randomly generating the 3 components in RGB (xxx,xxx,xxx) in a way that the sum of the components is greater than X (X depends of the contrast you want), and the convert to HSB.
HSV and HSL are broken models than have nothing to do with perception. You should generate your colors in YUV space (also known as YCbCr) where the intensity (Y) channel is not simply R+G+B but rather models the perceptual intensities of red, green, and blue. A good starting point would be to try random Y values at least 200 and random U and V values between -50 and 50.
I'm drawing rectangles at random positions on the stage, and I don't want them to overlap.
So for each rectangle, I need to find a blank area to place it.
I've thought about trying a random position, verify if it is free with
private function containsRect(r:Rectangle):Boolean {
var free:Boolean = true;
for (var i:int = 0; i < numChildren; i++)
free &&= getChildAt(i).getBounds(this).containsRect(r);
return free;
}
and in case it returns false, to try with another random position.
The problem is that if there is no free space, I'll be stuck trying random positions forever.
There is an elegant solution to this?
Let S be the area of the stage. Let A be the area of the smallest rectangle we want to draw. Let N = S/A
One possible deterministic approach:
When you draw a rectangle on an empty stage, this divides the stage into at most 4 regions that can fit your next rectangle. When you draw your next rectangle, one or two regions are split into at most 4 sub-regions (each) that can fit a rectangle, etc. You will never create more than N regions, where S is the area of your stage, and A is the area of your smallest rectangle. Keep a list of regions (unsorted is fine), each represented by its four corner points, and each labeled with its area, and use weighted-by-area reservoir sampling with a reservoir size of 1 to select a region with probability proportional to its area in at most one pass through the list. Then place a rectangle at a random location in that region. (Select a random point from bottom left portion of the region that allows you to draw a rectangle with that point as its bottom left corner without hitting the top or right wall.)
If you are not starting from a blank stage then just build your list of available regions in O(N) (by re-drawing all the existing rectangles on a blank stage in any order, for example) before searching for your first point to draw a new rectangle.
Note: You can change your reservoir size to k to select the next k rectangles all in one step.
Note 2: You could alternatively store available regions in a tree with each edge weight equaling the sum of areas of the regions in the sub-tree over the area of the stage. Then to select a region in O(logN) we recursively select the root with probability area of root region / S, or each subtree with probability edge weight / S. Updating weights when re-balancing the tree will be annoying, though.
Runtime: O(N)
Space: O(N)
One possible randomized approach:
Select a point at random on the stage. If you can draw one or more rectangles that contain the point (not just one that has the point as its bottom left corner), then return a randomly positioned rectangle that contains the point. It is possible to position the rectangle without bias with some subtleties, but I will leave this to you.
At worst there is one space exactly big enough for our rectangle and the rest of the stage is filled. So this approach succeeds with probability > 1/N, or fails with probability < 1-1/N. Repeat N times. We now fail with probability < (1-1/N)^N < 1/e. By fail we mean that there is a space for our rectangle, but we did not find it. By succeed we mean we found a space if one existed. To achieve a reasonable probability of success we repeat either Nlog(N) times for 1/N probability of failure, or N² times for 1/e^N probability of failure.
Summary: Try random points until we find a space, stopping after NlogN (or N²) tries, in which case we can be confident that no space exists.
Runtime: O(NlogN) for high probability of success, O(N²) for very high probability of success
Space: O(1)
You can simplify things with a transformation. If you're looking for a valid place to put your LxH rectangle, you can instead grow all of the previous rectangles L units to the right, and H units down, and then search for a single point that doesn't intersect any of those. This point will be the lower-right corner of a valid place to put your new rectangle.
Next apply a scan-line sweep algorithm to find areas not covered by any rectangle. If you want a uniform distribution, you should choose a random y-coordinate (assuming you sweep down) weighted by free area distribution. Then choose a random x-coordinate uniformly from the open segments in the scan line you've selected.
I'm not sure how elegant this would be, but you could set up a maximum number of attempts. Maybe 100?
Sure you might still have some space available, but you could trigger the "finish" event anyway. It would be like when tween libraries snap an object to the destination point just because it's "close enough".
HTH
One possible check you could make to determine if there was enough space, would be to check how much area the current set of rectangels are taking up. If the amount of area left over is less than the area of the new rectangle then you can immediately give up and bail out. I don't know what information you have available to you, or whether the rectangles are being laid down in a regular pattern but if so you may be able to vary the check to see if there is obviously not enough space available.
This may not be the most appropriate method for you, but it was the first thing that popped into my head!
Assuming you define the dimensions of the rectangle before trying to draw it, I think something like this might work:
Establish a grid of possible centre points across the stage for the candidate rectangle. So for a 6x4 rectangle your first point would be at (3, 2), then (3 + 6 * x, 2 + 4 * y). If you can draw a rectangle between the four adjacent points then a possible space exists.
for (x = 0, x < stage.size / rect.width - 1, x++)
for (y = 0, y < stage.size / rect.height - 1, y++)
if can_draw_rectangle_at([x,y], [x+rect.width, y+rect.height])
return true;
This doesn't tell you where you can draw it (although it should be possible to build a list of the possible drawing areas), just that you can.
I think that the only efficient way to do this with what you have is to maintain a 2D boolean array of open locations. Have the array of sufficient size such that the drawing positions still appear random.
When you draw a new rectangle, zero out the corresponding rectangular piece of the array. Then checking for a free area is constant^H^H^H^H^H^H^H time. Oops, that means a lookup is O(nm) time, where n is the length, m is the width. There must be a range based solution, argh.
Edit2: Apparently the answer is here but in my opinion this might be a bit much to implement on Actionscript, especially if you are not keen on the geometry.
Here's the algorithm I'd use
Put down N number of random points, where N is the number of rectangles you want
iteratively increase the dimensions of rectangles created at each point N until they touch another rectangle.
You can constrain the way that the initial points are put down if you want to have a minimum allowable rectangle size.
If you want all the space covered with rectangles, you can then incrementally add random points to the remaining "free" space until there is no area left uncovered.
Is there an intuitive way, or a good mnemonic, for understanding the correspondence between colors and their hexadecimal values?
You just have to remember that the scale is 00 (no color effect) through FF (full color effect) and the three parts of the triplet are red, green and blue.
000000 is black (i.e., no color) and FFFFFF is white (mixing all three primary colors).
The hard bit is remembering the mixtures, which I use the following mnemonics for:
Really good yams: Red + Green = Yellow (potatoes are my favorite food).
Really bad prunes: Red + Blue = Purple (I really hate prunes).
Good/bad apples: Green + Blue = Aqua (I'm indifferent about apples).
Obviously, you may have to come up with your own mnemonics if you food tastes differ from mine. But I find that's the easiest way for me.
Then it's just a matter of varying the quantities to add a little more red or little less blue and so on. I generally only use values of 00, 40, 80, C0 and FF since that gives you a 125-color palette to choose from and I don't want an abundance of choices to slow me down.
You need to get your head around three distinct things here.
Reading and writing numbers in hexadecimal. This just takes practice and familiarizing yourself with it. Color codes range from 00 (zero) through FF (= 255), so spend a little time (using calc.exe in Scientific mode, maybe?) picking a number and trying to guess what it'll be in hex, and vice versa. Counting in decimal is probably second nature to you; counting in hex is the same concept, with different symbols and rules.
How to read an RGB colour code as three component values. RGB codes are either written as three digits (#FFF) or six (#FFFFFF). In the first case, each digit is a single colour component; red, green, then blue. 0 = empty, F = 'full' (maximum). The second is the same but the additional digit gives you a much wider range of tones because you have 256 possible intensities for each component instead of 16.
Knowing the RGB colour model. Your primary colours are red, green and blue. Your secondary colours are yellow (R+G), magenta (R+B) and cyan (G+B). Increasing all the values makes the colour lighter; decreasing them all makes it darker. Pure greyscales will have three equal components - e.g. #ddd, #222. Tint greys by nudging one of the colour values up a bit - #866 will give you a dark grey with a hint of red. This bit you just have to learn through experience. Play with it. Get a tool like Instant Eyedropper and use it to find the hex values of known colours.
Getting it exactly right is very, very hard, but it won't take more than a couple of days before you can say "right... I want orange, which is somewhere between red (#F00) and yellow (#FF0) on the colour wheel, so let's try splitting the difference and use #F90... hmm, bit dark, so let's nudge the G and B components up a bit... #FA2... got it!"
Just think of it as mixing paint.
There are the three components RR, GG, BB, (RRGGBB) which can be mixed with different strengths. Remember that 00 is lowest strength, and ff is highest.
For example, we know that 000000 will be low strength on all colors and will be black. Likewise, ffffff is highest on all three color components, and will be white.
You can add or remove color by making one part stronger or weaker. For example, start with black 000000, and add a little red as 330000, or add a little blue with 000033. Add both and see what happens 330033.
Add a LOT of red, with ff0000. Or add a medium amount of green for 006600.
If all three components are close to the same strength, they cancel each other out and you have grayscale, which will be light or dark depending on the strength of the colors. So 000000, 333333, 666666, 999999, and ffffff are black, dark greys, ligher grays, and white.
If you take a medium gray, and add a little blue, you end up with a bluish grey, like 888899. Red, green, and blue almost equal, so almost grey, but with a bit more blue. You can experiment with them a bit.
One really great tool for viewing color codes is this (small and free):
http://www.nattyware.com/pixie.html
Hope that helps!
I'm not sure what you mean, but I always remember that it is an RGB colour: The first two digits are for red, the second pair is for green and the las ones are for blue.
like this: #RRGGBB
And of course trying to remember that lower numbers give darker coulours.
If you are looking for some way to remember the colour code for orange, I'm afraid you are out of luck. I always need to look that one up...
Hexadecimal colours are of the form #RRGGBB where RR is red, GG is green and BB is blue. Since they're hexadecimal they range from 00-FF. It should be fairly easy from that to gauge roughly what kind of colour you're dealing with for example #FF12A3 is going to be fairly red.
Break the colour up into a triplet of values that represent red, green and blue. The higher the hex value in each segment then the more of that colour there is. So #000000 is black, #FF0000 is all red, #FF00FF is purple etc.
I think it's useful to get a good HTML color chart and keep it by your desk. I like the Visibone mouse pads and posters.
Remember:
1) The order of the hex-pairs are Red, Green and Blue. This is easy to remember since we aften talk about RGB-color, RGB screens and so on.
2) The numbers should interpreted as the amount of light in the color. So 00 is no light, while FF is maximum light in that color.
3) I all three colors are of the same level, they "cancel each other out", so we get a greyscale color. 00 is minimum light, så if all three colors are 00 we get no light - ie. black. If all three colors are max-light (#FFFFFF) we get white, Any number in between is grey.
Well, monitors use an 'RGB' colorscheme. The order of the colors in the hexadecimal number is RRGGBB. So just remembering the name of the colorscheme tells you what goes where. Then you can parse the #RRGGBB as three numbers RR, GG and BB.
The higher the number the brighter that particular component.
Then you just need to remember the additive color wheel from grade school. ;)
I manage to create colors intuitively now (with some trial and error, of course), having practiced them a lot. Playing/using color mixers (with red/green/blue sliders for example) helps in understanding relationships. Now, I know that FFFF00 is yellow, FF00FF is magenta, 00FFFF is cyan, plus of course primary colors, and all others colors are in between.