How can I keep overlapping regions that I see in Imfuse and remove everything else in the image?
조회 수: 15(최근 30일)
표시 이전 댓글
I used region props and have converted frames in a video binary images. How do I keep only the overlapping regions and remove everythign else? I would like to store the residual as frames to a video
댓글 수: 2
AAS
2020년 7월 6일
The right most panel is the imfused image of the first and second panel. I would like to keep only the regions that have an overlap and discard everything else as shown in the next image i.e just keep the blue circled regions .


채택된 답변
Image Analyst
2020년 7월 6일
This can be done in one line of code with imreconstruct().
J = imreconstruct(marker,mask) performs morphological reconstruction of the image marker under the image mask, and returns the reconstruction in J. The elements of marker must be less than or equal to the corresponding elements of mask. If the values in marker are greater than corresponding elements inmask, then imreconstruct clips the values to the mask level before starting the procedure.
댓글 수: 14
AAS
2020년 7월 6일
I also would like the keep the part around the overlapping region. How would I do it with imreconstruct?
Image Analyst
2020년 7월 7일
Show me a blown up diagram of the two input regions, and the output region(s) that you would like to keep.
AAS
2020년 7월 7일
Hi ,
Thank you so much for helping. Attached is the diagram-
I would like to keep the cyan circled images and remove the magenta circled images.
Thank you

Image Analyst
2020년 7월 7일
So, for the blobs that have yellow (overlap) in them, you want a new blob that is the union of the green, yellow, and red regions? Or you want just the yellow part only? Or just the red+green parts without the yellow?
I'll check back tomorrow. In the meantime, did you at least try imreconstruct()?
AAS
2020년 7월 7일
I would like the union of yellow, red and green parts. Yes, I was able to use imreconstruct but that was succesful in getting the yellow part only.
Thanks a lot!!
AAS
2020년 7월 7일
I was able to get it.. I used one of your previous solutions (code attached below in italics) to create larger rectangles around the separated overlapping yellow region and multiplied it with the original image so hence was able to get the green,red and yellow region. If you have any other recommendations, please let me know. Thanks a ton for your help.
binaryImage = grayImage > someThreshold;
[labeledImage, numBlobs] = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox');
for k = 1 : numBlobs
% Get the bounding box for this particular blob.
thisBB = measurements(k).BoundingBox;
% Get rows and columns of the bounding box for this blob.
col1 = ceil(thisBB(1));
col2 = col1 + thisBB(3);
row1 = ceil(thisBB(2));
row2 = row1 + thisBB(4);
% Write white into original image there.
grayImage(row1:row2, col1:col2) = 255;
end
Image Analyst
2020년 7월 7일
That doesn't look right. You could have problems if a pair invaded the bounding box of another pair. Attach the two original binary images and I'll do it correctly for you.
AAS
2020년 7월 7일
Hi,
Thats a great point! I have attached the two original images and their infused product, Please let me know if there is an any confusion or error.
Thanks a lot once again!!
Image Analyst
2020년 7월 7일
Please attach the original PNG files, or at least a mat file with the images in it. I can't get the images from the fig files without some extra work. Make it easy for me to help you, not hard.
Image Analyst
2020년 7월 7일
Try this:
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
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;
pre = imread('pre.png');
post = imread('post.png');
% Convert to logical
pre = logical(pre(:,:,1));
post = logical(post(:,:,1));
% Show image.
subplot(3, 2, 1);
imshow(pre);
axis('on', 'image');
impixelinfo;
title('Pre', 'FontSize', fontSize);
% Show image.
subplot(3, 2, 2);
imshow(post);
axis('on', 'image');
impixelinfo;
title('post', 'FontSize', fontSize);
% Find common/overlap areas
overlapped = pre & post;
% Show image.
subplot(3, 2, 3);
imshow(overlapped);
axis('on', 'image');
impixelinfo;
title('overlapped', 'FontSize', fontSize);
fused = imfuse(pre,post,'falsecolor','Scaling','joint','ColorChannels',[1 2 0]);
subplot(3, 2, 4);
imshow(fused)
axis('on', 'image');
impixelinfo;
title('fused', 'FontSize', fontSize);
% Make the union of images.
both = pre | post;
% Show image.
subplot(3, 2, 5);
imshow(both);
axis('on', 'image');
impixelinfo;
title('Both -- ORed, Union of images', 'FontSize', fontSize);
% Make output image.
output = imreconstruct(overlapped, both);
% Show image.
subplot(3, 2, 6);
imshow(output);
axis('on', 'image');
impixelinfo;
title('Output', 'FontSize', fontSize);

In the lower images, I zoomed in on one particular blob so you can see that it's getting all of the pre and post together in one blob. In the output there are only the 4 small blobs (and of course the huge white frame that you included) so it looks pretty similar to the "overlapped" image, just with slightly bigger blobs.
AAS
2020년 7월 13일
Thank you very much!!! Is there any way to extract the blobs in pre that have an overlap with the post? In other words, I only want the blobs in pre that have an overlap in the next frame. Really sorry if this is redundant
Image Analyst
2020년 7월 13일
Sure. Just simply use pre in imreconstruct() instead of both:
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
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;
pre = imread('pre.png');
post = imread('post.png');
% Convert to logical
pre = logical(pre(:,:,1));
post = logical(post(:,:,1));
% Show image.
subplot(3, 2, 1);
imshow(pre);
axis('on', 'image');
impixelinfo;
title('Pre', 'FontSize', fontSize);
% Show image.
subplot(3, 2, 2);
imshow(post);
axis('on', 'image');
impixelinfo;
title('post', 'FontSize', fontSize);
% Find common/overlap areas
overlapped = pre & post;
% Show image.
subplot(3, 2, 3);
imshow(overlapped);
axis('on', 'image');
impixelinfo;
title('overlapped', 'FontSize', fontSize);
fused = imfuse(pre,post,'falsecolor','Scaling','joint','ColorChannels',[1 2 0]);
subplot(3, 2, 4);
imshow(fused)
axis('on', 'image');
impixelinfo;
title('fused', 'FontSize', fontSize);
% Make output image.
output = imreconstruct(overlapped, pre);
% Show image.
subplot(3, 2, 6);
imshow(output);
axis('on', 'image');
impixelinfo;
title('Output', 'FontSize', fontSize);
추가 답변(0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)