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

15 views (last 30 days)
clear all
close all
% Example: Load image and define ROI
I = imread('image.jpg');
R = imresize(I, [800 1000]); % Resize image to full res
% Define a rectangular ROI
xmin = 450;
ymin = 350;
width = 100;
height = 100;
roi_mask = zeros(size(R, 1), size(R, 2)); % Create a mask
roi_mask(ymin:ymin+height-1, xmin:xmin+width-1) = true;
% Calculate mean intensity within the ROI
roi_intensity_value = mean(R(roi_mask));
% 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?

Answers (1)

Manikanta Aditya
Manikanta Aditya on 10 Jul 2024
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
I = imread('image.jpg');
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
roi_mask = false(size(R, 1), size(R, 2)); % Create a mask
roi_mask(ymin:ymin+height-1, xmin:xmin+width-1) = true;
% Calculate mean intensity within the ROI
roi_intensity_value = mean(R(roi_mask(:)));
% 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!
  2 Comments
Gabriel
Gabriel on 10 Jul 2024
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
Manikanta Aditya on 10 Jul 2024
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
I = imread('image.jpg');
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
roi_mask = false(size(R, 1), size(R, 2)); % Create a mask
roi_mask(ymin:ymin+height-1, xmin:xmin+width-1) = true;
% Calculate mean intensity within the ROI
roi_intensity_value = mean(R(roi_mask(:)));
% 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!

Sign in to comment.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!