How can i extract desired image from an image?

In the attached figure, I have to extract the visible part (i.e. rectangular eye part) in order to have focused eye region. I have tried imcrop function and it does what I need. But everytime i have to select the image co-ordinates. I want to do it automatically, like MATLAB itself sense the co-ordinates and extract that focused eye part. I mean there might be some algorithm which can vanish the black part and extract visible one. anybody have idea, how to do it?
cr1=imcrop(fmr_ext1,[500.5 388.5 85 117]);

 Accepted Answer

You can get the bounding box and then use crop:
measurements = regionprops(grayImage>0, 'BoundingBox');
croppedImage = imcrop(grayImage, measurements.BoundingBox);

7 Comments

it gives error in 2nd line
Error using imcrop>parseInputs (line 197) Too many input arguments.
Error in imcrop (line 94) [x,y,a,cm,spatial_rect,h_image,placement_cancelled] = parseInputs(varargin{:});
Error in trye (line 15) croppedImage = imcrop(a,measurements.BoundingBox);
That was untested just off the top of my head. Alright, I guess I'll have to download your image and build you a full-blown demo. Give me time.
sure, I will be waiting. :)
When I downloaded your image, I found out it was corrupted with a white border and bad JPG artifacts so I had to clean those up. DO NOT use JPG images for image processing. More robust code is below:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
%===============================================================================
% Read in a gray scale demo image.
folder = pwd;
% Get the full filename, with path prepended.
baseFileName = 'd.jpg';
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.
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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Threshold (binarize) the image.
binaryImage = grayImage > 10;
% Get rid of white border/frame that is there for some weird reason.
binaryImage = imclearborder(binaryImage);
% Because he used a JPG image instead of PNG,
% there are tons of jpg artifacts.
% Take the largest blob.
binaryImage = bwareafilt(binaryImage, 1);
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find the bounding box.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'BoundingBox');
croppedImage = imcrop(grayImage, measurements.BoundingBox);
% Display the cropped image.
subplot(2, 2, 3);
imshow(croppedImage, []);
title('Cropped', 'FontSize', fontSize, 'Interpreter', 'None');
boss you have achieved the result what i want. First of all thankyou for your prestigious effort and help. But i Got the error in
binaryImage = bwareafilt(binaryImage, 1);
Undefined function 'bwareafilt' for input arguments of type 'double'.
Error in trye (line 52)
binaryImage = bwareafilt(binaryImage, 1);
I guess this function is not defined in MATLAB. I am using MATLAB 2012b
thanks it works in 2015a..thankyou alot
See my own function, attached, written before they came out with bwareafilt().

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!