can i subtract 1 image from another by imsubtract()? i am having some errors in this case.

i have generated 2 images, one by sobel operator and 1 by 1x2 operator. now i am trying to subtract the 1x2 operator applied image from sobel operator applied image. i used imsubtract() but it is not displaying the subtracted image. it is giving errors. can we use imsubtract() to only subtract arrays or we can also use it for subtractng images? please let me know.
grayImage= rgb2gray(rgbImage);
size(grayImage);
BW = edge(grayImage,'sobel',0.006);
filteredImage = conv2(double(grayImage), [-1 1], 'same');
subtractedImage = imsubtract(BW,filteredImage);
imshow(subtractedImage);
drawnow();

 Accepted Answer

The values are likely very small for the 1x2 filter - like 0-20 or so. And the Sobel thresholded edge image has values of only 0 and 1. So subtracting almost anything at all from that (except subtracting zero) will clip to zero. So your image is most likely all zeros and displays as black. There may - slight chance - be some pixels that have a value of 1 and you can see those if they exist with the [] option to imshow():
imshow(subtractedImage, []);
But most likely there are no such 1 pixels. But the bigger question is why you are even doing this strange operation in the first place. I can see no theoretical reason why you'd want this subtraction in your algorithm - it's just worthless. Share your thought process so I can see if it makes sense or if what you want to do is better accomplished in some other way that does make sense.

11 Comments

