Removing white pixels from background

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 个评论

If your image is always of the same structure with a black rectangle of pixels enclosing the area of interest then you can just find the row and column indices of the first and last black pixel in the image. Then create a mask out of everything outside of those extents and do whatever you want then with that mask.
I don't have the time to give code for that, but the process should be simple enough for an image such as that.
You can do it in 2d or even collapse your image to 1d, find the first and last black values in the 1d array and use
ind2sub
to get back your 2d coordinates for the corners.
Thanks for your reply. I want to work only in 2d because I have thousands of images and I want to process them in loops. Would you please write the code for that or just few instructions.
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

请先登录,再进行评论。

回答(2 个)

Adam
Adam 2015-11-13
编辑:Adam 2015-11-13
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.
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.

提问:

2015-11-13

Community Treasure Hunt

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

Start Hunting!

Translated by