Filling a Bounding Box with zeros

5 ビュー (過去 30 日間)
Vijay
Vijay 2013 年 4 月 6 日
移動済み: DGM 2023 年 2 月 12 日
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??

採用された回答

Image Analyst
Image Analyst 2013 年 4 月 8 日
編集済み: Image Analyst 2016 年 10 月 8 日
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 件のコメント
Marco A. Acevedo Z.
Marco A. Acevedo Z. 2021 年 11 月 12 日
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 2021 年 11 月 13 日
@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().

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

その他の回答 (2 件)

Anand
Anand 2013 年 4 月 8 日
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 件のコメント
Vijay
Vijay 2013 年 4 月 8 日
移動済み: DGM 2023 年 2 月 12 日
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
Julio Cesar Garcia Navarro 2016 年 10 月 5 日
移動済み: DGM 2023 年 2 月 12 日
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.

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


VINSHI K K
VINSHI K K 2017 年 3 月 28 日
Hello, I need to fill bounding box region with ones..could you please suggest the code.
  3 件のコメント
Image Analyst
Image Analyst 2021 年 6 月 25 日
@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 2021 年 6 月 26 日
Thank you so much Sir @Image Analyst. Everyday I am learning a lot from you and Matlab community. I am really grateful.

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

Community Treasure Hunt

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

Start Hunting!

Translated by