how to crop a interest part image

hellow
I have this image i segmented by kmeans méthod, and i get this résult
Now i want to crop it to get just the license plate
, so there is a solution ???
please help me .

 Accepted Answer

Just get profiles and use find:
verticalProfile = sum(binaryImage, 2); % Sum along columns in each row.
row1 = find(verticalProfile, 1, 'first');
row2 = find(verticalProfile, 1, 'last');
horizontalProfile = sum(binaryImage, 1); % Sum along rows in each column.
column1 = find(horizontalProfile, 1, 'first');
column2 = find(horizontalProfile, 1, 'last');
croppedImage = binaryImage(row1:row2, column1:column2);

4 Comments

guitone's answer moved here:
thank you for reply
but when i execute the code i have the same image 1 in grayscale not the only license plate Number
So just crop the rgb image to get the cropped RGB image:
croppedImage = rgbImage(row1:row2, column1:column2, :);
If this does it for you, then can you mark the Answer as Accepted? Here's the full code in case the snippet I gave was not explicit enough for you:
clc;
close all;
workspace; % Make sure the workspace panel with all the variables is showing.
format long g;
format compact;
fontSize = 18;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%==================================================
% Read in a standard MATLAB color demo image.
folder = pwd;
baseFileName = 'image-segmented.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows, columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 1);
imshow(rgbImage);
axis on;
title('Original Color Image (White Frame Removed)', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
% Crop the image
rgbImage = rgbImage(50:400, 100:600, :);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Crop the image
grayImage = redChannel;
subplot(2, 3, 2);
imshow(rgbImage);
title('Cropped Color Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);
subplot(2, 3, 3);
pixelCount(1) = pixelCount(2);
pixelCount(end-1) = pixelCount(end);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of cropped gray image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Threshold the image to create a binary image
binaryImage = grayImage > 75;
% Display the original gray scale image.
subplot(2, 3, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize);
% Find the bounding box
verticalProfile = sum(binaryImage, 2); % Sum along columns in each row.
row1 = find(verticalProfile, 1, 'first');
row2 = find(verticalProfile, 1, 'last');
horizontalProfile = sum(binaryImage, 1); % Sum along rows in each column.
column1 = find(horizontalProfile, 1, 'first');
column2 = find(horizontalProfile, 1, 'last');
% Plot the box over the image.
xBox = [column1, column2, column2, column1, column1];
yBox = [row1, row1, row2, row2, row1];
hold on;
plot(xBox, yBox, 'r-', 'LineWidth', 2);
axis on;
%---------------------------------------------------------------------------
% Display the image.
subplot(2, 3, 5);
croppedImage = rgbImage(row1:row2, column1:column2, :);
imshow(croppedImage);
title('Final Cropped Image', 'FontSize', fontSize);
drawnow;
uiwait(helpdlg('Done with demo!'));
guitone's "Answer" moved here:
Thank you for reply, but there is an error:
Index exceeds matrix dimensions.
Error in test (line 51)
greenChannel = rgbImage(:, :, 2)
Can you help me?
You're obviously not using the image that you posted and gave to me. The image you posted is a color image, but the one you're using is a grayscale image. So you need to put this code after your call to imread():
if numberOfColorBands >= 3
% Crop the image
rgbImage = rgbImage(50:400, 100:600, :);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Crop the image
grayImage = redChannel;
else
% It's already gray scale.
% Crop the image
grayImage = rgbImage(50:400, 100:600, :);
end

Sign in to comment.

More Answers (1)

Categories

Community Treasure Hunt

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

Start Hunting!