clear an area in binary image

Hi guys,
I need to clear area in a binary image.
I only need the center part of a binary image, for example, only the center 100x100 image out of 1000x1000 image.
I know there is an easy way, prob just 1-2 line of code.
please show me your talent.
good day
C

 採用された回答

Walter Roberson
Walter Roberson 2011 年 8 月 14 日

0 投票

W = 100; %window size
img((end-W)/2+1:(end+W)/2, (end-W)/2+1:(end+W)/2) = 0;
Note: this will not work if your window size is even and your image size is odd, or if your window size is odd and your image size is even: in such cases, "center" is not well-defined.

4 件のコメント

charlie
charlie 2011 年 8 月 14 日
thank you, what if this is not the center 100x100, but for example, random offset at (x,y) with a window size of 400x400.
Image Analyst
Image Analyst 2011 年 8 月 15 日
charlie, the first index is the rows, the second is the columns. The colon notation means startingValue:endingValue. For example img(row1:row2, col1:col2) = false. Now, knowing this, don't you think you could figure it out? This is stuff you learn about during the first hour or two of learning MATLAB - have you gone over the "Getting Started" section of the help?
Walter Roberson
Walter Roberson 2011 年 8 月 15 日
W = 400;
if islogical(img)
t = false(size(img));
else
t = zeros(size(img),class(img));
end
t((x-W)/2+1:(x+W)/2,(y-W)/2+1:(y+W)/2) = img((x-W)/2+1:(x+W)/2,(y-W)/2+1:(y+W)/2));
Note: there are variations of the above that may be a bit clearer or shorter if you can guarantee that the img will not be of type "logical" and will not be of type "int64" or "uint64", or if you can guarantee that the code will only be used in R2011A or later.
charlie
charlie 2011 年 8 月 15 日
thank you for your help. in the mean time, I will get more familiar with Matlab.
Have a good day.
Charlie

サインインしてコメントする。

その他の回答 (1 件)

bym
bym 2011 年 8 月 14 日

0 投票

img=rand(1000)<.5; % some data
img(451:550,451:550); % center 100x100

2 件のコメント

charlie
charlie 2011 年 8 月 14 日
thanks, i want to know how to set everything apart the cetner 100x100 to zero. not the center 100x100.
how about with a random shift.
charlie
charlie 2011 年 8 月 14 日
a random shift of (x,y)

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by