How can i delete a selected portion in an image and then paint it green?

4 ビュー (過去 30 日間)
Sandeep V
Sandeep V 2015 年 4 月 4 日
コメント済み: Image Analyst 2017 年 10 月 15 日
I am doing a work on inpainting procedure, for that i am in requirement of a code that deletes a selected portion in an image and colors that region green i.e.(RGB =0 255 0)? Can anybody help

採用された回答

Image Analyst
Image Analyst 2015 年 4 月 4 日
For a freehand drawn method, see the full demo below:
% Demo to have the user freehand draw an irregular shape over a color image.
% Then it creates a new image where the drawn region is all green inside the region and untouched outside the region,
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB gray scale demo image.
folder = fileparts(which('onion.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'onion.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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);
imshow(rgbImage, []);
axis on;
title('Original Color Image', 'FontSize', fontSize);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
% Ask user to draw freehand mask.
message = sprintf('Left click and hold to begin drawing.\nSimply lift the mouse button to finish');
uiwait(msgbox(message));
hFH = imfreehand(); % Actual line of code to do the drawing.
% Create a binary image ("mask") from the ROI object.
binaryImage = hFH.createMask();
xy = hFH.getPosition;
% Now make it smaller so we can show more images.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis on;
drawnow;
title('Original gray scale image', 'FontSize', fontSize);
% Display the freehand mask.
subplot(2, 2, 2);
imshow(binaryImage);
axis on;
title('Binary mask of the region', 'FontSize', fontSize);
% Get coordinates of the boundary of the freehand drawn region.
structBoundaries = bwboundaries(binaryImage);
xy=structBoundaries{1}; % Get n by 2 array of x,y coordinates.
x = xy(:, 2); % Columns.
y = xy(:, 1); % Rows.
subplot(2, 2, 1); % Plot over original image.
hold on; % Don't blow away the image.
plot(x, y, 'LineWidth', 2);
drawnow; % Force it to draw immediately.
% Burn region as green into image by setting it to [0, 255, 0] wherever the mask is true.
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Assign colors within each color channel individually.
redChannel(binaryImage) = 0;
greenChannel(binaryImage) = 255;
blueChannel(binaryImage) = 0;
% Recombine separate masked color channels into a single, true color RGB image.
maskedRgbImage = cat(3, redChannel, greenChannel, blueChannel);
burnedImage(binaryImage) = 255;
% Display the image with the mask "burned in."
subplot(2, 2, 3);
imshow(maskedRgbImage);
axis on;
caption = sprintf('Masked green inside region');
title(caption, 'FontSize', fontSize);
% Report results.
message = sprintf('Done with demo');
msgbox(message);
  3 件のコメント
Khaulah Zia
Khaulah Zia 2017 年 10 月 15 日
Actually i want to replace all the lights seen in an image by a white blob. I tried a for loop but in each loop the changes made in the previous loop is removed. At the end i am left with a single blob that i placed in the very last loop.
Image Analyst
Image Analyst 2017 年 10 月 15 日
You need to keep a cumulative mask and OR in the latest mask, like
thisMask = imfreehand(........
masterMask = masterMask | thisMask;
Make sure you set masterMask to false(rows, columns) before you start the loop.
masterMask = false(rows, columns);
Or you can initialize it in the loop on the first iteration
if k == 1
masterMask = thisMask;
else
masterMask = masterMask | thisMask;
end

サインインしてコメントする。

その他の回答 (1 件)

Geoff Hayes
Geoff Hayes 2015 年 4 月 4 日
Sandeep - if you know the region of the region that needs to be coloured green (and if we assume that the region is rectangular) then you could try something like
% create a black image
myImg = zeros(400,400,3);
% colour the top left-corner
greenPatch = zeros(200,200,3);
greenPatch(:,:,2) = 255;
myImg(1:200,1:200,:) = greenPatch;
% display the image
image(uint8(myImg));
Note that we size the green patch to be such that it will fill the deleted portion of myImg.

Community Treasure Hunt

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

Start Hunting!

Translated by