Filling a Bounding Box with zeros

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??

 Respuesta aceptada

Image Analyst
Image Analyst el 8 de Abr. de 2013
Editada: Image Analyst el 8 de Oct. de 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 comentarios

akpojotor princewill
akpojotor princewill el 24 de Ag. de 2017
I followed this pattern. but when i display BW2 it doesn't show that the bounded regions have been replaced with 0's. How can I fix that
Image Analyst
Image Analyst el 24 de Ag. de 2017
Start your own new question and attach your code and image.
Shigeru Kato
Shigeru Kato el 23 de Ag. de 2021
As well, I confuse.
Even though size of image or anchor box is defiined as [row,col] i.e. [height, widht],
On the other hand, a rectangle form is defined as [x,y, width, height].
It is annoying me too much.
Image Analyst
Image Analyst el 23 de Ag. de 2021
@Shigeru Kato, yes I understand and share your pain. The (x,y)/(row/column) issue is something you always have to keep in mind. It's not just images, it's also a watchout for matrices in general.
Also the [x,y,width,height] used in rectangle() and imcrop() and other functions can sometimes be less convenient than if we had a [x1,y1,x2,y2] or [x1,x2,y1,y2] option.
Thank you very much for your response.
I made a script for confirmation of my understanding.
clear;
clc;
close all;
A = imread('peppers.png');
[row, col, dim] = size(A)
imshow(A);
hold on;
x = 50;
y = 300;
plot(x, y, '*b');
text(x,y, ' [x,y]', 'Color', 'b');
plot([x,x],[0,y], '--c');
plot([0,x],[y,y], '--g');
hold off;
Marco A. Acevedo Z.
Marco A. Acevedo Z. el 12 de Nov. de 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 el 13 de Nov. de 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().

Iniciar sesión para comentar.

Más respuestas (2)

Anand
Anand el 8 de Abr. de 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 comentarios

Vijay
Vijay el 8 de Abr. de 2013
Movida: DGM el 12 de Feb. de 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
Julio Cesar Garcia Navarro el 5 de Oct. de 2016
Movida: DGM el 12 de Feb. de 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.

Iniciar sesión para comentar.

VINSHI K K
VINSHI K K el 28 de Mzo. de 2017

0 votos

Hello, I need to fill bounding box region with ones..could you please suggest the code.

3 comentarios

kanika bhalla
kanika bhalla el 25 de Jun. de 2021
Editada: kanika bhalla el 25 de Jun. de 2021
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.
@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 el 26 de Jun. de 2021
Thank you so much Sir @Image Analyst. Everyday I am learning a lot from you and Matlab community. I am really grateful.

Iniciar sesión para comentar.

Categorías

Más información sobre Scripts en Centro de ayuda y File Exchange.

Preguntada:

el 6 de Abr. de 2013

Movida:

DGM
el 12 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by