Filter löschen
Filter löschen

filter objects below a certain size in a binary image ?

1 Ansicht (letzte 30 Tage)
AMIT VERMA
AMIT VERMA am 20 Jan. 2015
Kommentiert: AMIT VERMA am 22 Jan. 2015
clear
clc
A5=imread('C:\Users\AMIT\Desktop\CB\sulphur\4-5.jpg');
imshow(A5);
BW = im2bw(A5,0.5);
imshow(BW,0.5)
C = imcrop(BW);
bw2 = bwareaopen(C,5);
figure, imshow(bw2)
ComplementImage=imcomplement(bw2);
imshow(ComplementImage);
[labeledimage,numberofobject]=bwlabel(ComplementImage);
numberofobject
% bwareaopen is not working even if I increase 5 to another higher value

Akzeptierte Antwort

Image Analyst
Image Analyst am 20 Jan. 2015
Well of course not. You inverted the image so you have only one object - the background. Don't do that! Try this code:
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 = 20;
originalImage=imread('D:\Temporary stuff/1234.jpg');
subplot(2,2,1);
imshow(originalImage);
axis on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
BW = im2bw(originalImage,0.5);
subplot(2,2,2);
imshow(BW, []);
axis on;
promptMessage = sprintf('Do you want to crop the image,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
message = sprintf('Drag out a box in the upper right image\nthen double-click inside to finish');
uiwait(helpdlg(message));
croppedImage = imcrop(BW);
bw2 = bwareaopen(croppedImage, 9);
bw2 = imclearborder(bw2); % Get rid of huge white surround.
subplot(2,2,3);
imshow(bw2)
axis on;
[labeledimage, numberofobjects] = bwlabel(bw2);
numberofobjects
message = sprintf('The number of objects = %d', numberofobjects);
uiwait(helpdlg(message));
measurements = regionprops(labeledimage, 'Area');
allAreas = sort([measurements.Area], 'Descend')
  5 Kommentare
Image Analyst
Image Analyst am 21 Jan. 2015
This one needs to be inverted since it's not a binary image to start with like the original image you posted. The code is below and does give different numbers of objects if you vary the parameter to bwareaopen().
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 = 20;
originalImage=imread('D:\Temporary stuff/4-1.jpg');
subplot(2,2,1);
imshow(originalImage);
axis on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
BW = ~im2bw(originalImage,0.5);
subplot(2,2,2);
imshow(BW, []);
axis on;
promptMessage = sprintf('Do you want to crop the image,\nor Cancel to abort processing?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');
if strcmpi(button, 'Cancel')
return;
end
message = sprintf('Drag out a box in the upper right image\nthen double-click inside to finish');
uiwait(helpdlg(message));
croppedImage = imcrop(BW);
bw2 = bwareaopen(croppedImage, 9);
bw2 = imclearborder(bw2); % Get rid of huge white surround.
subplot(2,2,3);
imshow(bw2)
axis on;
[labeledimage, numberofobjects] = bwlabel(bw2);
numberofobjects
message = sprintf('The number of objects = %d', numberofobjects);
uiwait(helpdlg(message));
measurements = regionprops(labeledimage, 'Area');
allAreas = sort([measurements.Area], 'Descend')
AMIT VERMA
AMIT VERMA am 22 Jan. 2015
It worked ,You're the best thanks a ton

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Specifying Target for Graphics Output finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by