I was looking for a way to change the default colors of the different categories in d3.js.
I found where the colors are laid out in the main d3.js. They look like this for one category:
var ml = [2062260, 16744206, 2924588, 14034728, 9725885, 9197131, 14907330, 8355711, 12369186, 1556175].map(yt)
I've tried replacing these values with everything from Hex codes to HSL to RGB and it never yields the expected colors.
Any ideas how I can generate the proper numbers for whatever colors I want?
Thanks.
First, just FYI, to see the RGB (i.e. hex) value that corresponds to these numbers:
(2062260).toString(16); // 16 for hex, aka base 16
> "1f77b4"
Next, given an RGB (again, hex) that you want to convert to number:
parseInt("1f77b4", 16); // 16 for hex
> 2062260
And that would be the number you want to use.
The colors you got from the d3 source are used to construct what you get from d3.scale.category10(). You can get the same thing but with your own colors — and without modifying d3's source code — by constructing a d3.scale.ordinal:
var myCategory3 = d3.scale.ordinal()
.domain(["red", "#1f77b4", "rgb(128, 255, 128)"]);// All kinds of colors are possible
myCategory3("X");// "red"
myCategory3("blabla");// "#1f77b4"
myCategory3("X");// "red"
myCategory3(123456);// "rgb(128, 255, 128)"
Related
I have an image: to which I have performed segmentation to receive a binary image. I'd like to label each of the objects in the image with different colours. I have the following code so far:
img = imread('lab5a.tif');
BW = imbinarize(img,graythresh(img));
figure; imshowpair(img,BW,'montage')
title ('Opening Operation on Image');
se = strel ('disk', 3);
rem = imclose(BW,se);
figure; imshow (rem, []);
title ('Removed Undesired Features');
CC = bwconncomp(rem);
L = labelmatrix(CC);
RGB = label2rgb(L, spring, 'c', 'shuffle');
figure; imshow(RGB, []);
The output is this image: which is not what I want. It colours the background and the objects are white. I would just like the objects to be of different colours.
Any form of help would be much appreciated!
In your example, the background and foreground of the image are reversed from what you think it should be. The default for the matlab commands is to assume that the higher-value pixels (white) are the foreground or items of interest, while the lower-value pixels (black) are the background. So when you run your example code, the object CC contains only 1 object (the "background" that is blue in your image):
CC =
struct with fields:
Connectivity: 8
ImageSize: [256 256]
NumObjects: 1
PixelIdxList: {[43341×1 double]}
Any easy way to fix this is simply to invert your cleaned up image with the imcomplement command. Add this line to your code:
% invert the image so that the background is black
rem = imcomplement(rem);
Now the CC struct contains 62 objects identified:
CC =
struct with fields:
Connectivity: 8
ImageSize: [256 256]
NumObjects: 62
PixelIdxList: {1×62 cell}
And you will get this image:
If you want to change the colors used for the items, look at the colormap property of the label2rgb command.
I am trying to average the colours in an image, I have come up with the following script (from here):
scrip.rb:
require 'rmagick'
file = "./img.jpg"
img = Magick::Image.read(file).first
color = img.scale(1, 1).pixel_color(0,0)
p [color.red, color.green, color.blue]
img.jpg:
BUT the RGB output is [31829, 30571, 27931].
Questions:
Shouldn't the numbers be in the range of [0-255]?
What am I doing wrong?
What you have there is the red, green, and blue histogram values.
You need to divide by 256 in order to get each RGB value. In this case, the RGB values are:
require 'rmagick'
file = "./img.jpg"
img = Magick::Image.read(file).first
color = img.scale(1, 1).pixel_color(0,0)
p [color.red/256, color.green/256, color.blue/256]
# => [124, 119, 109]
This blog post provides a more thorough explanation on how to analyse images with RMagick.
The reason for the odd output is due to the bit depth. As a previous answer states, "They are stored in a 'quantum depth' of 16-bits." This was an initial assumption, but by looking at previous answers, this makes more sense. In order to properly convert these numbers back to your typically desires [0-255] range, you must divide the values by 256.
Note: You can change the quantum depth during runtime. When reading the file, you should be able to use a block like shown in the following code.
img = Magick::Image.read(file){self.depth = 8}.first
I have with 5 different values and I would like to create a legend ?
These are continuous data, I need small coloured squares !
How to add legend in imagesc plot in matlab Something like this but with squares, I tried replacing "line" by "rectangle" but that's not the trick apparently !
Thank you
I just used your linked example code and modified it a little:
N=4; % # of data types, hence legend entries
Data = randi(N,30,30); % generate fake data
imagesc(Data) % image it
cmap = jet(N); % assigen colormap
colormap(cmap)
hold on
markerColor = mat2cell(cmap,ones(1,N),3);
L = plot(ones(N), 'LineStyle','none','marker','s','visible','off');
set(L,{'MarkerFaceColor'},markerColor,{'MarkerEdgeColor'},markerColor);
legend('A','B','C','D')
The trick is to use markers instead of the line itself.
it returns:
I am modifying images in matlab and I have a problem.
I need to separate the 3 channels of color and modify them separately.
I use this to obtain the three channels:
a = imread('./images/penguins.png');
colorlist = {'R','G','B'};
subplot(2,2,1);
imshow(a);
for k=1:3
subplot(2,2,k+1);
imshow( a(:,:,k));
title(colorlist{k});
end
a(:,:,k) is one color of the three. The problem is when I add the three vectors in one, to obtain the color image. I do this:
A=a(:,:,1)+a(:,:,2)+a(:,:,3)
figure; imshow(A);
But it dont works, it only show me a very highlight image, no a color image.
Anyone knows how can I recover the color image? Thanks for yout help^^
You are adding the values of the three layers instead of concatenating them in a 3D array.
Try this:
A= cat(3, a(:,:,1), a(:,:,2), a(:,:,3));
I should also note that you can edit the layers simply by indexing, say you want to switch the red and green components:
I1 = imread('http://i.stack.imgur.com/1KyJA.jpg');
I2=I1;
I2(:,:,1)=I1(:,:,2);
I2(:,:,2)=I1(:,:,1);
imshowpair(I1,I2, 'montage');
Now if I take your title literally, let's say you do want to add the three layers and display the result with a colormap, you can do:
A=a(:,:,1)+a(:,:,2)+a(:,:,3)
imagesc(A); axis image;
colorbar;
Results:
Can someone provide some insight on how scales and extents work together in cubism.js
.call(context.horizon()
.extent([-100, 100])
.scale(d3.scale.linear().domain([-10,10]).range([-100,100])
)
);
For example what does the code above do? If the values are generated using a random number generator (numbers between -10 and 10)
I know extent is used to set the maximum and minimum.
I know how to define a scale, example:
var scale = d3.scale.threshold().domain([100]).range([0,100])
console.log(scale(1)) // returns 0
console.log(scale(99.9)) // returns 0
console.log(scale(88.9)) // returns 0
console.log(scale(100)) // returns 100
I read about d3.scales here http://alignedleft.com/tutorials/d3/scales/
My main issue is that I want to define thresholds for my data, very simple
0-98 Red
98-100 Pink
100 Blue
Or maybe just
0-99.99 Red
100 Blue
But I'm not being able to use all what I've read to construct something that works.
I'm guessing that you just want to use a different color to represent anomalies in your data. If that is true, you don't need to create a domain and range.
You can just create a custom color palette like this:
var custom_colors = ['#ef3b2c', '#084594', '#2171b5', '#4292c6', '#6baed6', '#9ecae1', '#c6dbef', '#deebf7', '#f7fbff', '#f7fcf5', '#e5f5e0', '#c7e9c0', '#a1d99b', '#74c476', '#41ab5d', '#238b45', '#006d2c', '#00441b'];
This color palette was constructed using the palette on this page with an extra red color tacked on to the end.
Then just call the custom colors like this:
d3.select("#testdiv")
.selectAll(".horizon")
...
.call(context.horizon()
.colors(custom_colors)
));
Play around with the colors until you find a combination that you like. In this above example, only the outlier will be in red while the rest will follow the blue and green pattern.
Hope this helps!