I have an OCT images dataset with white border for image acquiring, and I want to delete this white border, I tried imclearborder, but the inner border of the image also deleted , the image below is as the result of imcomlement
1 view (last 30 days)
Show older comments
5 Comments
Answers (1)
Image Analyst
on 2 May 2020
I work with OCT images every week. Why do you need to remove them? It shouldn't matter. Anyway, what does remove mean? Like crop? Or set to white? Or set to the mean of the image?
Do you mean like this:
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 = 22;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = pwd;
baseFileName = 'image.jpeg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
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
% Read in the image from disk. If storedColorMap is not empty, it's an indexed image with a stored colormap.
[grayImage, storedColorMap] = imread(fullFileName);
if ~isempty(storedColorMap)
grayImage = ind2rgb(grayImage, storedColorMap);
end
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
grayImage = rgb2gray(grayImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
% grayImage = grayImage(:, :, 2); % Take green channel.
end
% Display the image.
hFig = figure;
subplot(2, 2, 1);
imshow(grayImage, []);
impixelinfo;
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
hFig.WindowState = 'maximized'; % May not work in earlier versions of MATLAB.
drawnow;
%--------------------------------------------------------------------------------------------------------
% SEGMENTATION
% Binarize the image
binaryImage = grayImage <= 5;
% Take the blobs largest than 20 pixels only.
binaryImage = bwareafilt(binaryImage, [20, inf]);
% Display the segmented image.
subplot(2, 2, 2);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
%--------------------------------------------------------------------------------------------------------
% EXTRACTION OF EACH BLOB
props = regionprops(binaryImage, 'BoundingBox', 'Area');
allAreas = [props.Area]
% Display them
for k = 1 : length(props)
thisBB = props(k).BoundingBox;
% Erase image in the extended box.
row1 = ceil(thisBB(2));
row2 = row1 + thisBB(4);
col1 = ceil(thisBB(1));
col2 = col1 + thisBB(3);
grayImage(row1:row2, :) = 0;
end
% Crop
[r, c] = find(grayImage);
grayImage = grayImage(min(r):max(r), :);
% Display the segmented image.
subplot(2, 2, 3:4);
imshow(grayImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
11 Comments
Image Analyst
on 3 May 2020
That's because it's been rotated in the opposite direction. You can tell which direction it's been rotated by looking at the width and height of the black triangle in the lower left corner. You can use regionprops() to get the bounding box of the triangles.
So make up two functions, one to crop for a counter-clockwise rotation like I already did for you, and then another, slightly modified function to handle rotation in the other direction. Something like
if heightOfTriangle > widthOfTriangle
croppedImage = RotateCounterClockwise(grayImage);
else
croppedImage = RotateClockwise(grayImage);
end
The functions will be largely the same just with slight modifications that I'm sure you can figure out.
See Also
Categories
Find more on Blue 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!