Removing white pixels from background

조회 수: 14 (최근 30일)
Mohammad
Mohammad 2015년 11월 13일
답변: Image Analyst 2015년 11월 13일
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
Mohammad
Mohammad 2015년 11월 13일
편집: Image Analyst 2015년 11월 13일
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 2015년 11월 13일
That won't do it.

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

답변 (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.

Image Analyst
Image Analyst 2015년 11월 13일
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.

Community Treasure Hunt

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

Start Hunting!

Translated by