Determining values in array (of 1s and 0s) that neighbour similar values

3 vues (au cours des 30 derniers jours)
Jack Williams
Jack Williams le 11 Août 2019
Commenté : Image Analyst le 11 Août 2019
Background:
error = (abs(P1) - abs(P2) );
error = abs(error);
error( error > threshold) = 1;
error( error < threshold) = 0;
error(sqrt(xx.^2 + yy.^2) >= radius) = 1;
error = ones(conf.resolution)-error;
I have a means of determining the error between 2 arrays P1 and P2.
Error values within a given threshold are set to 1 and error values above the given threshold are set to 0, which are represented by black and grey respectively on the image below.
Question:
My main aim is to remove the 'noise' from the image, such that the remaining 1 values are only those that are dense populated in the center of the image
Thanks very much.

Réponse acceptée

John D'Errico
John D'Errico le 11 Août 2019
Modifié(e) : John D'Errico le 11 Août 2019
As an alternative to a tool like imerode, you can just use conv2. The minor virtue is that conv2 lives in MATLAB proper, with no neeed for a toolbox.
It sounds like you are asking, if an element of a binary array is a 1, AND it is surrounded by elements that are also 1's, then leave that element as a 1. Otherwise, set it to zero.
For example, while I don't have your array, here is a trivial example. So a binary, 100x100 array.
A = double(rand(100,100) > 0.5);
B = double(conv2(A,ones(3,3),'same') == 9);
spy(B)
B is now a new array, that is equal to 1 only where an element is surrounded by 8 other ones.
If you were willing to accept the case where the fill was slightly less complete, it would be almost as easy.
B = double((conv2(A,ones(3,3),'same') >= 8) & A);
spy(B)
untitled.jpg
So in the second case, I accepted elements that were 1 in the center, AND had at least 7 elements that were true surrounding it.
  2 commentaires
Jack Williams
Jack Williams le 11 Août 2019
Thank you! Smart man
John D'Errico
John D'Errico le 11 Août 2019
This sort of trick with conv2 is very useful in other places, so I won't claim to have invented it myself. For example, in the game of life (the JH Conway game) you need to count how many neighbors of an element are 1. There you only care about the case where it is exactly 2 or 3.

Connectez-vous pour commenter.

Plus de réponses (2)

Bruno Luong
Bruno Luong le 11 Août 2019
If you have Image Processing tbx you might take a look at imerode

Image Analyst
Image Analyst le 11 Août 2019
Your largest central blob is actually connected to several other blobs as you can see in the lower left image. So you can't simply take the largest blob. Now this is determined by your error message and how you decided on the threshold. It's possible a different threshold would lead to disconnected central blob.
So now the question you need to answer is how far you want to go out from the central blob. You can use imerode to disconnect the central blob, then use imdilate to grow it back to the same original size (but slightly different shape). Eroding an amount and dilating the same amount is what the imopen() function does. Or you could simply use a circular mask (see the FAQ) to go out as far as you want to include more of the connected other blobs.
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;
%===============================================================================
% Read in gray scale demo image.
% Let's let the user select from a list of all the demo images that ship with the Image Processing Toolbox.
folder = pwd;
baseFileName = 'image.png';
% Get the full filename, with path prepended.
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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% 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(rgbImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Make a mask.
binaryImage = grayImage == 0;
% Display the image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
mask = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
title('Largest Blob', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Do a opening on it.
se = strel('disk', 2, 0);
% Disconnect the blob in the middle from the others.
mask2 = imopen(binaryImage, se);
% Take the largest of what's left
mask2 = bwareafilt(mask2, 1);
% Erase outside of this from the original mask.
binaryImage(~mask2) = false;
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
0000 Screenshot.png
  1 commentaire
Image Analyst
Image Analyst le 11 Août 2019
You should not use the built-in function error() to be the name of your variable. You could be sorry.
Since you seem to have some radii in mind, like when you set everything to 1 outside some radius
error(sqrt(xx.^2 + yy.^2) >= radius) = 1;
Why don't you do the same thing with a different radii?
myErrors(sqrt(xx.^2 + yy.^2) >= radius2) = 1;
myErrors = bwareafilt(myErrors, 1); % Take largest blob only.
Why do you want the central portion anyway? Doesn't the actual value of the errors matter at all? Why are you thresholding the errors?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by