thank you. i am trying this. i am an image processing and 3D computer vision student. the problems are part of my assignments and exercises. i am asked to display the result and check whether there is any residue when subtracting a 1x2 operator applied image from sobel operator applied image. i can understand it visually but cannot display the residue. so i am trying to subtract it.
when i am running the program, it is showing error
Error in assignment (line 44)
subtractedImage = imsubtract(BW,filteredImage);
the code is here
grayImage= rgb2gray(rgbImage);
size(grayImage);
BW = edge(grayImage,'sobel',0.006);
filteredImage = conv2(double(grayImage), [-1 1], 'same');
subtractedImage = imsubtract(BW,filteredImage);
imshow(subtractedImage,[]);
drawnow();
You do know that the image you get from edge() is not the Sobel edge image, don't you? It has been thresholded and skeletonized.
i have edited the code a bit
%....the sobel operator applied edge image
grayImage= rgb2gray(rgbImage);
size(grayImage);
C=double(grayImage);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Sobel mask for x-direction:
Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
%Sobel mask for y-direction:
Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image
%grayImage(i,j)=abs(Gx)+abs(Gy);
grayImage(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
title('Sobel gradient');
imshow(grayImage);
drawnow();
%...part(2)1x2 operator
grayImage = rgb2gray(rgbImage);
size(grayImage);
filteredImage = conv2(double(grayImage), [-1 1], 'same');
imshow(filteredImage);
drawnow();
%1x2 operator applied image have visual advantage over sobel operator in
%this case
%.......part(2)resulting image of 1x2 operator applied image subtracted from sobel applied
%image
subtractedImage = imsubtract(filteredImage,grayImage);
if (numel(grayImage)) == 1 && isa(class(grayImage),'double');
subtractedImage1 = imlincomb(1.0, filteredImage, -grayImage);
else
subtractedImage1 = imlincomb(1.0, filteredImage, -1.0, grayImage);
end
imshow(subtractedImage,[]);
drawnow();
but here it is showing this error
Error using imsubtract (line 54)
X and Y must have the same size and class or Y must be a scalar double.
Error in assignment2 (line 61)
subtractedImage = imsubtract(filteredImage,grayImage);
please let me know how to overcome this
Try casting both to double. Then, use conv2 to do Sobel filter rather than manually like you're doing - it will be more efficient and faster.
i edited the code, but now it is not showing the sobel edge image
grayImage= rgb2gray(rgbImage);
size(grayImage);
C=double(grayImage);
A = [-1 -2 -1;0 0 0;1 2 1];
B = [-1 0 1;-2 0 2;-1 0 1];
Gx = conv2(C, A);
Gy = conv2(C, B);
%The gradient of the image
%grayImage(i,j)=abs(Gx)+abs(Gy);
filteredSobelImage = sqrt(Gx.^2+Gy.^2);
title('Sobel gradient');
imshow(filteredSobelImage);
drawnow();
it is only showing a white screen with a few dots on it.
i can now generate the sobel gradient image but it is not as prominent as before(for the manual calculation). and i cannot do the subtraction also. it is displaying the same error. this is the code
grayImage= rgb2gray(rgbImage);
size(grayImage);
C=double(grayImage);
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
resX = conv2(C, maskX);
resY = conv2(C, maskY);
gradient = sqrt(resX.^2 + resY.^2);
direction = atan(resY/resX);
thresh = gradient < 25;
gradient(thresh) = 0;
title('Sobel gradient');
uiwait(msgbox('Click OK to see the sobel operator applied image'));
imshow(gradient);
drawnow();
%...part(2)1x2 operator
grayImage = rgb2gray(rgbImage);
size(grayImage);
D = double(grayImage);
filteredImage = conv2(D, [-1 1], 'same');
uiwait(msgbox('click OK to see the 1x2 operator applied image'));
imshow(filteredImage);
drawnow();
%.......part(2)resulting image of 1x2 operator applied image subtracted from sobel applied
%image
subtractedImage = imsubtract(filteredImage,gradient);
if (numel(gradient)) == 1 && isa(class(gradient),'double');
subtractedImage1 = imlincomb(1.0, filteredImage, -gradient);
else
subtractedImage1 = imlincomb(1.0, filteredImage, -1.0, gradient);
end
uiwait(msgbox('click OK to see the subtracted image'));
imshow(subtractedImage1,[]);
drawnow();
the error is Error using imsubtract (line 55) X and Y must have the same size and class or Y must be a scalar double.
Error in assignment2 (line 60)
subtractedImage = imsubtract(filteredImage,gradient);
I still don't know what you're doing, but this code fixes it. You didn't use the 'same' option in conv2(). I also use subplot to show all images on the same screen.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% 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, 3, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
grayImage= rgb2gray(rgbImage);
subplot(2, 3, 2);
imshow(grayImage);
title('Gray Image', 'FontSize', fontSize);
size(grayImage);
C=double(grayImage);
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
resX = conv2(C, maskX, 'same');
resY = conv2(C, maskY, 'same');
SobelImage = sqrt(resX.^2 + resY.^2);
subplot(2, 3, 3);
imshow(SobelImage, [])
title('Sobel Image', 'FontSize', fontSize);
direction = atan(resY/resX);
thresh = SobelImage < 25;
SobelImage(thresh) = 0;
% Part(2)1x2 operator
grayImage = rgb2gray(rgbImage);
size(grayImage);
D = double(grayImage);
filteredImage = conv2(D, [-1 1], 'same');
subplot(2, 3, 4);
imshow(filteredImage, []);
title('2x1 Filtered Image', 'FontSize', fontSize);
% Part(3): resulting image of 1x2 operator applied image
% subtracted from sobel applied image.
subtractedImage = imsubtract(filteredImage,SobelImage);
if (numel(SobelImage)) == 1 && isa(class(SobelImage),'double');
subtractedImage1 = imlincomb(1.0, filteredImage, -SobelImage);
else
subtractedImage1 = imlincomb(1.0, filteredImage, -1.0, SobelImage);
end
subplot(2, 3, 5);
imshow(subtractedImage1,[]);
title('Subtracted Image', 'FontSize', fontSize);
thank you. i am trying it. this is part of my assignment..thank you
But what does subtracting a direction edge filter from a non-directional edge filter mean? How does that help with anything? And what is the purpose of the subtractedImage1 stuff? I see no use for it at all. Just using subtractedImage as it is, is fine.

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!