2

I've been trying to model the HSV Color Space onto a cylinder in MATLAB, but have failed thus far. I've looked at Modeling HSV Color Space in MATLAB which has been very helpful in understanding the texture map, but when I create a cylinder and input its X,Y, and Z coordinates into surf(), I get an open cylinder. Is there a way to generate a closed cylinder with X,Y,Z coordinates that can be input into surf()? Below is the code I have been working with thus far (very similar to link):

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
Community
  • 1
  • 1
Luiserebii
  • 55
  • 2
  • 11

1 Answers1

2

You need to add a center point for the top of your cylinder so that half of the texture can be mapped on the top surface and half down the sides. You also have to flip your Z coordinates. Here's how to generate your cylinder coordinates:

[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

And you should get this plot:

enter image description here

gnovice
  • 125,304
  • 15
  • 256
  • 359