Cutting off a segment of an Image that is blurry

1 view (last 30 days)
Hello--
Basically I wanted to perform image analysis on a a hair switch and I was able to seperate the image from the background as follows:
If you look at the last image, all I want to do is basically cut off the top and bottom parts of the image because they are blurry. This can be a standard way of doing it that does the same crop for every picture I process because they will always be taken the same way.
The original pictures are here:
and
My code to get souly the hair is as follow (thank you image analyst for providing me with most of this):
  2 Comments
Brian Peoples
Brian Peoples on 8 Jul 2020
% Demo to mask of hair switch.
% By Image Analyst.
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.
file = 'C:\Users\bpeoples\Downloads\MATLAB GLOSS MEASUREMENTS\Sample_Image_Folder\Measurements\Blonde_Hair_Diffused\3CROP_trial 3';
filename = dir(fullfile(file, '*.jpg'));
total_images = numel(filename);
f = fullfile(file, filename(1).name);
rgbImage = imread(f);
[rows, columns, numberOfColorChannels] = size(rgbImage);
% Display the RGB image full size.
subplot(2, 2, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Original Image : "%s"', filename(1).name);
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';
% Do color segmentation to get the blue tape.
[BW,maskedRGBImage] = createMask(rgbImage);
mask = ~BW;
% Get rid of blobs smaller than 10,000 pixels.
mask = bwareaopen(mask, 10000);
% Display the binary image.
subplot(2, 2, 2);
imshow(mask, []);
axis('on', 'image');
caption = sprintf(' Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get the area and centroid.
labeledImage = bwlabel(mask);
props = regionprops(labeledImage, 'Area', 'Centroid');
allAreas = [props.Area]
xy = vertcat(props.Centroid);
% Find out which one is closest to the middle.
x = xy(:, 1);
middlex = columns/2
distanceFromMiddle = abs(x - middlex)
[minDistance, indexOfMiddle] = min(distanceFromMiddle)
% Keep the middle blob.
hairMask = ismember(labeledImage, indexOfMiddle);
hairMask = imfill(hairMask, 'holes');
%convert masked image to rgb
I4 = cat(3,hairMask,hairMask,hairMask);
I5 = uint8(I4).*rgbImage;
% Display the RGB image full size.
subplot(2, 2, 3);
imshow(hairMask, []);
axis('on', 'image');
caption = sprintf('Hair Mask');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Display the RGB image full size.
subplot(2, 2, 4);
imshow(I5, []);
axis('on', 'image');
caption = sprintf('RGB Hair Mask');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%=====================================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 06-Jul-2020
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.580; %orig .410
channel1Max = 0.670; %orig. .670
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
Brian Peoples
Brian Peoples on 8 Jul 2020
If you look at the subplot, right around 500 and 3500 is where it gets blurry, so if I could do a cut at those spots that'd be ideal and just have those parts removed from the image section.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 8 Jul 2020
Can't you just erase above and below those lines?
rgbImage(1 : 500, :, :) = 0;
rgbImage(3500 : end, :, :) = 0;
  2 Comments
Brian Peoples
Brian Peoples on 8 Jul 2020
YES ahaha! I literally did that a second after I posted the question and forgot about it. I really do apologize for wasting your time

Sign in to comment.

More Answers (0)

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!