MATLAB Answers

How to subtract two images in matlab?

조회 수: 219(최근 30일)
I have couple of images below which I want to subtract from each other. The images are taken from the same sample at different time frames. I want to notice the movement of the particles between those time frames. So I want to take out the part of the image that only contains the liquid particles. I want to subtract INTENSITY14.JPG from INTENSITY15.JPG. Ideally, the resultant image should look like SUBTRACT.JPG where the red dased box shows the part of the image which contains the liquid. But, I am getting the result as shown in S1.jpg which is undesirable. Below is my code. Any suggestions would be very much appreciated. Thank you.
a = imread('Intensity15.jpg');
b = imread('Intensity14.jpg');
dblSubtractedImage = double(a) - double(b);
imshow(dblSubtractedImage, []);

  댓글 수: 0

댓글을 달려면 로그인하십시오.

채택된 답변

Benjamin Brenner
Benjamin Brenner 20 Aug 2019
Your code is successfully subtracting the two images. The reason your subtraction looks like that is because your two images are so closely correlated that whatever is left after subtraction is mostly noise. How did you get SUBTRACT.jpg? I don't see how you could get an image like that from subtracting a and b.

  댓글 수: 3

Warid Islam
Warid Islam 20 Aug 2019
Hi Benjamin,
I found subtract.jpg by analyzing both the images manually, i.e. by repeatedly switching the images using the key board and noticing any changes. The red dashed box is the part of the image that changes when I carefully analyze both the images. I wanted to identify these changes using MATLAB where the change would be automatically detected and would be cropped into a separate image. Subtracting images is just a process that came on my mind. Is there any other way by which I can obtain the desired result? Thank you.
Benjamin Brenner
Benjamin Brenner 20 Aug 2019
I see what you mean. The images are a bit different. Theoretically subtracting them should show that. Converting a and b to double makes the display look oversaturated. If you replace the last two lines with these lines:
dblSubtractedImage = (a) - (b);
imshow(dblSubtractedImage*5);
The difference comes out a little more between the sections. I attached a figure. If I was doing showing the difference quantitatively, I might make a 2d pot that shows the correlation between each row of the two images. Something like this:
ag=rgb2gray(a);
bg=rgb2gray(b);
for i=1:650
correlation(i)=corr2(ag(i,:),bg(i,:))
end
plot(correlation(50:584))
or maybe if you want to correlate 10 rows at a time
ag=rgb2gray(a);
bg=rgb2gray(b);
for i=1:65
correlation(i)=corr2(ag((i*10-9):(i*10),:),bg((i*10-9):(i*10),:))
end
plot(correlation(5:58))
Warid Islam
Warid Islam 21 Aug 2019
Hi Benjamin,
That helps a lot. Thank you.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by