Filling a Bounding Box with zeros
Show older comments
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
More Answers (2)
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
Julio Cesar Garcia Navarro
on 5 Oct 2016
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.
VINSHI K K
on 28 Mar 2017
0 votes
Hello, I need to fill bounding box region with ones..could you please suggest the code.
3 Comments
kanika bhalla
on 25 Jun 2021
Edited: kanika bhalla
on 25 Jun 2021
Hi @VINSHI K K
I am not sure whether you need to know this or not. But I was just looking at this post. So, read your comment.
Here is a solution which I tried now.
Colorimage=imread('1.jpg')
Binaryimage=imread(binary.jpg) % C is a binary image
info = regionprops(binary,'Boundingbox') ;
hold on
for k = 1 : length(info)
BB = info(k).BoundingBox;
rectangle('Position', [BB(1),BB(2),BB(3),BB(4)],'EdgeColor','r','LineWidth',2) ;
x= BB(1)
y=BB(2)
width= BB(3)
height= BB(4)
end
hold off;
figure; imshow(Colorimage); % if you want same bounding box in colored image
x= BB(1)
y=BB(2)
width= BB(3)
height= BB(4)
hold on;
rectangle('Position',[x,y,width,height],...
'EdgeColor', 'r',...
'LineWidth', 3,...
'LineStyle','-')
hold off;
% Below is the method to make your bounding box object to 255.
x1 = ceil(x);
x2 = round(x1 + width);
y1 = ceil(y);
y2 = round(y1 + height);
Image(y1:y2,x1:x2,:) = 255 % Note if your image is RGB
Image(y1:y2,x1:x2) = 255 % if your image is binary. I mentioned this because to solve this error costs me 7 hours of debugging.
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
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.
Categories
Find more on Scripts in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!