MATLAB Answers

How to add and subtract two color images in matlab?

24 views (last 30 days)
I-G(I);
where I=original image and G(I)=Gaussian filtered version of I.

  2 Comments

Rik
Rik on 31 Jan 2020
Using just the normal symbols will result in an element-wise addition or subtraction. That could cause issues with underflow and overflow if you are using an integer data type, and the result might not be what you mean.
What is your question? What have you tried? How did it fail to match your desired result?
Sajina Rose
Sajina Rose on 1 Feb 2020
I'm writing a code for subtract two images and then apply histogram stretching to resultant image.
Here, my sample code
%%%%%%
z=imsubtract(double(X)-double(Y));
N=imadjust(z,[],0.5);
figure,imshow(N),title('Histogram Stretching');
%%%%%%%
But is shows error like,
"Not enough input arguments.
Error in imsubtract (line 34)
scalarDoubleY = isa(Y,'double') && numel(Y) == 1;
Error in method1 (line 83)
z=imsubtract(double(AGW)-double(B));"

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 1 Feb 2020
Hi, Sajina the function imsubtract expects two real, nonsparse, numeric or logical image. Threfore the below syntax is not right.
z = imsubtract(double(X)-double(Y));
Try the code below.
% Read your image here
X = imread('peppers.png');
figure; imshow(X, []); title('Original Color Image');
% Gaussian Filtering
sigma = 5; % Standard deviation of the kernel
Y = imgaussfilt(X, sigma);
figure; imshow(Y, []); title('Gaussian Filtered Image');
Z = imsubtract(X, Y);
N = imadjust(Z, stretchlim(Z));
figure,imshow(N, []), title('Histogram Stretched Subtracted Image');
gaussSub.png
Hope this helps!

  4 Comments

Show 1 older comment
Rik
Rik on 1 Feb 2020
The imsubtract is already handling the subtraction, so you don't have to use the symbol yourself. See if the line below helps.
z=imsubtract(double(X),double(Y));
Subhadeep Koley
Subhadeep Koley on 1 Feb 2020
@ Sajina Rose Refer the below code
% Read your image here
X = imread('peppers.png');
figure; imshow(X, []); title('Original Color Image');
% Gaussian Filtering
sigma = 5; % Standard deviation of the kernel
Y = imgaussfilt(X, sigma);
figure; imshow(Y, []); title('Gaussian Filtered Image');
Z = imsubtract(X, Y);
N = imadjust(Z, stretchlim(Z));
figure,imshow(N, []), title('Histogram Stretched Subtracted Image');
U = (imadd(X, N)) / 2;
figure,imshow(U, []); title('Unsharp Masked Image');
Otherwise you can use the MATLAB built-in function imsharpen(), which performs Unsharp Masking.
XSharp = imsharpen(X, 'Radius', 1.5, 'Amount', 2);
figure,imshow(XSharp, []), title('Unsharp Masked Image');

Sign in to comment.

More Answers (0)

Sign in to answer this question.