【问题标题】:Modeling HSV Color Space Cylinder in MATLAB在 MATLAB 中建模 HSV 颜色空间圆柱体
【发布时间】:2015-10-10 00:19:19
【问题描述】:

我一直在尝试将 HSV 颜色空间建模到 MATLAB 中的圆柱体上,但到目前为止都失败了。我查看了Modeling HSV Color Space in MATLAB,它对理解纹理贴图很有帮助,但是当我创建一个圆柱体并将其 X、Y 和 Z 坐标输入到 surf() 中时,我得到了一个开放的圆柱体。有没有办法生成一个可以输入到 surf() 中的带有 X、Y、Z 坐标的封闭圆柱体?以下是我迄今为止一直在使用的代码(与链接非常相​​似):

clear;
clc;
close all;

spSize = 500;

% linspace(a,b,s) creates a vector with s values equally spaced from a
% to b
% repmat repeats a vector x*y times
H = repmat(linspace(0,1,spSize),spSize,1);                     % spSize-by-spSize hues
% the spSize-value vector generated by linspace(0,1,spSize) is copied spSize times
% one under the other, resulting in a spSize x spSize array:
%
%        1           2            3           spSize
% 1    (0,0)     (0,.001)     (0,.002)  ...  (0,1)
% 2   (.001,0) (.001,.001) (.001,.002) ... (.001,1)
% ...
% spSize  (1,0)     (.001,1)     (.002,1)  ...   (1,1)

S = repmat([linspace(0,1,spSize/2) linspace(1,0,spSize/2)].',1,spSize); % spSize-by-spSize saturations
% same for the saturation. Because the saturation goes from 1 to 0 to 1, we
% need first a vector that goes from 1 to 0 and a second from 0 to 1
% ([linspace(0,1,spSize/2) linspace(1,0,spSize/2)]). The ' turnes the vector in a
% row into a vector in a column.

V = repmat([ones(1,spSize/2) linspace(1,0,spSize/2)].',1,spSize);        % spSize-by-spSize values
% same procedure for the intensity. Because we map a picture on a 3D-shape,
% one part of the picture has to indicate the colors of the top of the cone
% (i.e., the color wheel) - that is the first part of the vector
% (ones(1,spSize/2)) than we need to reduce the intensity to generate the rest of
% the color for the rest for the rest of the cylinder surface.

% Create an HSV image
% once all three matrices ahave been independently generated, merge them
% into a single 3-D matrix
hsvImage = cat(3,H,S,V);

% Convert it to an RGB image
C = hsv2rgb(hsvImage);

% Generate cylinder with XYZ coordinates
[X,Y,Z] = cylinder(250);
Z = Z * 400;

% Finally, plot the texture-mapped surface
figure;
surf(X,Y,Z,C,'FaceColor','texturemap','EdgeColor','none');
axis equal

【问题讨论】:

    标签: matlab colors hsv


    【解决方案1】:

    您需要为圆柱体的顶部添加一个中心点,以便纹理的一半可以映射到顶面,而另一半可以映射到侧面。您还必须翻转 Z 坐标。以下是如何生成圆柱坐标:

    [X, Y, Z] = cylinder(250);
    X = [zeros(1, 21); X];
    Y = [zeros(1, 21); Y];
    Z = 400.*Z([2 2 1], :);
    figure;
    surf(X, Y, Z, C, 'FaceColor', 'texturemap', 'EdgeColor', 'none');
    axis equal
    

    你应该得到这个情节:

    【讨论】:

    • 耶!来自gnovice的回答!
    猜你喜欢
    • 2011-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-08
    • 2011-11-14
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多