# Obtain intensity value in an ROI on an image, and output the image with the displayed intensity value inside of the ROI on the image

28 views (last 30 days)
Gabriel on 10 Jul 2024 at 12:59
Commented: Manikanta Aditya on 10 Jul 2024 at 14:26
clear all
close all
% Example: Load image and define ROI
R = imresize(I, [800 1000]); % Resize image to full res
% Define a rectangular ROI
xmin = 450;
ymin = 350;
width = 100;
height = 100;
% Calculate mean intensity within the ROI
% Convert mean intensity to string
mean_intensity_str = sprintf('Mean Intensity: %.2f', roi_intensity_value);
% Position for text
text_position = [x, y - 20]; % Adjusted position above ROI
% Write text onto the image
img_with_text = insertText(R, text_position, mean_intensity_str, ...
'FontSize', 12, 'TextColor', 'red', 'BoxColor', 'white', 'BoxOpacity', 0.8);
% Display the image with text
imshow(img_with_text);
% Optionally, save the image with the text
imwrite(img_with_text, 'image_with_mean_intensity.png');
Hello. I am receiving an error message with the line "roi_intensity_value = mean(R(roi_mask));". Does anyone know why?

Manikanta Aditya on 10 Jul 2024 at 13:09
The error is likely occurring because the roi_mask is of type logical, and R(roi_mask) is trying to index into R using this logical mask, which may not be properly handled by the mean function. To address this, you can convert the mask to linear indices and then compute the mean intensity value.
clear all
close all
% Example: Load image and define ROI
R = imresize(I, [800 1000]); % Resize image to full res
% Define a rectangular ROI
xmin = 450;
ymin = 350;
width = 100;
height = 100;
% Create a mask for the ROI
% Calculate mean intensity within the ROI
% Convert mean intensity to string
mean_intensity_str = sprintf('Mean Intensity: %.2f', roi_intensity_value);
% Position for text (adjusted position above ROI)
text_position = [xmin, ymin - 20];
% Write text onto the image
img_with_text = insertText(R, text_position, mean_intensity_str, ...
'FontSize', 12, 'TextColor', 'red', 'BoxColor', 'white', 'BoxOpacity', 0.8);
% Display the image with text
imshow(img_with_text);
% Optionally, save the image with the text
imwrite(img_with_text, 'image_with_mean_intensity.png');
Hope this helps!
Gabriel on 10 Jul 2024 at 14:10
Okay this is great. I forgot to include that I want to draw the ROI onto the image and have the text inside of the drawn ROI. How would I go about that?
Manikanta Aditya on 10 Jul 2024 at 14:26
To draw the ROI onto the image and place the text inside the drawn ROI, you can use the insertShape function to draw the rectangle and adjust the text position to be inside the ROI.
Here is the code:
clear all
close all
% Example: Load image and define ROI
R = imresize(I, [800 1000]); % Resize image to full res
% Define a rectangular ROI
xmin = 450;
ymin = 350;
width = 100;
height = 100;
% Create a mask for the ROI
% Calculate mean intensity within the ROI
% Convert mean intensity to string
mean_intensity_str = sprintf('Mean Intensity: %.2f', roi_intensity_value);
% Position for text (inside the ROI)
text_position = [xmin + 5, ymin + 5]; % Slightly offset from the top-left corner of the ROI
% Draw the ROI on the image
img_with_roi = insertShape(R, 'Rectangle', [xmin, ymin, width, height], ...
'LineWidth', 2, 'Color', 'yellow');
% Write text onto the image inside the ROI
img_with_text = insertText(img_with_roi, text_position, mean_intensity_str, ...
'FontSize', 12, 'TextColor', 'red', 'BoxColor', 'white', 'BoxOpacity', 0.8);
% Display the image with text and ROI
imshow(img_with_text);
% Optionally, save the image with the text and ROI
imwrite(img_with_text, 'image_with_mean_intensity_and_roi.png');
Hope this helps!