How to erode parts of an image

6 次查看(过去 30 天)
Does anyone know the best way to get rid of the black part on the left side of the image and the black border around the white. I need to keep the numbers and letters. I tried different types of imerode, but could never get rid of it. Any pointers would be great thank you!NeedtobeEroded.jpg
  2 个评论
John D'Errico
John D'Errico 2019-11-9
Just crop the image. Whats the problem?
Adam Kelly
Adam Kelly 2019-11-9
How do I find the right dimensions to crop the part I want. Sorry if it is a dumb question, I am new to Matlab

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2019-11-10
This works without changing the shape of the "good" characters, like erosion would do:
grayImage = imread('NeedtobeEroded.jpeg');
if ndims(grayImage) > 1
% It's RGB - convert to gray scale.
grayImage = grayImage(:,:,1);
end
subplot(2, 2, 1);
imshow(grayImage);
binaryImage = grayImage > 128; % Convert to binary.
% Get rid of largest blob.
mask = bwareafilt(~binaryImage, 1);
binaryImage(mask) = true;
subplot(2, 2, 2);
imshow(binaryImage);
% Get aspect ratios so we can find the tall, narrow vertical line at the right.
props = regionprops(~binaryImage, 'BoundingBox', 'PixelIdxList');
bb = vertcat(props.BoundingBox)
aspectRatios = bb(:, 4) ./ bb(:, 3) % Heights over widths
% Find aspect ratios more than, say, 50.
badRegions = aspectRatios > 50;
% Fill them in
for k = 1 : length(props)
if badRegions(k)
% It's bad. Fill this one in.
binaryImage(props(k).PixelIdxList) = true;
end
end
subplot(2, 2, 3);
imshow(binaryImage);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
Another way to do it rather than getting rid of blobs with bad aspect ratios is, if you know the bad line is always there and always at the right side, you can simply fill in the rightmost blob.
grayImage = imread('NeedtobeEroded.jpeg');
if ndims(grayImage) > 1
% It's RGB - convert to gray scale.
grayImage = grayImage(:,:,1);
end
subplot(4, 1, 1);
imshow(grayImage);
binaryImage = grayImage > 128; % Convert to binary.
% Get rid of largest blob.
largestBlob = bwareafilt(~binaryImage, 1);
binaryImage(largestBlob) = true;
subplot(4, 1, 2);
imshow(binaryImage);
% Get rid of rightmost blob.
[labeledImage, numBlobs] = bwlabel(~binaryImage);
rightMostBlob = ismember(labeledImage, numBlobs);
subplot(4, 1, 3);
imshow(rightMostBlob);
binaryImage(rightMostBlob) = true;
subplot(4, 1, 4);
imshow(binaryImage);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
0000 Screenshot.png

更多回答(1 个)

Thiago Henrique Gomes Lobato
If you just have one image, doing a matlab code to find the borders would be an overkill, so it would be better to just check the pixel values that you want to cut. If, however, you have many similar images it start to make sense to develop some algorithm to find the borders for you. I made an example that may give you some ideas if this is the case (the resulting image is only with the white part and numbers):
I = imread('NeedtobeEroded.jpeg');
Ib = im2bw(I,0.5); % Binarize the image, so only values of 0 and 1 remains in the matrix
% Get Vertical and Horizontal Lines
VerticalLine = Ib(:,round(end/2));
HorizontalLine = Ib(round(end/2),:);
% Take the derivative, i.e, when the white becomes black and vice-versa
Diffy = diff(VerticalLine);
Diffx = diff(HorizontalLine);
% Get the color change locations
IndexChangeX = find(Diffy~=0);
IndexChangeY = find(Diffx~=0);
% Use the color change locations as index references
SafetyOffset = 5; % This is just a safety offset
xi = IndexChangeX(2)+SafetyOffset;xf = IndexChangeX(end-1)-SafetyOffset;
yi = IndexChangeY(2)+SafetyOffset;yf = IndexChangeY(end-1)-SafetyOffset;
INew = I (xi:xf,yi:yf,:);
figure,imshow(INew)
This code doesn't take into account boundary index problems, but as I said, it is just to give you a roughly idea what could be done and, for your case, it works fine.

类别

Help CenterFile Exchange 中查找有关 Geometric Transformation and Image Registration 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by