using surf function on color image in matlab

To use the surf function on a color image in Matlab, you first need to convert the image from RGB to grayscale. This can be done using the rgb2gray function. Once the image is in grayscale, you can apply the surf function to generate a 3D plot of the image intensity values.

Here's an example code snippet that demonstrates the process:

main.m
% Read in a color image
img = imread('lena.png');

% Convert the image to grayscale
gray_img = rgb2gray(img);

% Generate a 3D plot of the grayscale image using surf
figure;
surf(gray_img);
colormap(gray);
axis off;
216 chars
12 lines

In this example, we read in a color image, convert it to grayscale using rgb2gray, generate a 3D plot of the grayscale image using surf, set the colormap to grayscale, and turn off the axis to create a cleaner plot.

gistlibby LogSnag