Filter löschen
Filter löschen

How do I do Edge detection with filling the holes?

34 Ansichten (letzte 30 Tage)
curious dolphin
curious dolphin am 29 Jan. 2022
Kommentiert: Image Analyst am 30 Jan. 2022
Hi! I'm practicing on edge detection but i cant seem to do it properly. Im trying this photo since i also want to fill holes from the window.
This is the code that I've come up with so far. Im not sure about the error in line 6 and i cant figure out the error in line 6
BGBG= imread('bldg.png');
imshow(BGBG);
graybg= rgb2gray(BGBG);
imshow(graybg);
edgefr=edge(graybg,'Sobel');
BWBW= bwareaopen(imfill(edge,'holes')*20);
figure; imshow(BWBW);
BWBW=bwpropfilt(BWBW,'Area',[1000, 3667]);
BWBW=bwpropfilt(BWBW,'Solidity',[0.5, 1]);
imshow(graybg);
pause;
graybg(BWBW)=0;
imshow(graybg);

Antworten (2)

DGM
DGM am 29 Jan. 2022
edgefr = edge(graybg,'Sobel'); % this is your variable name
BWBW = bwareaopen(imfill(edgefr,'holes'),20); % wrong variable name, comma
  3 Kommentare
DGM
DGM am 30 Jan. 2022
Bearbeitet: DGM am 30 Jan. 2022
Why would the edgemap be overlaid? The edgemap is only being used as a method of finding solid rectangular regions in the image. Creating a mask is the goal. Using an edgemap is just one means to that end. Once BWBW is created, the edgemap isn't needed anymore.
BGBG = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/877930/image.png');
graybg = rgb2gray(BGBG);
% create edgemap
edgefr = edge(graybg,'Sobel');
imshow(edgefr);
% objects in BWBW represent closed paths in the edgemap
BWBW = bwareaopen(imfill(edgefr,'holes'),20); % fill closed paths, filter
BWBW = bwpropfilt(BWBW,'Area',[1000, 3667]); % filter
BWBW = bwpropfilt(BWBW,'Solidity',[0.5, 1]);
imshow(BWBW);
% apply the mask
graybg(BWBW) = 0;
imshow(graybg); % the mask has been applied
FWIW, using bwareaopen() and bwpropfilt(...'area',...) is redundant. The call to bwareaopen() removes all objects <20px. The first call to bwpropfilt() removes all objects <1000px.
curious dolphin
curious dolphin am 30 Jan. 2022
Thank you so much! Thank you also for clarifying the difference of an overlay and a mask! Will definitely take note on these.

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 30 Jan. 2022
Is this what you want? Or do you want to find the windows in the garage door instead?
% Demo by Image Analyst
clc; % Clear the command window.
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 = 15;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
fileName = 'building.png';
grayImage = imread(fileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Extract the blue channel (so the magenta lines will be white).
grayImage = grayImage(:, :, 3);
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
axis('on', 'image');
caption = sprintf('Original image is %d rows by %d columns', rows, columns);
title(caption, 'FontSize', fontSize);
hold on
drawnow;
% Maximize window.
g = gcf;
g.WindowState = 'maximized'
drawnow;
% Defining edges and filling holes
% Finding windows based on properties
edgeImage = edge(grayImage,'Sobel');
subplot(2, 2, 2);
imshow(edgeImage);
title('Edge Image', 'FontSize', fontSize)
mask = imfill(edgeImage,'holes');
mask = bwareaopen(mask, 20);
% Image Region Analyzing
props = regionprops(mask, 'Area');
allAreas = sort([props.Area])
% Filters image based on properties
mask=bwpropfilt(mask,'Area',[1000, 3667]);
% mask=bwpropfilt(mask,'Solidity',[0.5, 1]);
subplot(2, 2, 3);
imshow(mask);
title('Final Mask', 'FontSize', fontSize)
% Result of Edge Detection
subplot(2, 2, 4);
overlayImage = imoverlay(grayImage, mask, 'r');
imshow(overlayImage);
title('Final Image', 'FontSize', fontSize)
  2 Kommentare
curious dolphin
curious dolphin am 30 Jan. 2022
Im trying to cover the windows with black using the edge detection. I cant seem to overlay the detected edges to the gray photo like this as you could see in MATLAB's video https://www.youtube.com/watch?v=Z4msmuJNpI4
I'm practicing on edge detection so that I could apply it to other images but i can't do the same as what the video showed me.
Image Analyst
Image Analyst am 30 Jan. 2022
Your edge for the middle window did not go all the way around. Edge detection is rarely the approach you need to use for images. In this case, some thresholding and morphological operations would be the better approach. But you're practicing edge detection. Let this be a lesson as to why edge detection is rarely good. So, you've learned something. If you really need to blacken the windows (which I guess was not really the goal of your exercise - learning about edge detection was) then let me know and I'll show you how to do it. But the algorithm would apply to this image or very very similar images. It wouldn't apply to all house images and all windows.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by