Filling a Bounding Box with zeros

5 views (last 30 days)
Vijay
Vijay on 6 Apr 2013
Moved: DGM on 12 Feb 2023
I am trying to Replace an boxed region with all 0's in a binary image
BW2 is the image & the object lies at
[642.5000 1.5000 55.0000 82.0000] (from regionprops-BoundingBox)
BW2(642.5:697.5,1.5:82.5)=zeros(55,82);-gives"Subscripted assignment dimension mismatch."
BW2(642.5:697.5,1.5:82.5)=0;-Doesn't replace the regions with zeros
BW2(642.5:697.5,1.5:82.5)=[0];-Doesn't replace the regions with zeros
How to do this Change all the pixels to zero with in that box??

Accepted Answer

Image Analyst
Image Analyst on 8 Apr 2013
Edited: Image Analyst on 8 Oct 2016
You can't use fractional indexes. Keep in mind that bounding box goes outside the pixels, and the pixel has it's center at integer indexes, so to be outside, you need to be a half pixel away from the center. So you need to do ceil() on the left and top, and do floor() on the right and bottom edges.
The main problem is that you made the very common beginner mistake of thinking (x,y) = (row, column). It does not. The bounding box from regionprops() returns [x, y, width, height], NOT [row, column, height, width]. You took the first argument x (the horizontal/column location) and used it as the vertical/row/first index instead of the horizontal/column/second index. You can't do that. You can do it correctly this way:
bbox = props.BoundingBox;
x1 = ceil(bbox(1));
x2 = round(x1 + bbox(3));
y1 = ceil(bbox(2));
y2 = round(y1 + bbox(4));
BW2(y1:y2, x1:x2) = 0; % NOTE : y comes first, NOT x!!!
Note that what you did was essentially
BW2(x1:x2, y1:y2) = 0; % This is wrong.
  7 Comments
Marco A. Acevedo Z.
Marco A. Acevedo Z. on 12 Nov 2021
I would like to point out that using the technique outline above is the best option. However, if using imcrop() with the bounding box, you will have 1 more pixel (in columns) for some reason. Maybe, the rounding of .5 values is different in the inputs of imcrop() since I had converted strings to double to define the bounding box. So, I recommend the solution of Image Analyst. Bye.
Image Analyst
Image Analyst on 13 Nov 2021
@Marco A. Acevedo Z., imcrop() is meant to have rectangles, like the boundingBVox returned from regionprops(), that are a half pixel outside the region to be cropped. It's not meant to work on integer indexes. I know it's confusing but that's the way it is. If you want to crop using integer indexes, simply using indexing, not imcrop().

Sign in to comment.

More Answers (2)

Anand
Anand on 8 Apr 2013
Why does this not work?
BW2(642:642+56,1:1+83) = 0;
Give us something more than "Doesn't replace the region with zeros".
  2 Comments
Vijay
Vijay on 8 Apr 2013
Moved: DGM on 12 Feb 2023
it gives a larger image than the input image 1536X2048 becomes 2048X2048 and that also filled filled improperly column wise improperly.. so i have started using ismember function for the condition and to remove the objects not satisfying the condition for text extraction using the bounding box criteria..
Julio Cesar Garcia Navarro
Moved: DGM on 12 Feb 2023
Hello,
I tried the:
BW2(642:642+56,1:1+83) = 0;
And it indeed changes the size of the image; however, if you transpose the values, you will get the correct Bounding Box filled without modifying the image size. Thus:
BW2(1:1+83,642:642+56) = 0;
Will be correct.

Sign in to comment.


VINSHI K K
VINSHI K K on 28 Mar 2017
Hello, I need to fill bounding box region with ones..could you please suggest the code.
  3 Comments
Image Analyst
Image Analyst on 25 Jun 2021
@kanika bhalla,I think you meant this for setting the pixels in your images:
Colorimage(y1:y2, x1:x2, :) = 255; % Note if your image is 3-D and RGB
Binaryimage(y1:y2, x1:x2) = true; % if your image is 2-D binary (logical). I mentioned this because to solve this error costs me 7 hours of debugging.
Also, it's probably not a good idea to name your vairable Image since there is a built in function called image(). True, MATLAB is case sensitive so it wouldn't overwrite the image functions, but nonetheless it's probably best not to have variables with names like functions. Also your code finds (potentially) many bounding boxes but you only assign the last one to white. If you want the others, then that code should be inside your for loop.
kanika bhalla
kanika bhalla on 26 Jun 2021
Thank you so much Sir @Image Analyst. Everyday I am learning a lot from you and Matlab community. I am really grateful.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!