how to remove areas of an image containing a single color?
조회 수: 14 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2024년 5월 15일
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.
채택된 답변
Image Analyst
2024년 5월 15일
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
Image Analyst
2024년 5월 16일
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)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 3-D Volumetric Image Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!