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??
채택된 답변
추가 답변 (2개)
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
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
2017년 3월 28일
0 개 추천
Hello, I need to fill bounding box region with ones..could you please suggest the code.
댓글 수: 3
kanika bhalla
2021년 6월 25일
편집: kanika bhalla
2021년 6월 25일
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
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
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.
카테고리
도움말 센터 및 File Exchange에서 Scripts에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!