Effacer les filtres
Effacer les filtres

how to remove areas of an image containing a single color?

8 vues (au cours des 30 derniers jours)
Bradley
Bradley le 15 Mai 2024
Commenté : Image Analyst le 16 Mai 2024
I have a video that I saved each frame from. The entire video has static text in big white font and it doesnt move so each frame looks very similar. Each frame also has a black background like I want to remove. Is there a way for me to remove any pixle that is (0,0,0) or (1, 1, 1) RGB? Thanks!
heres an example image that I want to crop. I want to crop all of the black and all of the white.
  3 commentaires
Walter Roberson
Walter Roberson le 15 Mai 2024
It is not possible to create a matrix that has "holes" in it. You could detect 0,0,0 and 1,1,1 and have a logical mask that is true everywhere there is background -- but you cannot delete the background leaving only foreground. You can fill the background with something.
Bradley
Bradley le 15 Mai 2024
Thanks for this!
In the imsubtract examples this use this line of code that im not fimilar with:
strel('disk',15)
I looked up strel but I still dont really get it. The example says it uses that line of code to estimate the background, how does that work? How do I define the background as being black or anything other than the sonar image? Thanks again!

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 15 Mai 2024
Try this:
% Demo by Image Analyst
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;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'bradley.png';
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
rgbImage = imread(fullFileName);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis('on', 'image');
title('Original RGB Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Update 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(rgbImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------
% SPLIT INTO INDIVIDUAL COLOR CHANNELS SO WE CAN FIND WHITE.
[redImage, greenImage, blueImage] = imsplit(rgbImage);
threshold = 200; % Adjust as necessary.
whitePixels = (redImage >= threshold) & (greenImage >= threshold) & (blueImage >= threshold);
subplot(2, 2, 2);
imshow(whitePixels)
impixelinfo;
axis('on', 'image');
title('White Pixels', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------
% GET THE SECTOR SHAPED MASK.
% Fill holes.
mask = imfill(whitePixels, 'holes');
% Take largest blob only.
mask = bwareafilt(mask, 1);
% Erode away one pixel layer to get rid of the white outline.
mask = imerode(mask, ones(3));
% Erase outside the mask by setting those pixels to zero.
redImage(~mask) = 0;
greenImage(~mask) = 0;
blueImage(~mask) = 0;
% Reconstruct the RGB image.
rgbImage2 = cat(3, redImage, greenImage, blueImage);
% Display the image
subplot(2, 2, 3);
imshow(mask)
impixelinfo;
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%--------------------------------------------------------------------------------------------
% Find bounding box
props = regionprops(mask, 'BoundingBox')
bb = props.BoundingBox;
% Crop original image
rgbImage2 = imcrop(rgbImage2, bb);
subplot(2, 2, 4);
imshow(rgbImage2)
impixelinfo;
axis('on', 'image');
title('Cropped RGBImage', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
  4 commentaires
Bradley
Bradley le 16 Mai 2024
Modifié(e) : Bradley le 16 Mai 2024
I tried adding this to my code, but got errors. Specifically about the size of the image and how alpha is invalid. Heres the error I got:
Error using writepng>parseInputs
The value of 'alpha' is invalid. Expected input to be of size 514x928, but it is of size 536x1030.
Error in writepng (line 20)
[results, unmatched] = parseInputs(data,map,filename,varargin{:});
Error in imwrite (line 566)
feval(fmt_s.write, data, map, filename, paramPairs{:});
Error in untitled2 (line 27)
imwrite(rgbImage2, 'figure1.png', 'Alpha', AlphaImage)
Im not exactly sure what this means or how to fix it. This is my first time working with images.
I tried adding this line but it didnt work?
K = imresize(rgbImage2, [514 928]);
Thanks again.
Image Analyst
Image Analyst le 16 Mai 2024
That's because only the RGB image was cropped. The mask is still for the original image. If you want to crop that too you need to call imcrop on it
mask2 = imcrop(mask, bb); % Crop mask.
AlphaImage = double(~mask2);
imwrite(rgbImage2, 'FileNameGoesHere.png', 'Alpha', AlphaImage)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Image Segmentation and Analysis dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by