copping grayscale image and remove any other borders or details

99 次查看(过去 30 天)
could you please help me to get only the gray scale images without any borders and without any written words or letters created by the device . i want a general code to do this beause i have other images with other border thickness and other written , and i want the gray image entire only

采纳的回答

DGM
DGM 2024-8-2,21:03
编辑:DGM 2024-8-2,21:13
This example relies on the images being clean TIFF files of the expected size. It won't work on JPGs, and it won't work on the downscaled PNG thumbnails. The template images are attached.
unzip UltraS_tiff_images.zip
% the image
inpict = imread('US_tiff_images/tech_029.tiff');
inpict = im2gray(inpict); % needs to be single-channel
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a template image of the mfr logo
logo = imread('logo.png');
% find the logo
corrmap = normxcorr2(logo,inpict);
[row col] = find(corrmap == max(corrmap(:)));
szl = size(logo,1:2);
nwcorner = [row col] - szl + 1;
% fill the logo with a known value so that it can be inpainted later
% if it doesn't get cropped out
inpict(nwcorner(1)+(0:szl(1)-1),nwcorner(2)+(0:szl(2)-1)) = 234;
% crop to the logo position
% this immediately gets rid of the top and most LH annotations
inpict = inpict(nwcorner(1):end,nwcorner(2):end);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% a template image of the color bar
cbar = imread('cbar.png');
% find the colorbar
corrmap = normxcorr2(cbar,inpict);
[row col] = find(corrmap == max(corrmap(:)));
szl = size(cbar,1:2);
nwcorner = [row col] - szl + 1;
% fill the colorbar with a known value so that it can be inpainted later
% if it doesn't get cropped out
inpict(nwcorner(1)+(0:szl(1)-1),nwcorner(2)+(0:szl(2)-1)) = 234;
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% try to get rid of annotations by doing inpainting
% this won't always work, and the masking relies on an exact value
mask = ismember(inpict,233:234);
inpict = regionfill(inpict,mask);
% try to find the ultrasound image region
% based on the value distribution in row/col vectors
ent = entropyfilt(inpict);
xb = median(ent,1) > 2;
yb = median(ent,2) > 2;
mask = bwareafilt(xb & yb,1);
% crop again
[~,rows,cols] = crop2box(mask);
outpict = inpict(rows,cols);
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
imshow(outpict)
If there are large bands within the ultrasound image where there is little variation (e.g. at the top or bottom of the scan), those regions may get cropped off. If there is such a band which splits the image, only the largest part will be returned.
  5 个评论
Dina
Dina 2024-8-3,19:53
yes, i am going to use transfer learning. the size of image expects to be 227*227*3 or 224*224*3. The logo and white written doesn't have the same place for all images. I will classify the images as normal and abnormal. Could you help me about the preprocessing steps that i needed before using transfer learning , filtreing,normalization,etc depend on the images nature as seen @Image Analyst
Image Analyst
Image Analyst 2024-8-3,21:42
@Dina it's far too much information for me to spell out here so I suggest you go through the Deep Learning Onramp where they will teach you how to do transfer learning: https://matlabacademy.mathworks.com/details/deep-learning-onramp/deeplearning
Basically you need to make up two folders called Normal and Abnormal where you have your ground truth images (already cropped, white pixels filled in, and resized). Then you call a function called something like trainnet to train/create the neural network model. Then you can call a function like classify or minibatchpredict to predict the class on new images.

请先登录,再进行评论。

更多回答(3 个)

KSSV
KSSV 2024-8-2,6:26
Read about imcrop.
[I,rect] = imcrop(myimage) ;
  6 个评论
Dina
Dina 2024-8-2,9:25
i attached another images , the original extension of images are .tiff but i convert it to display here
Dina
Dina 2024-8-2,9:31
so , you mean removing redundant objects or letters can not be done ?
what about cropping only gray without specific dimenssions

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-8-2,13:27
移动:DGM 2024-8-3,3:54
Not sure of the source of this image. It looks like a screenshot that was exported by your ultrasound program. That means the original grayscale image is probably contained in an image, like a dicom image, along with some metadata, some of which gets imprinted on the screen as text. Plus with the PNG images you attached, I can see some different cropping, like in one you included the words on the left and in another you didn't, so that makes it harder to simply use the fixed ROI that the image would have. So I think the best approach is to just use the original format image and try to extract the image from it alone, and not use a screenshot exported by some other program. If you have a dicom image, attach it.
  6 个评论
Image Analyst
Image Analyst 2024-8-3,3:46
移动:DGM 2024-8-3,3:56
These are the annotated images. Is that all you have? Is your instrument not able to give you the file with the image data and the meta data separated?
DGM
DGM 2024-8-3,3:58
Sorry about all the moving. Page shifted when I went to move the comment-as-answer above yours and I didn't catch it.

请先登录,再进行评论。


Image Analyst
Image Analyst 2024-8-3,22:41
Try this:
% Demo by Image Analyst
% Initialization steps:
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 = 16;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = fullfile(pwd, '\US_tiff_images');
baseFileName = "tech_004.tiff";
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~isfile(fullFileName)
% 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 image file.
rgbImage = imread(fullFileName);
% Get size
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Get gray scale version of it.
if numberOfColorChannels == 3
grayImage = rgbImage(:, :, 2); % Take green channel.
else
grayImage = rgbImage;
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
axis('on', 'image');
impixelinfo;
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------------------
% CREATE MASK.
% Threshold.
thresholdValue = 0;
xline(thresholdValue, 'Color', 'r');
% Create initial mask
imageMask = grayImage > thresholdValue;
subplot(2, 2, 2);
imshow(imageMask);
axis('on', 'image');
impixelinfo;
title('Initial Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------------------
% Fill holes, extract largest, crop, and resize to 227x227.
imageMask = bwconvhull(imageMask, 'objects');
% Take largest blob which should be the image.
imageMask = bwareafilt(imageMask, 1);
% Do an opening (erosion followee by dilation) to get rid of little
% "tendrils" caused by text sticking out of the image.
imageMask = imopen(imageMask, true(3));
subplot(2, 2, 3);
imshow(imageMask);
axis('on', 'image');
impixelinfo;
title('Final Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------------------
% Get the bounding box and crop out the image from the full sized image.
props = regionprops(imageMask, 'BoundingBox');
croppedImage = imcrop(grayImage, props.BoundingBox);
croppedImage = imresize(croppedImage, [227, 227]);
subplot(2, 2, 4);
imshow(croppedImage);
axis('on', 'image');
impixelinfo;
title('Final Cropped, Resized Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------------------
% Save the image in an output folder.
outputFolder = fullfile(folder, '\output');
if ~isfolder(outputFolder)
% Create folder if it does not exist yet.
fprintf('Creating new folder for output images:"%s".\n', outputFolder);
mkdir(outputFolder);
end
fullOutputFileName = fullfile(outputFolder, baseFileName);
fprintf('Saving output file "%s".\n', fullOutputFileName);
imwrite(croppedImage, fullOutputFileName);
I would not worry about the small remnants of the text that remain in the image. It's likely that they will be ignored and not cause any difference in the predicted output.

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by