Matlab生成RGB彩色渐变光栅图,RGB渐变色

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 一、用word颜色自定义查看需要的颜色渐变
  • 二、Matlab生成的一个周期颜色渐变

一、用word颜色自定义查看需要的颜色渐变

Matlab生成RGB彩色渐变光栅图,RGB渐变色_第1张图片

 例如:

[255,0,0]红R
[0,255,0]绿G
[0,0,255]蓝B

二、Matlab生成的一个周期颜色渐变

代码如下(示例):

H = 1536; %定义一个周期内的长宽
W = 1536;
imgvertical = uint8(zeros(H,W,3)); %RGB三通道
imghorizontal = uint8(zeros(H,W,3)); 

pixelstep=256; % 256个像素点


numsV = W / pixelstep;
numsH = H / pixelstep;

% 可以打开word中的自定义颜色,查看颜色变化
% [255,0:1:255,0]; %[255,0,0]红
% [255:1:0,255,0];
% [0,255,0:1:255]; %[0,255,0]绿
% [0,255:1:0,255];
% [0:1:255,0,255]; %[0,0,255]蓝
% [255,0,255:1:0];

for i = 1:numsV
    if i==1
        for j=0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = 255;  % 竖条纹 [255,0,0]渐变到[255,255,0]
%            Q1=imgvertical(:,(j + 1) + (i - 1) * 255,2);
            imgvertical(:,(j + 1) + (i - 1) * 256,2) =j;
%            Q2=imgvertical(:,(j + 1) + (i - 1) * 255,2);
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = 0; 
        end
    elseif i==2
%        Q3=imgvertical(:,1:256,2);
         for j =0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = 255-j;  % 竖条纹
            imgvertical(:,(j + 1) + (i - 1) * 256,2) = 255;
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = 0; 
        end
     elseif i==3
         for j = 0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = 0;  % 竖条纹
            imgvertical(:,(j + 1) + (i - 1) * 256,2) = 255;
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = j; 
         end
      elseif i==4
         for j = 0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = 0;  % 竖条纹
            imgvertical(:,(j + 1) + (i - 1) * 256,2) = 255-j;
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = 255; 
         end
        elseif i==5
         for j = 0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = j;  % 竖条纹
            imgvertical(:,(j + 1) + (i - 1) * 256,2) = 0;
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = 255; 
         end
        elseif i==6
         for j = 0:255
            imgvertical(:,(j + 1) + (i - 1) * 256,1) = 255;  % 竖条纹
            imgvertical(:,(j + 1) + (i - 1) * 256,2) = 0;
            imgvertical(:,(j + 1) + (i - 1) * 256,3) = 255-j; 
         end        
    end
end

for i = 1:numsH
    if i==1
        for j=0:255
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = 255; %横条纹
            imghorizontal((j + 1) + (i - 1) * 256,:,2) =j;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = 0; 
        end
    elseif i==2
         for j = 255:-1:0
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = 255-j;  
            imghorizontal((j + 1) + (i - 1) * 256,:,2) = 255;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = 0; 
        end
     elseif i==3
         for j = 0:255
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = 0; 
            imghorizontal((j + 1) + (i - 1) * 256,:,2) = 255;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = j; 
         end
      elseif i==4
         for j = 0:255
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = 0;  
            imghorizontal((j + 1) + (i - 1) * 256,:,2) = 255-j;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = 255; 
         end
        elseif i==5
         for j = 0:255
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = j;  
            imghorizontal((j + 1) + (i - 1) * 256,:,2) = 0;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = 255; 
         end
        elseif i==6
         for j = 0:255
            imghorizontal((j + 1) + (i - 1) * 256,:,1) = 255;  
            imghorizontal((j + 1) + (i - 1) * 256,:,2) = 0;
            imghorizontal((j + 1) + (i - 1) * 256,:,3) = 255-j; 
         end        
    end
end


figure
subplot(121)
imshow(imgvertical)
title('Vertical')
 
subplot(122)
imshow(imghorizontal)
title('Horizontal')

结果如下:

Matlab生成RGB彩色渐变光栅图,RGB渐变色_第2张图片

 

你可能感兴趣的:(matlab,图像处理,开发语言)