官术网_书友最值得收藏!

Time for action – making a custom object eraser function

This time we are going to make a more complex tool. We'll write a function that accepts an image as an input, prompts the user to select a ROI (using either one of the two methods described previously) and then prompts the user to select a pixel with the color to be used for the erasing process. Finally, it will use the color of the pixel chosen by the user to erase the area defined by the mask. The code will be something like as follows (we'll call it FreehandMasking.m):

function[output] = FreehandMasking(input,method)

% Function that performs masking of a user-defined ROI
% Inputs:
%           input -  Input image
%           method – ROI selection (1: roipoly, 2: imfreehand)
% Output:
%           output - Output image (masked)

switch method
case 1
mask = roipoly(input);););% Select ROI using roipoly
case 2
figure, imshow(input)
h = imfreehand;      % Select ROI using imfreehand
pos = wait(h);
[rows,columns] = size(input);
 mask = poly2mask(pos(:,1),pos(:,2),rows,columns);
end
pix = impixel(input);   % Select pixel with eraser color
output = input;    % Set output equal to input
output(mask) = pix(1);  % Perform masking to erase selected object
  1. Now let's test our code. We will try to erase two parts of the middle rock of our examples, using different colors. Let's first type in the commands to crop the middle rock:
    >> img = imread('3rocks.jpg');
    >> rock = rgb2gray(img);
    >> rock = imcrop(rock)
  2. Once we crop the area we want to use, we must call the function we just made, twice. We will now use the roipoly function for the part of the rock that is below the water level in the image and the imfreehand function for the part of the rock that is above the water level. First, let's mask the part below sea level (we will double-click on a pixel from the sky region to select its color for erasing the rock):
    >> rock2 = FreehandMasking(rock,1);
  3. Now, we will mask the part above sea level (we will double-click on a pixel from the sea for erasing the rock):
    >> rock3 = FreehandMasking(rock2,2);
  4. Let's see the final result:

What just happened?

The tool we just finished making is more sophisticated than the others so far. It prompts twice for user input; once for the ROI selection using the predefined method (given as input) and once for the selection of the color of the eraser. We took advantage of this functionality to repeat the example of erasing the middle rock, this time using two colors, one for each chosen ROI of the rock. The result is even better than before, since the part of the rock lying below the sea level got erased using a darker color, hence camouflaging the rock more efficiently. In the following chapters, you will see more exciting examples using the ROI selection techniques that we presented in this section.

主站蜘蛛池模板: 利川市| 乳源| 平塘县| 绍兴县| 农安县| 老河口市| 东丰县| 彝良县| 甘洛县| 西平县| 乡宁县| 克拉玛依市| 河曲县| 清水县| 上杭县| 五河县| 玉屏| 清丰县| 沿河| 尖扎县| 惠来县| 吴川市| 修武县| 根河市| 武强县| 齐齐哈尔市| 衡山县| 巢湖市| 岑巩县| 象州县| 阜新市| 江安县| 民勤县| 乐亭县| 乌兰县| 班戈县| 铜陵市| 涿州市| 揭西县| 郁南县| 肥乡县|