Need help to overlay transparent red on gray scale images (or movie) using a binary mask
Show older comments
Hi!
I have gray scale data with objects, to simplify in this case the 5's belong to the object e.g.:
[1, 1, 1, 1, 1, 1;
1, 1, 5, 5, 1, 1;
1, 1, 5, 5, 1, 1;
1, 1, 1, 1, 1, 1]
(In realitity the image is larger, with different values, multiple images (movie), and more objects)
Anyway I also have a mask that matches with the objects visible. e.g.:
[0, 0, 0, 0, 0, 0;
0, 0, 1, 1, 0, 0;
0, 0, 1, 1, 0, 0;
0, 0, 0, 0, 0, 0]
I would like to overlay a red transparent blur over the original image to highlight the objects using the mask.
I have tried some stuff, but the resulting images/movie turns out to be all white.
What I tried is to add RBG channels and copying the original data in each of them, and then modify the red channel by using the mask. Can anyone help me?
Accepted Answer
More Answers (2)
Hi, you can take the following steps to overlay a red transparent blur over the original grayscale image using the provided mask:
1. Define the original grayscale image and the mask.
- `gray_image` represents the grayscale image with objects. It is a 4x6 matrix in this example.
- `mask` represents the mask that matches the objects visible. It has the same size as `gray_image`.
2. Create an RGB image from the grayscale data.
- `rgb_image` is a 4x6x3 matrix created using the `cat` function. The three color channels (red, green, blue) are initialized with the same values as the grayscale image.
3. Apply the mask to the red channel of the RGB image.
- `red_channel` is extracted from `rgb_image` using `rgb_image(:,:,1)`.
- Wherever the mask is 1, the corresponding pixels in the red channel are set to 255 (maximum intensity), highlighting the objects.
4. Apply a blur to the modified red channel to create a transparent effect.
- The `imgaussfilt` function is used to apply a Gaussian blur to the `red_channel`, creating a smooth and blurred effect.
- The `blur_radius` parameter determines the extent of the blur. In this example, a radius of 5 is used.
5. Combine the modified red channel with the original green and blue channels.
- The modified red channel `blurred_red` is assigned back to the red channel of `rgb_image`, replacing the original values.
- The green and blue channels remain unchanged.
6. Display the resulting image.
- The `imshow` function is used to display the final RGB image with the overlay of the red transparent blur.
By following these steps, the code creates an image where the objects specified by the mask are highlighted with a red transparent blur effect, overlaying the original grayscale image.
% Define the original grayscale image and the mask
gray_image = [1, 1, 1, 1, 1, 1;
1, 1, 5, 5, 1, 1;
1, 1, 5, 5, 1, 1;
1, 1, 1, 1, 1, 1];
mask = [0, 0, 0, 0, 0, 0;
0, 0, 1, 1, 0, 0;
0, 0, 1, 1, 0, 0;
0, 0, 0, 0, 0, 0];
% Create an RGB image from the grayscale data
rgb_image = cat(3, gray_image, gray_image, gray_image);
% Apply the mask to the red channel of the RGB image
red_channel = rgb_image(:,:,1);
red_channel(mask == 1) = 255; % Set the value to 255 (maximum intensity) where the mask is 1
% Apply a blur to the modified red channel to create a transparent effect
blur_radius = 5; % Adjust the radius as needed
blurred_red = imgaussfilt(red_channel, blur_radius);
% Combine the modified red channel with the original green and blue channels
rgb_image(:,:,1) = blurred_red;
% Display the resulting image
imshow(rgb_image);
1 Comment
Blurring part of an image doesn't make it transparent. It just makes it blurry.
gray_image = imread('peppers.png');
gray_image = im2gray(gray_image); % image is presumed to be uint8, single-channel
mask = imread('redpepmask.png');
mask = mask > 128; % mask is presumed to be unit-scale
% Create an RGB image from the grayscale data
rgb_image = cat(3, gray_image, gray_image, gray_image);
% Apply the mask to the red channel of the RGB image
red_channel = rgb_image(:,:,1);
red_channel(mask == 1) = 255; % Set the value to 255 (maximum intensity) where the mask is 1
% Apply a blur to the modified red channel to create a transparent effect
blur_radius = 5; % Adjust the radius as needed
blurred_red = imgaussfilt(red_channel, blur_radius);
% Combine the modified red channel with the original green and blue channels
rgb_image(:,:,1) = blurred_red;
% Display the resulting image
imshow(rgb_image);
It's not just the red overlay that's blurred; it's the entire red channel of the original image, which will cause red-cyan halos around all the edges in the rest of the image.
% zoom in on the image to show the edge effects
sample = imcrop(rgb_image,[149 144 183 140]);
figure
imshow(sample)
If the image region is already white, then the result will not be red, but white (as your own example demonstrates; yes, that big blank region is an all-white image). You're not doing any compositing. You're merely doing what's equivalent to a max() operation.
Image Analyst
on 1 Jul 2023
0 votes
Or see these links:
Categories
Find more on Image Processing Toolbox 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!




