How to replace the appropriate colors with my own pallette in MATLAB? - matlab

I am using MATLAB 2015. I want to reduce the image color count. An RGB image will be segmentated using k-means algorithm. Then mean colors will be replaced with the colors I have.
The colors are (10),
black - [255, 255, 255],
yellow - [255, 255, 0],
orange - [255, 128, 0],
white - [255, 255, 255],
pink - [255, 153, 255],
lavender - [120, 102, 255],
brown - [153, 51, 0],
green - [0, 255, 0],
blue - [0, 0, 255],
red - [255, 0, 0].
I have succeeded clustering the image. Clustered images should be replaced with the nearest color. How can I change those colors after clustering?

In case you don't succeed in finding a way with MATLAB, you can remap the colours in an image at the command line with ImageMagick which is installed on most Linux distros and is available for OSX and Windows too.
First, you would make a swatch of the colours in your palette. You only need do this once obviously:
convert xc:black xc:yellow xc:"rgb(255,128,0)" \
xc:white xc:"rgb(255,153,255)" xc:"rgb(120,102,255)" \
xc:"rgb(153,51,0)" xc:lime xc:blue xc:red \
+append colormap.png
That looks like this (enlarged):
Now, let's assume you have an image like this colorwheel (colorwheel.png):
and you want to apply your palette (i.e. remap the colours to those in your swatch):
convert colorwheel.png +dither -remap colormap.png result.png

Related

How to convert a 1 or 4 bit deep PNG into an 8-bit PNG image?

image = Image.open(file) # file is 1-bit jpg
image = image.convert('P') # image become 8-bit
colors = [0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128]
image.putpalette(colors) # why use this code image become 4-bit ?
image.save("1.png",format='PNG') # save image become 4-bit png ????? (-w-)?
i need a 8-bit color png ,can you help me?

Networkx Edge Colormap Based on Attribute

I have a graph where each edge (not nodes) has a capacity and a load. I want to draw this graph using a color map which is a gradient from blue to red, red being the most loaded and blue being the less loaded. I tried to understand the official documentation of Edge Colormap, but it doesn't help me. Can you help me?
The key is to set edge_color to be a sequence of edges coinciding with edgelist, both keyword arguments of nx.draw_networkx. Here is a minimal working example using pandas and networkx.
import networkx as nx
import pandas as pd
edge_df = pd.DataFrame({"source": [0, 1, 2],
"target": [1, 2, 0],
"capacity": [.1, .2, .3],
"load": [40, 20, 10]})
G = nx.from_pandas_edgelist(edge_df,
source="source",
target="target",
edge_attr=["capacity", "load"])
nx.draw_networkx(G,
edgelist=list(zip(edge_df['source'], edge_df['target'])),
edge_color=edge_df['capacity'],
edge_cmap=plt.cm.bwr)
Should get you something like this:

Turn on specific LED using NeoPixel module for Espruino?

I am trying to turn on a specific LED using the NeoPixel module. How it works is really easy: Parse it a 2D array of RGB colors. Here's an example:
require("neopixel").write(NodeMCU.B2, [[255, 0, 0], [0, 255, 0], [0, 0, 255]]);
That will turn on the first three LEDs with red, green, and blue. I want to have a function, where I can do something like:
function single(number, color) {
require("neopixel").write(NodeMCU.B2, number, color);
}
single(0, [255, 0, 0]);
single(1, [0, 255, 0]);
single(2, [0, 0, 255]);
Which would do the exact same as above. Now you might ask: Why would you want that? Well:
I want it to remember the last "configuration" of LEDs, so I can update it over time
If I want to turn off all my 100+ LEDs and just turn on the last few (or the ones in the middle), I wouldn't have to parse the write() function 100+ LEDs, where most of them are black
Is something like that possible, or would I have to do some magic in order to remember the last LED configuration?
Yes, totally - it's worth looking at the neopixel docs at http://www.espruino.com/WS2811 as they suggest you use an array to
store the current state.
Once you have that array - called arr here - you can use the .set method to set the 3 elements at the right position (3x the number, because RGB), and then can resend the whole array.
var arr = new Uint8ClampedArray(NUM_OF_LEDS*3);
function single(number, color) {
arr.set(color, number*3);
require("neopixel").write(NodeMCU.B2, arr);
}

Microsoft charting stacked area still visible even when y-values are all 0

Here is my problem. Let's say I have 2 stacked area series and their data looks like this :
Series A (Color blue) :
X values {1, 2, 3, 4}
Y values {4, 6, 7, 6}
Series B (Color red) :
X values {1, 2, 3, 4}
Y values {0, 0, 0, 0}
If I had these 2 series to a chart (A first and B second), there is still a red line that appears on top of the blue area even if all the y-values of the B series are 0. Is there a way to make sure that the red line doesn't appear in the graph without removing the B-series from the legend?
Format the data series to have a fill only and no border. If the areas have a border, it will show even if the values are 0.

The Gimp - Creating background gradient

I have the below css code for a web gradient on my page, I would like to make a background image that is exact to this gradient using the Gimp. Anyone have expertise doing this that might be able to lend some advice? Thanks
background-image:-webkit-linear-gradient(90deg, rgba(51, 51, 51, 1.00) 0.0% , rgba(26, 26, 26, 1.00) 50.5% , rgba(51, 51, 51, 1.00) 50.7% , rgba(77, 77, 77, 1.00) 100.0% );
GIMP can't parse that directly, althoug GIMP 2.8 ships with a Python script that can output gradients in this CSS syntax
You could make a python-script to parse CSS gradient syntax into GIMP Gradients,
and them use this gradient on an image.
Of course it is overkill if you are needing that just once -
I'd recommend creating a new gradient in GIMP, and manually edit the recorded file
(in ~/.gimp-2.8/gradients folder if you are on *nix, else check for the user gradients folder in the preferences).
GIMP's gradient file is straightforward - a text only file that goes like:
GIMP Gradient
Name: Untitled
2
0.000000 0.243464 0.486928 0.000000 0.000000 0.000000 1.000000 0.000000 1.000000 0.000000 1.000000 0 0 0 0
0.486928 0.743464 1.000000 0.000000 0.000000 1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 0 0 0 0
So this is a single gradient, with two segments - each line has the start-point, endpoint of each segment, the starting ARGB color, ending ARGB color, and ,...don't care, just keep the four zeros at the end: most likely they are used to describe the type of color in each endpoint, and we want 0.
Those rgba colors correspond to the following html-notations :
rgba(51, 51, 51, 1.00) - #333333 (Color A)
rgba(26, 26, 26, 1.00) - #1a1a1a (Color B)
rgba(51, 51, 51, 1.00) - #333333 (Color A)
rgba(77, 77, 77, 1.00) - #4d4d4d (Color C)
You could try creating a rectangular image (with height twice the width). Fill the top square half with a gradient of color A to color B, and the bottom square with a gradient from color A to color C.
Then you set it as your background image with "repeat" property enabled.