how to make hsv converted image two dimensional?

11 views (last 30 days)
I have converted an image to hsv image...now i need to find the edges by using canny.But while doing i am getting an error...
Error using ==> iptcheckinput Function EDGE expected its first input, I, to be two-dimensional.
Error in ==> edge>parse_inputs at 541 iptcheckinput(I,{'numeric','logical'},{'nonsparse','2d'},mfilename,'I',1);
Error in ==> edge at 197 [a,method,thresh,sigma,thinning,H,kx,ky] = parse_inputs(varargin{:});
Error in ==> mergealgo at 37 im_canny = edge(inputgray, 'canny');
how to make hsv image two dimensional?

Accepted Answer

Jan
Jan on 19 Apr 2012
You have to decide for one channel, e.g.:
RGB = rand(100, 200, 3);
HSV = rgb2hsv(RGB);
im_canny = edge(HSV(:, :, 1), 'canny');
Depending on your problem the H, S or V channel is best to recognize edges.
  6 Comments
vysakh
vysakh on 20 Apr 2012
size(inputgray)
ans =
256 256 3
>> class(watimg)
ans =
double
>> size(watimg)
ans =
256 256 3
>> class(inputgray)
ans =
double
Walter Roberson
Walter Roberson on 20 Apr 2012
a 256 x 256 x 3 image is *not* a grayscale image.
max(max(watimg)) when watimg is 3d, is going to be a vector in the third dimension. A vector cannot appear in a range specification such as 1:N . You could get around the complaint about it being "full 3d real" by using
for i = 1 : squeeze(max(max(watimg)))
but then you will still get a complaint because you will be getting a vector rather than a scalar.
If you want to be cutting edge, you could change the line to
for i = 1 : max(watimg(:))
but I think it very likely you will run into problems further on.
But is combineregion your own routine or someone else's? It is not a Mathworks-provided routine, and it does not appear to be part of any File Exchange contribution.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 20 Apr 2012
My guess is that the "inputgray" image is still a 3D RGB image, or else the 3D HSV image. What does this say when you put it in?
[rows columns numberOfColorChannels] = size(inputgray)
It should tell you the sizes/dimensions in the command window. Here's code for a working demo using the standard MATLAB peppers color image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 4, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Get HSV color space version of the image
hsv = rgb2hsv(rgbImage);
h = hsv(:, :, 1);
s = hsv(:, :, 2);
v = hsv(:, :, 3);
% Display the images.
subplot(2, 4, 2);
imshow(h, []);
title('Hue Image', 'FontSize', fontSize);
subplot(2, 4, 3);
imshow(s, []);
title('Saturation Image', 'FontSize', fontSize);% Display the images.
subplot(2, 4, 4);
imshow(v, []);
title('Value Image', 'FontSize', fontSize);
% Run Canny Edge on them all
hEdge = edge(h, 'canny');
sEdge = edge(s, 'canny');
vEdge = edge(v, 'canny');
% Display the images.
subplot(2, 4, 6);
imshow(hEdge, []);
title('Hue Edge Image', 'FontSize', fontSize);
subplot(2, 4, 7);
imshow(sEdge, []);
title('Saturation Edge Image', 'FontSize', fontSize);% Display the images.
subplot(2, 4, 8);
imshow(vEdge, []);
title('Value Edge Image', 'FontSize', fontSize);
  2 Comments
vysakh
vysakh on 20 Apr 2012
i want the same image to be converted to hsv and use it to find edges using canny...
Image Analyst
Image Analyst on 20 Apr 2012
And what do you think I did? Did you even run this demo I made for you???

Sign in to comment.


Walter Roberson
Walter Roberson on 20 Apr 2012
There is no 2 dimensional equivalent to an hsv image.
The closest you can get is to use the hsv equivalent of pseudo-color images -- to create a 2 dimensional array in which each value is the index of an HSV tuple in a map.
You can create such a thing by applying rgb2ind() to the HSV array (rgb2ind() will not notice that the data is actually HSV.)
I warn, however, that the canny edge detector will likely have very poor performance on mapped images. The index numbers that are used in mapping do not have any relationship to each other: index #83 might refer to a color that is extremely different than index #84.
If working on one of the individual channels, H, S, or V, does not work for you, then converting to a mapped HSV image and doing the edge detection on that will almost certainly be much worse.
But you can always try it if you need to prove to yourself that a "2 dimensional HSV image" is the wrong thing to use.

Categories

Find more on Images in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!