copping grayscale image and remove any other borders or details
Show older comments
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
1 Comment
Accepted Answer
More Answers (3)
KSSV
on 2 Aug 2024
[I,rect] = imcrop(myimage) ;
6 Comments
Dina
on 2 Aug 2024
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1745341/image.jpeg');
inpict = imcrop(inpict,[120.51 63.51 679.98 455.98]);
imshow(inpict)
If you have other images which differ significantly in size such that a fixed rectangular crop won't work, then in what other ways do they differ? Are the backgrounds always black? What about the text, ticks and annotations within the region of interest? Are those to also be addressed, or is it okay to leave them? If they need to be removed, do they vary between images?
Dina
on 2 Aug 2024
The color distribution of the ultrasound image and the annotations, border, etc. are not mutually exclusive.
% the image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1745341/image.jpeg');
% subdivide the image regions
north = imcrop(inpict,[0.51 0.51 799.98 57.98]);
west = imcrop(inpict,[0.51 0.51 111.98 599.98]);
roi = imcrop(inpict,[120.51 63.51 679.98 455.98]);
mask = imread('mk.png');
% pixels from the ultrasound image with no annotations
uspix = roi(~mask);
subplot(3,1,1)
imhist(uspix)
% pixels from anywhere else
annpix = [roi(mask); north(:); west(:)];
subplot(3,1,2)
imhist(annpix)
% find the pixels whose values are common to both regions
compix = union(unique(uspix),unique(annpix));
commask = ismember(inpict,compix);
compix = inpict(commask);
subplot(3,1,3)
imhist(compix)
The distribution of values common to both regions spans the dynamic range of the datatype. Since the image is a JPG, and the non-ROI regions are full of high-contrast edges, this shouldn't be surprising. Let's say we just take the peaks of the histogram and select those pixels that are at least in the region which predominantly represents the non-ROI content:
% get counts and threshold
[count,cen] = imhist(annpix);
mk = count>1000;
figure
mask = ismember(inpict,[cen(mk)]);
imshow(~mask)
That's still not terribly good by itself, especially at the edges.
If we can't simply use pixel value alone to globally discriminate between the area of interest and other parts of the the image, we have to use some other information. How did I find the ROI? I used the marker annotations. Are those easy to find by graylevel alone? No, but if there is some aspect of their geometry or some other aspect of the image presentation which is consistent between images, then maybe we can use that.
For this image, it might suffice to use row/column means to find the location of the ROI, but if the annotation text on the left side extends further down, our assumptions may fail. If the average brightness of the ROI changes, the process may become unreliable.
It helps to have a more concrete understanding of the possible range of inputs.
Dina
on 2 Aug 2024
Dina
on 2 Aug 2024
Image Analyst
on 2 Aug 2024
Moved: DGM
on 3 Aug 2024
0 votes
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 Comments
Image Analyst
on 2 Aug 2024
Moved: DGM
on 3 Aug 2024
Need to login. Just zip up the files and attach the zip file here with the paperclip icon.
Image Analyst
on 3 Aug 2024
Moved: DGM
on 3 Aug 2024
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
on 3 Aug 2024
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
on 3 Aug 2024
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.
Categories
Find more on Image Processing Toolbox 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!



