Remove unwanted part of image, to count objects

조회 수: 8 (최근 30일)
Altina Rexha
Altina Rexha 2020년 9월 26일
댓글: Image Analyst 2020년 9월 26일
code:
y=ind2gray(pic, bmap);
a=y>120;
imshow(a);
I only want to count the 3 rows of circles, even through the first ones are half.
How is it possible to do it while there are alot of other things in there?
  댓글 수: 2
Matt J
Matt J 2020년 9월 26일
Could you just use imfreehand() to draw a mask around the objects you want to keep?
Altina Rexha
Altina Rexha 2020년 9월 26일
mm I dont really know, because i have to create a function to count the circles

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

답변 (1개)

Image Analyst
Image Analyst 2020년 9월 26일
편집: Image Analyst 2020년 9월 26일
Do you know that that segmentation is the best you could do? Can you show us the original photo?
Do you know that they will always be in the lower half of the image? If so, you could make it easier by erasing the top half:
mask(1:rows/2, :) = false;
Do you know that there are always 28 of them? If so you could use
mask = bwareafilt(mask, 28, 4); % Take 28 largest 4-connected blobs.
Can you guarantee that no blob touching the border is an oval? If so you can get rid of border clutter with
mask = imclearborder(mask);
Do you know if all the ovals are in a certain size range? If so you could also use bwareafilt() or bwareaopen().
  댓글 수: 2
Altina Rexha
Altina Rexha 2020년 9월 26일
Original photo
Image Analyst
Image Analyst 2020년 9월 26일
It's possible background subtraction would be a good way. But you have to be able to answer yes to these two questions
  1. Is the lighting level fairly stable?
  2. Can you get a blank shot" with no potatoes or rolls/buns in the field of view?
If so, then just subtract the blank image from the actual image using imabsdiff() and then threshold. You may still need to do a little cleanup with morphological functions like bwareafilt() or bwareaopen().
Otherwise you can try color segmentation using the Color Thresholder on the Apps tab of the tool ribbon to export a function that will do color segmentation.
It looks like you can mask off certain parts of the image image immediately, like the bar at the top of the image.
Here's a start, though you may want to look at watershed to separate them, if the objects are touching.
% Demo to find buns leaving the oven. By Image Analyst, Sep 26, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'image.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% It's not an RGB image! It's an indexed image, so read in the indexed image...
[img, map] = imread(fullFileName);
% and then convert it, using the stored colormap, to an RGB image.
rgbImage = ind2rgb(img, map);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Get color we want.
[mask, maskedRGBImage] = createMask(rgbImage);
% Erase everything down to line 270
mask(1:270, :) = false;
% Display the initial mask image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
title('Initial Color Segmentation Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Fill holes
mask = imfill(mask, 'holes');
props = regionprops(mask, 'area');
allAreas = sort([props.Area], 'descend')
% Get rid of small blobs less than 500 pixels in area.
mask = bwareaopen(mask, 500);
% Display the final mask image.
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Display the final mask image.
subplot(2, 2, 4);
imshow(maskedRgbImage, []);
axis('on', 'image');
title('Final Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 26-Sep-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.000;
channel1Max = 0.218;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.430;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.575;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end

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

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by