The result for certain calculations when drawn using python matshow
.
Is there any way to draw lines
like a border between the various colors like in this figure?
This is a grid n X n that looks like this initially.
Each square has 4 small squares(lattices) within it. i.e in the case of the first square, it has (0,0), (1,0), (0,1) and (1,1) indices occupied by the same number say 0 and the next square has (0,3), (0,3), (1,3) and (1,3) indices occupied by a different number say 1.
This is resulting in a different color. After subjecting it to some mathematical operations I got the first image. Now I want to distinguish between these colors as in figure 2. Is there a way to do this? Any leads would be appreciated!
You could iterate over each cell in the final grid to compare its color with its N, S, E, W neighbors, and draw a line segment corresponding to the border, when the condition is met, and not when the cells share the same color.
I would have provided a small sample code, but you did not explain how your cells and grids are represented; however, this is rather straightforward code that you can probably write.
Related
I have three columns with RGB colors. In another set of columns, I need to convert them to X,Y coordinates per the following examples:
How?
The math is eluding me, mostly because it has to wrap back around to red.
Of note: no VB please, and I'd prefer the map jump from the second red directly to pure grayscale (i.e. no bleeding).
UPDATE: I came up with the following:
X —
=IF(AND(MAX([#R]:[#B])=[#R],MAX([#R]:[#B])<>[#B]),[#R]+[#G]-[#B],
IF(MAX([#R]:[#B])=[#G],512+[#G]+[#B]-[#R],
IF(MAX([#R]:[#B])=[#B],1024+[#B]+[#R]-[#G])))
-(256*(MAX([#R]:[#B])/256))
Y —
=ROUNDUP(MAX(Table1[#[R]:[B]])/2*IF(MIN(Table1[#[R]:[B]])=0,1,1+MIN(Table1[#[R]:[B]])/256),0)
Plus a separate quick calculation to wrap around negative X values. I'll also write a quick check for grayscale exceptions after I resolve colors.
But first, tell me why the above is wrong.
I wanna split an image in cv2 as shown in the picture and get the dominant color for each of the parts of the picture. Is there any function so I can give it parameters (a, b, c, d) for the count of lines to up, right, bottom and down of the picture and it returns color in rgb space?
Usually this is done not by the count of lines (up, down, left, right), but by the angle of one pizza slice. The whole pizza is 360 degrees, so you may choose a number like 10-20-30 degrees, to make slices even. Then take the x, y coordinates of the image pixel and after subtracting the center pixel coordinates calculate the slice number as atan((y - y_center)/(x - x_center)).
To avoid calculation complications, you may want to split the calculation into the two similar, but separate cases when x > x_center and x < x_center to avoid ZeroDivide and make life easier.
Regarding the dominant colors, there are a bunch of articles on this site, just use the search, I don't want to copy-paste someone else's work.
I'm doing a triangle rasteriser and it all works but now I want to use parallel computing to draw a bunch of triangles.
This means that the triangles can be drawn in any random order each frame, the problem is this disordered drawing is causing artifacts.
As a test case, let's consider two triangles that share two vertices, like so:
If we look at a grid of pixels the vertices will look like this:
The filling convention I'm using is just ceil()
So if we draw the blue triangle it will look like this
The problem that now becomes apparent is that if we now draw the yellow triangle, because they share the two vertices, the yellow triangle will be drawn over the blue one:
This isn't a problem on it's own, the problem is IF we draw them in the reverse order (yellow first, blue second) then the blue one will be drawn over instead:
This causes obvious artifacts because in any frame the order can be random so you will see a flickering line as one triangle is drawn over another and vise versa.
Is there any thing that can be done to ensure that the final image will look identical no matter the order the triangles are drawn in?
Always truncate to integer device pixels, and always do it the same way. This could be floor or ceiling or even rounding (if your sure both triangles get numerically identical inputs then they should round the same).
And define the boundaries of the triangle to include the lowest endpoint but exclude the highest, ie., min(x0,x1) <= x < max(x0,x1) and similarly for the y range.
I would like to generate color palette based on the given image containig max. 10 colors. Assume that, the given picture is bot bigger then 800x600 px. I've tried the next algorithm:
Generate 500 random X, Y values.
Check the colors' R,G,B values at the (X,Y) position, put colors into an array.
Find similar colors to each color, count how many similar colors have found. (Similar means: +- 10 difference in R, G, B)
Display colors which have the most similar colors.
The result is not what I expect. Any idea how to get the appropriate colors?
An example, I want something like this
You probably want Median Cut or K-means.
With median cut, you'll generate a point cloud of color samples from your source image. Divide the pointcloud in half at its median across the axis with maximum variance, creating two sub-pointclouds. Recursively divide these until you have the desired number of leaf nodes. You can then generate a palette by averaging the color samples in each leaf node.
With K-means, you select k random color samples from your image. These will be the first color samples in k buckets. Then, for each pixel, add its color value to the bucket whose average color is closest to that of the pixel in question-- you may use euclidean distance to determine "closeness". After all pixels have been sampled, the average colors of the k buckets is your palette.
You will get better results if you first convert your color samples to CIE lab color space, where euclidean distance is a better measure of perceptual distance.
Suppose I have some data to be displayed on a bar chart, and each bar will be coloured according to its height.
Say the bars range in height from a to b.
What is the best way of calculating the numbers in a continuous colour map so that a is assigned a set of RGB values, a+1 is assigned a set of RGB values all the way through to b. The data is continuous.
The colours assigned need to be such that if data point 1 has value x and data point 2 has a value close to x, the colours assigned to data points 1 and 2 will be similar.
Pretty much, depending on what program you're using to do it in, it's fairly straight forward. Think of having 3 columns R G and B (they could be stored multiple different ways).
Pure red is 255 with G and B being 0. Same goes for pure green and blue with their respective colors. Then it's just a matter of covering every possible color you want.
A sample color chart is here
Another option is to use the HSV color system and then convert it to RGB if you really need to have RGB. I personally find working with HSV to be easier and more concise when generating a whole array of usable colors.