clc;
close all;
clear;
workspace;
format long g;
format compact;
fontSize = 25;
baseFileName = 'capture.png';
folder = pwd
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
fullFileNameOnSearchPath = baseFileName;
if ~exist(fullFileNameOnSearchPath, 'file')
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);
imshow(rgbImage, []);
axis on;
caption = sprintf('Original Color Image, %s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo();
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
hp = impixelinfo();
redChannel = rgbImage(:, :, 1);
uiwait(helpdlg('Locate the nose. Double-click inside it to accept it'));
hRect = imrect();
noseBox = wait(hRect);
subplot(2, 2, 1);
imshow(rgbImage);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
rectangle('Position', noseBox, 'EdgeColor', 'b');
axis on;
hsvImage = rgb2hsv(rgbImage);
hueImage = hsvImage(:,:,1);
satImage = hsvImage(:,:,2);
valueImage = hsvImage(:,:,3);
subplot(2, 2, 2);
imshow(hsvImage);
title('HSV Image, displayed in RGB', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
hold on;
rectangle('Position', noseBox, 'EdgeColor', 'r');
axis on;
impixelinfo();
nx = noseBox(1) + noseBox(3)/2;
ny = noseBox(2) + noseBox(4)/2;
final_x = nx - 145;
final_y = ny - 25;
theta = 0 : 0.01 : 2*pi;
radius = 45;
X = radius * cos(theta) + final_x;
Y = radius * sin(theta) + final_y;
plot(X, Y, 'b-', 'LineWidth', 1);
leftMask = poly2mask(X, Y, rows, columns);
final_x = nx + 145;
theta = 0 : 0.01 : 2*pi;
X2 = radius * cos(theta) + final_x;
plot(X2, Y, 'b-', 'LineWidth', 1);
rightMask = poly2mask(X2, Y, rows, columns);
mask = leftMask | rightMask;
blurWindowWidth = 21;
se = strel('disk', blurWindowWidth, 0);
kernel = se.Neighborhood;
kernel = kernel / sum(kernel(:));
blurredMask = conv2(double(mask), ones(blurWindowWidth)/blurWindowWidth^2, 'same');
mask = blurredMask > 0;
subplot(2, 2, 3);
imshow(blurredMask);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
hold on
axis on;
hueImage(mask) = hueImage(mask) .* (1 - blurredMask(mask));
satImage = satImage + 0.01 * blurredMask;
valueImage = valueImage + 0.01 * 5 * blurredMask;
hueImage(hueImage>1)=1; hueImage(hueImage<0)=0;
satImage(satImage>1)=1; satImage(satImage<0)=0;
valueImage(valueImage>1)=1; valueImage(valueImage<0)=0;
hsvImage = cat(3, hueImage, satImage, valueImage);
newRGBImage = hsv2rgb(hsvImage);
subplot(2, 2, 4);
imshow(newRGBImage);
title('New Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
rectangle('Position', noseBox, 'EdgeColor', 'b');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
figure;
imshow(newRGBImage);
title('New Image', 'FontSize', fontSize, 'Interpreter', 'None');
hold on;
rectangle('Position', noseBox, 'EdgeColor', 'b');
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
Best Answer