I would like to change image A into image B using Matlab. I only need that specific region, shown in B, located between green and red contours. Can you please help?

 Akzeptierte Antwort

Image Analyst
Image Analyst am 9 Sep. 2020

0 Stimmen

Since it's not clear where the boundary of the region is in the upper left, I suggest you manually trace it out. I'm attaching a demo.

6 Kommentare

Gobert
Gobert am 10 Sep. 2020
Bearbeitet: Gobert am 10 Sep. 2020
Thank you for the demo! The boundaries of interest are delineated by those red and green contours. The only area of interest is between red and green contours (as shown in B part). The remaining areas must be turned into "white" or "black". Do you have any suggestions that can lead to extracting that area of interest automatically or simply changing A into B as shown above (in B part)?
Oh, so the red and green contours have already been "delineated" and are already there? Burned into the image? Well then it's easy. Just
% Split the image into color channels.
[r, g, b] = imsplit(rgbImage);
% Then find a green and red mask
greenMask = (r == 0) & (g == 255) & (b == 0);
redMask = (r == 255) & (g == 0) & (b == 0);
% Then fill the masks
greenMask = imfill(greenMask, 'holes');
redMask = imfill(redMask, 'holes');
% Now erase the red mask from within the green mask, leaving the donut.
finalMask = greenMask; % Initialize.
finalMask(~redMask) = false; % Erase the red mask.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(finalMask, 'like', rgbImage));
Gobert
Gobert am 10 Sep. 2020
@Image Analyst. Here's the output from the above code.
That's because your green and red were not pure green and red, like with values of 255. They were "greenish" and "reddish". Here, try this:
% Demo to find distances. By Image Analyst, May 5, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = 'image.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image.
subplot(4, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Split the image into color channels.
[r, g, b] = imsplit(rgbImage);
% Then find a green and red mask
threshold1 = 100;
threshold2 = 150;
greenMask = (r <= threshold1) & (g >= threshold2) & (b <= threshold1);
redMask = (r >= threshold2) & (g <= threshold1) & (b <= threshold1);
% Display the mask images.
subplot(4, 2, 2);
imshow(greenMask, []);
axis('on', 'image');
title('Green Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(4, 2, 3);
imshow(redMask, []);
axis('on', 'image');
title('Red Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Then fill the masks
greenMask = imfill(greenMask, 'holes');
redMask = imfill(redMask, 'holes');
% Take largest blob only
greenMask = bwareafilt(greenMask, 1);
redMask = bwareafilt(redMask, 1);
% Display the mask images.
subplot(4, 2, 4);
imshow(greenMask, []);
axis('on', 'image');
title('Green Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
subplot(4, 2, 5);
imshow(redMask, []);
axis('on', 'image');
title('Red Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Now erase the red mask from within the green mask, leaving the donut.
finalMask = greenMask; % Initialize.
finalMask(redMask) = false; % Erase the red mask.
subplot(4, 2, 6);
imshow(finalMask, []);
axis('on', 'image');
title('Final Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Mask the image using bsxfun() function to multiply the mask by each channel individually. Works for gray scale as well as RGB Color images.
maskedRgbImage = bsxfun(@times, rgbImage, cast(finalMask, 'like', rgbImage));
% Display the mask images.
subplot(4, 2, 7:8);
imshow(maskedRgbImage, []);
axis('on', 'image');
caption = sprintf('Masked Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
Gobert
Gobert am 11 Sep. 2020
This is great! Thank you! @Image Analyst
Then please "Accept this answer" if you think we're done here and don't think you'll get any better/more answers. Thanks in advance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by