フィルターのクリア

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 Center および File ExchangeImage Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by