Removing white pixels from background

3 visualizaciones (últimos 30 días)
Mohammad
Mohammad el 13 de Nov. de 2015
Respondida: Image Analyst el 13 de Nov. de 2015
If I have the following (binary) image and I want to remove the surrounding white pixels (or change these white pixels into black)
the resulted image should be like the followings
or like this one:
  4 comentarios
Mohammad
Mohammad el 13 de Nov. de 2015
Editada: Image Analyst el 13 de Nov. de 2015
thanks I solved this problem using the following code:
[r_min, c_min]=find(binaryImage==0,1,'first')
[r_max, c_max]=find(binaryImage==0,1,'last')
binaryImage(:,c_max+1)=0
binaryImage(:,c_min-1)=0
Image Analyst
Image Analyst el 13 de Nov. de 2015
That won't do it.

Iniciar sesión para comentar.

Respuestas (2)

Adam
Adam el 13 de Nov. de 2015
Editada: Adam el 13 de Nov. de 2015
Something like this should work, using an example image without anything inside the black rectangle, but that is irrelevant so long as the black rectangle encloses what you are interested in:
im = zeros( 20 );
im( 4:14, 3:18 ) = ones( 11, 16 );
[topLeftRow, topLeftCol] = ind2sub( size( im ), find( im(:) == 1, 1 ) );
[bottomLeftRow, bottomLeftCol] = ind2sub( size( im ), find( im(:) == 1, 1, 'last' ) );
[x, y]= meshgrid( topLeftRow:bottomLeftRow, topLeftCol:bottomLeftCol );
idx = sub2ind( size( im ), x, y );
mask = ones( size( im ) );
mask( idx ) = 0;
That gives you a mask (ones) for the white border which you can use to do whatever you want to the image afterwards.
I imagine doing it this way is faster than messing about in 2d finding the relevant points on the image, but someone else may have a more efficient approach.

Image Analyst
Image Analyst el 13 de Nov. de 2015
Try this (adapted from your "solution"):
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;
grayImage = imread('pic1.png');
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
if numberOfColorBands > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
binaryImage = grayImage > 128;
subplot(3,1,1);
imshow(binaryImage);
title('Original Image', 'FontSize', fontSize);
binaryImage1 = binaryImage;
[r_min, c_min]=find(binaryImage1 == 0,1,'first');
[r_max, c_max]=find(binaryImage1 == 0,1,'last');
binaryImage1(:,c_max+1)=0;
binaryImage1(:,c_min-1)=0;
subplot(3,1,2);
imshow(binaryImage1);
title('Your Way', 'FontSize', fontSize);
% Try it my way instead
binaryImage1 = binaryImage;
[r_min, c_min]=find(binaryImage1 == 0,1,'first');
[r_max, c_max]=find(binaryImage1 == 0,1,'last');
binaryImage1(:,c_max+1:end)=0;
binaryImage1(:,1:c_min-1)=0;
binaryImage1(1:r_min-1,:)=0;
binaryImage1(r_max:end,1:end)=0;
subplot(3,1,3);
imshow(binaryImage1);
title('My Way', 'FontSize', fontSize);
There is a more compact way to use regionprops() to get the BoundingBox, then to use imcrop(). Let me know if you want to see that.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by