In this post there was the same question
Fix bounding box in a image and save it
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi there,
I am trying to fix a Bounding Box permanently in a image because to save it.
I used as usual hold on for graphic the bounding box, but I want to fix it in a image for then save it.
As example:
% Obtain image
krillin = imread("https://es.mathworks.com/responsive_image/150/150/0/0/0/cache/matlabcentral/profiles/22817879_1627284404454.jpg");
% Process it
krillin_grey = im2gray(krillin);
krillin_grey_imadj = imadjust(krillin_grey);
krillin_BW = imbinarize(krillin_grey_imadj);
krillin_BW_2 = imclearborder(~krillin_BW);
% Boca
krillin_BW_2_largest_fin = bwpropfilt(krillin_BW_2, "Area", 1, "largest");
% Obtain the Bounding box
BB_krillboca=regionprops(krillin_BW_2_largest_fin,'BoundingBox'); %<--- rellevant
% Show in the image
imshow(krillin)
hold on
rectangle('Position', BB_krillboca.BoundingBox+[-1 -1 +1 +1], 'EdgeColor','b', 'LineWidth', 1)
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/928914/image.png)
And I want to save just the bounding box in a zeros image:
zeros_krillin = zeros(size(krillin_BW_2_largest_fin));
imshow(zeros_krillin)
hold on
rectangle('Position', BB_krillboca.BoundingBox+[-1 -1 +1 +1], 'EdgeColor','b', 'LineWidth', 1)
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/928919/image.png)
I tried exportgraphics but the size is not the same when it is saved.
Thank you so much
답변 (1개)
Saffan
2023년 6월 28일
I understand that you want to save an image along with the bounding box without changing its size. This can be accomplished in the following way:
- Get the figure using “getframe” function.
- Convert it to an image using “frame2im” function.
- Save it using “imwrite” function in the desired format.
Below is an example code snippet that demonstrates how to achieve this:
% Capture the figure with the drawn rectangle
f = getframe(gca);
% Convert it to an image
bounding_box_image = frame2im(f);
% Save the image with the bounding box
imwrite(bounding_box_image, 'bounding_box_image.png');
Refer to these links for more information:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!