필터 지우기
필터 지우기

How to add text to an image

조회 수: 58 (최근 30일)
Nikhil Hegde
Nikhil Hegde 2015년 11월 4일
답변: DGM 2022년 10월 21일
I've this code that adds a bounding box to all the cashews present in an image. The image is attached.
rgbImg = imread('White Wholes 500.jpg');
img = rgb2gray(rgbImg);
se = mmseline(2);
img1(:, :, 1) = mmasf(img, 'OC', se, 3);
T = uint8 (round( max(max(img1(:, :, 1))) * graythresh(img1(:, :, 1)) ));
X = mmthreshad (img1 (:, :, 1), T);
X = ~X;
X = imclose(X, true(5));
X = ~X;
stats = regionprops(bwlabel(X),'Area','Centroid','Perimeter','BoundingBox');
Boxes=cat(1,stats.BoundingBox);
maskm = false(size(X,1),size(X,2));
for k1 = 1:size(Boxes,1)
mask1 = false(size(X,1),size(X,2));
starty = round(Boxes(k1,1));
stopy = starty+round(Boxes(k1,3))-1;
startx = round(Boxes(k1,2));
stopx = startx+round(Boxes(k1,4))-1;
text(startx,starty,'Box1');
mask1(startx:stopx,starty:stopy) = true;
maskm = maskm + imdilate(edge(mask1,'sobel'),strel('disk',2));
end
maskm = maskm + X;
figure,imshow(maskm);
To label each box I can put it in a for loop I get that. I just wanted to know how I add text to a bounding box.I didn't understand the 'text' function in MATLAB so if your answer uses the 'text' function, then if you could please explain in brief the functionality then that would be great for me!

채택된 답변

Image Analyst
Image Analyst 2015년 11월 4일
You got x and y reversed. Confusing row,column with x,y (reversing the correct order) is a very common mistake. Bounding box is (x,y, width, height). You have startY = Boxes(k1,1) when it should be Boxes(k1, 2) since y is the second element, not the first.

추가 답변 (2개)

Thorsten
Thorsten 2015년 11월 4일
The text function just draws the text into the image at position x,y. The default text color is black. So if you want to draw text on black background, you have to specify a visible color, like
text(startx,starty,'Box1', 'Color', 'r');

DGM
DGM 2022년 10월 21일
For passers-by:
There are other ways to add text to an image without relying on using the figure as an ad-hoc compositor and potentially ruining the image resolution.

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by