How can I delete the part of the image if the black area exists in the image?
3 次查看(过去 30 天)
显示 更早的评论
Dear sir,
I have 22x224x3 image and divided 4 part region. Please see my attchment photo.
In the image, left_bottom and right_bottom have black area. How can I remove part of image ( from 4 part) if black area exists.
In this example, I want to delete two part (left_bottom_region and right_bottom_region - they have black area) and recombine (left_top_region and right_top_region - they havn't black area)
Someone can help me, please? Thanks a lot with respect.
FaceCrop = imread("1.png");
left_top_region = FaceCrop(1:112, 1:112, :); figure,subplot(2,2,1);imshow(left_top_region);
right_top_region = FaceCrop(1:112, 112:224, :);subplot(2,2,2);imshow(right_top_region);
left_bottom_region = FaceCrop(112:224, 1:112, :);subplot(2,2,3);imshow(left_bottom_region);
right_bottom_region = FaceCrop(112:224, 112:224, :);subplot(2,2,4);imshow(right_bottom_region);
3 个评论
Walter Roberson
2023-3-30
It is not possible to have an array with a "hole" in it, so what do you want to replace the black pixels with?
回答(2 个)
Image Analyst
2023-3-31
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = '1111.jpg';
folder = pwd;
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 image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Take just the left half of the image
middleColumn = columns / 2;
rgbImage = rgbImage(:, 1 : middleColumn, :);
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%--------------------------------------------------------------------------------------------------
% Find the black rectangle
[r,g,b] = imsplit(rgbImage);
mask = (r == 0) & (g == 0) & (b == 0);
% Take the largest blob.
mask = bwareafilt(mask, 1);
% Fill any holes.
mask = imfill(mask, 'holes');
% Display image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis on;
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
drawnow;
%--------------------------------------------------------------------------------------------------
% Get bounding box.
props = regionprops(mask, 'BoundingBox');
column1 = floor(props.BoundingBox(1))
column2 = ceil(column1 + props.BoundingBox(3))
row1 = floor(props.BoundingBox(2))
row2= ceil(row1 + props.BoundingBox(3))
%---------------------------------------------------------------------------------------
% Delete rows that have the black rectangle in them.
rgbImage2 = rgbImage; % Make a copy.
rgbImage2(row1:row2, :, :) = [];
% Display image.
subplot(2, 2, 3);
imshow(rgbImage2, []);
impixelinfo;
axis on;
caption = sprintf('After Rows %d - %d removed', row1, row2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
%---------------------------------------------------------------------------------------
% Delete columns that have the black rectangle in them.
rgbImage2(:, column1 : column2, :) = [];
% Display image.
subplot(2, 2, 4);
imshow(rgbImage2, []);
impixelinfo;
axis on;
caption = sprintf('After Columns %d - %d removed', column1, column2);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
0 个评论
DGM
2023-3-31
移动:Image Analyst
2023-3-31
Alternatively, you might want to keep all those surrounding pixels.
inpict = imread('plsremovethepixels.jpg');
% create a mask describing the area to be preserved
mask = rgb2gray(inpict)<4;
mask = bwareafilt(mask,2);
mask = imopen(~mask,ones(21));
mask = imclose(mask,ones(21));
mask = imdilate(mask,ones(3));
% find all candidate geometries for this number of pixels
sz0 = size(mask);
sz = factor2(nnz(mask),'ordered',false);
% select the one with aspect ratio most similar to the original
ar0 = (min(sz0(1:2))/max(sz0(1:2)));
ar = sz(:,1)./sz(:,2);
[~,idx] = min(abs(ar - ar0));
sz = [sz(idx,:) size(inpict,3)];
% mask and reshape into a new rectangular image with no holes
mask = repmat(mask,[1 1 size(inpict,3)]);
outpict = inpict(mask);
outpict = reshape(outpict,sz);
imshow(outpict)
Okay so maybe keeping all the pixels isn't totally necessary. Maybe we just want to close the hole using the surrounding image.
inpict = imread('plsremovethepixels.jpg');
% create a mask describing the area to be removed
mask = rgb2gray(inpict)<5;
mask = bwareafilt(mask,2);
mask = imclose(mask,ones(21));
mask = imopen(mask,ones(21));
mask = imdilate(mask,ones(3));
% fill the masked region with NaN
outpict = im2double(inpict);
outpict(repmat(mask,[1 1 size(inpict,3)])) = NaN;
% sift pixels radially inward to move empty region to the edges
outpict = siftpixels(outpict,'c');
% crop off empty fringes
outpict = imcrop(outpict,[47 22 177 237]);
imshow(outpict)
Or maybe instead of moving the surrounding pixels, we can just fill the region based on them!
inpict = imread('plsremovethepixels.jpg');
% create a mask describing the area to be removed
mask = rgb2gray(inpict)<5;
mask = bwareafilt(mask,2);
mask = imclose(mask,ones(21));
mask = imopen(mask,ones(21));
mask = imdilate(mask,ones(3));
% inpaint the selected region
outpict = inpict;
for c = 1:size(inpict,3)
outpict(:,:,c) = regionfill(outpict(:,:,c),mask);
end
imshow(outpict)
Hmm. That's still not quite good enough. Maybe we can fill it with our imagination!
inpict = imread('plsremovethepixels.jpg');
fg = imread('totallyokay.jpg');
% create a mask describing the area to be removed
mask = rgb2gray(inpict)<5;
mask = bwareafilt(mask,2);
mask = imclose(mask,ones(21));
mask = imopen(mask,ones(21));
mask = imdilate(mask,ones(3));
% fill the missing region
outpict = uint8(mask.*double(fg) + (1-mask).*double(inpict));
imshow(outpict)
... and that's why nobody pays me to draw things.
2 个评论
Image Analyst
2023-3-31
移动:Image Analyst
2023-3-31
Nonetheless, it's a good demonstration of some useful techniques. siftpixels does not seem to be a built-in function, though regionfill is.
Your comment "that's why nobody pays me to draw things" made me think of this:
😀😀😀
DGM
2023-4-1
The worst part about using a tablet is how poorly it replicates the familiar feel of the natural media such as pencil on paper, felt on bristol, or crayons on drywall.
另请参阅
类别
在 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!