이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to convert a vector to rgb colormap?
조회 수: 15 (최근 30일)
이전 댓글 표시
Hello guys
can anybody tell me how to convert a ector to rgb colormap??
I have have vector, which its value goes from -1 to 1. I want the define a colormap, where the value of the 0.2 to 1 is red and -0.2 to -1 is blue and the rest is green.
thanks in advance.
채택된 답변
Image Analyst
2019년 12월 30일
Do this:
% Colormap "where the value of the 0.2 to 1 is red and
% -0.2 to -1 is blue, and the rest is green.
numColors = 256;
vec = linspace(-1, 1, numColors);
cmap = zeros(numColors, 3);
% Make everything green to start with
cmap(:, 2) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.2 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make blue from where vec goes from -1 to -0.2.
blueIndexes = vec >= -1 & vec <= -0.2;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
cmap % Echo to command window so we can see it.
댓글 수: 19
Image Analyst
2019년 12월 31일
Ali's "Answer" moved here as a comment since it's a reply to me rather than an Answer to his original question:
thanks, but how can use it now as colormap?
Image Analyst
2019년 12월 31일
How do you want to use it? Do you have an image displayed? If so, just call colormap(cmap)
colormap(cmap);
colorbar;
Ali Nouri
2020년 1월 1일
편집: Ali Nouri
2020년 1월 1일
many thansk again!
But can you tell what is wrong with my loop?
why does not the color change?
for i=2:22
vec(:,i) = DD_vector{i-1}(:,1);
% Make red from where vec goes from 0.2 to 1.
if vec(:,i) >= 0.05 & vec(:,i) <= 1
% redIndexes = vec >= 0.05 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
elseif vec(:,i) >= -1 & vec(:,i) <= -0.05
% Make blue from where vec goes from -1 to -0.2.
% blueIndexes = vec >= -1 & vec <= -0.05;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
end
% Make everything green to start with
cmap(:, 2) = 1;
end
Image Analyst
2020년 1월 1일
You're just assigning a variable. You never send that variable to the display with the colormap() function. You need to
colormap(cmap);
Ali Nouri
2020년 1월 1일
numColors = 31;
vec = [-0.00653341206474312
0.00324578781459654
0.000552586889809205
0.0449766491863441
0.00356180692563616
0.0746463180472126
0.0078011233071506
-0.00078555536582595
-0.00374634887393066
0.00109938285489642
0.369351654077307
-0.00454936207691688
2.20408232101434e-05
0.000254609487960349
-0.00135086171535819
-0.0695803908468877
0.0116165165761393
-0.00197408951942686
-2.47282982787341e-05
0.375027165631663
-0.047418741927062
-0.840829144608918
-0.00254093170763818
0.0010078132834115
-0.000870537370561834
-0.019269131980187
0.000548413597787709
-0.00772253261710933
0.00372968305677465
0.0221712405360291
0.00246775692879843];
cmap = zeros(numColors, 3);
% Make everything white to start with
cmap(:, :) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.02 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make green from where vec goes from -1 to -0.2.
greenIndexes = vec >= -1 & vec <= -0.02;
cmap(greenIndexes, 1) = 0;
cmap(greenIndexes, 2) = 1;
cmap(greenIndexes, 3) = 0;
cmap; % Echo to command window so we can see it.
%B=sortrows((cmap));
%colormap(B);
colormap(cmap)
colorbar;
caxis([-1 1])
set(get(colorbar,'YLabel'),'String','risk')
As you can see that I use colormap(cmap), but something seems wrong, can you see what is wrong with the way i try to make plot.
Image Analyst
2020년 1월 1일
Can you post that image and the code you used to display it? I don't know what you want. You changed what I did to make values white instead of green. It's doing what you told it to do. Are you sure each country has the value you expect it to?
Ali Nouri
2020년 1월 1일
편집: Image Analyst
2020년 1월 1일
Which code??
Well I have 21 vector, that is why i used for loop to convert every vector to a new rgb colormap!
I didn't success to make code for it.
Image Analyst
2020년 1월 1일
And you didn't attach the image and code to show how you displayed it, even though I directly asked for it. Come on, make it easy for people to help you, not hard.
Ali Nouri
2020년 1월 1일
편집: Ali Nouri
2020년 1월 1일
I sent you the code and picture of the result in my previous message and I ask you again for code an image, and I asked you what code and picture you are looking for? at least try to be a little precise when you are asking someone, who is not so much familiar with matlab.
Ali Nouri
2020년 1월 1일
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
numColors = 31;
%value of 31 country
vec = [-0.00653341206474312
0.00324578781459654
0.000552586889809205
0.0449766491863441
0.00356180692563616
0.0746463180472126
0.0078011233071506
-0.00078555536582595
-0.00374634887393066
0.00109938285489642
0.369351654077307
-0.00454936207691688
2.20408232101434e-05
0.000254609487960349
-0.00135086171535819
-0.0695803908468877
0.0116165165761393
-0.00197408951942686
-2.47282982787341e-05
0.375027165631663
-0.047418741927062
-0.840829144608918
-0.00254093170763818
0.0010078132834115
-0.000870537370561834
-0.019269131980187
0.000548413597787709
-0.00772253261710933
0.00372968305677465
0.0221712405360291
0.00246775692879843];
cmap = zeros(numColors, 3);
% Make everything white to start with
cmap(:,:) = 1;
% Make red from where vec goes from 0.2 to 1.
redIndexes = vec >= 0.2 & vec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
% Make green from where vec goes from -1 to -0.2.
greenIndexes = vec >= -1 & vec <= -0.2;
cmap(greenIndexes, 1) = 0;
cmap(greenIndexes, 2) = 1;
cmap(greenIndexes, 3) = 0;
cmap; % Echo to command window so we can see it.
B=sortrows((cmap),1);
colormap(B);
%colormap(cmap)
colorbar;
caxis([-1 1])
set(get(colorbar,'YLabel'),'String','risk')
% shaperead for country
land = shaperead('cntry02.shp', 'UseGeoCoords', true);
%map over europe
w = worldmap('europe');
setm(gca,'ffacecolor','b') %using this line the seas are colored in blue
%facecolor with new colormap
faceColors = makesymbolspec('Polygon',...
{'INDEX', [1 31], 'FaceColor', ...
colormap});
% geshow with new facecolor
geoshow(land,'DisplayType', 'polygon',....
'SymbolSpec',faceColors)
Walter Roberson
2020년 1월 1일
numColors = 31;
cvec = linspace(-1, 1, numColors);
cmap = ones(numColors, 3); %defaults to white
redIndexes = cvec >= 0.05 & cvec <= 1;
cmap(redIndexes, 1) = 1;
cmap(redIndexes, 2) = 0;
cmap(redIndexes, 3) = 0;
blueIndexes = cvec >= -1 & cvec <= -0.05;
cmap(blueIndexes, 1) = 0;
cmap(blueIndexes, 2) = 0;
cmap(blueIndexes, 3) = 1;
Now display your image, and colormap(cmap)
Your vector of values, vec, has no role in this, unless it has some role in creating the values for the image you are displaying.
Walter Roberson
2020년 1월 1일
Your colorbar looks like that because you did not use linspace() like we said. You used your unsorted vec, and you used one colormap entry for each entry in vec.
Is your task to create a colormap and apply it to an image (or imagem or surface plot or patch) ? If so then you need to create a sorted vector of equal-spaced values -- the linspace that we have been repeatedly using in the code -- and in that case you would get a sensible colormap.
Or is your task to take a vector of values and convert each one into an RGB color directly, such as you might do for creating an RGB image yourself without a colormap, or such as you might do for creating a scatter plot in which each point is individually colored? That is what your code in https://www.mathworks.com/matlabcentral/answers/498482-how-to-convert-a-vector-to-rgb-colormap#comment_782315 is doing, converting each value in the vector into an RGB color directly, with the resulting array of color entries being your cmap array. That is a valid thing to do, very useful sometimes, but it will not give you an array you can use for a colorbar.
It is valid to combine the two approaches, to create a direct array of RGB values, and to also create a linear colormap using linspace() that you use to colorbar(), just for user comprehension, even though the colormap is not influencing what displays on the screen in that case.
Ali Nouri
2020년 1월 1일
My task as you mention; it is to take a vector of random value, which are between -1 and 1 and and the i need to convert each value to an rgb colormap. when i have convert this vector to rgb colormap, then i need use this color map to make plot over europe, beacuse each value in this vector represent for one country in europe.
I can sort the vector and change it to rgb colormap, but then the values in vector does not represent the right country.
Now with help of you guys, i can convert a vector to rgb colormap, but still i can not use this colormap correlty.
Image Analyst
2020년 1월 1일
Sorry but I don't have the mapping toolbox so I can't run some functions in your code and don't have demo files such as 'cntry02.shp'. I've added the Mapping Toolbox to the Products list your were supposed to fill out when you posted this. Hopefully Wlater has it and can continue to help you.
Image Analyst
2020년 1월 2일
Sine I don't have the toolbox, I don't have shaperead() and so I can't continue.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Orange에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)