how to crop the image from the green screen
5 ビュー (過去 30 日間)
古いコメントを表示
the background of the image will be green but i want to detect the white rectange inside and crop the image removing the green screen
0 件のコメント
回答 (3 件)
Image Analyst
2020 年 3 月 10 日
Use the Color Thresholder app on the Apps tab of the tool ribbon.
0 件のコメント
Gautam
2024 年 10 月 24 日
Hello @Nikhil Gulati
You can crop out the green portion by thresholding or quantizing the image based on colours.
Here's a code that uses K-means clustering to quantize the image and crops it
img = imread('image.jpeg');
numColors = 3;
L = imsegkmeans(img,numColors);
mask = mat2gray(L) > 0;
[rows, cols] = find(mask);
topRow = min(rows);
bottomRow = max(rows);
leftCol = min(cols);
rightCol = max(cols);
croppedImg = img(topRow:bottomRow, leftCol:rightCol, :);
imshow(croppedImg);
0 件のコメント
Image Analyst
2024 年 10 月 28 日
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;
markerSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN TEST IMAGE
folder = [];
baseFileName = 'green Background.jpeg';
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;
%--------------------------------------------------------------------------------------------
% MASK THE IMAGE.
[mask,maskedRgbImage] = createMask(rgbImage);
% mask = bwconvhull(mask, 'objects');
mask = imfill(mask, 'holes');
% Take largest blob (in case there are small noise blobs).
mask = bwareafilt(mask, 1);
% Show the mask image.
subplot(2, 2, 2);
imshow(mask)
impixelinfo;
axis('on', 'image');
caption = sprintf('Final Mask Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Mask the image with the new mask
% Mask image by multiplying each channel by the mask.
maskedRgbImage = rgbImage .* cast(mask, 'like', rgbImage); % R2016b or later. Works for gray scale as well as RGB Color images.
%--------------------------------------------------------------------------------------------
% FIND THE CROPPING BOX.
props = regionprops(mask, 'BoundingBox');
maskedRgbImage = imcrop(maskedRgbImage, props.BoundingBox);
% Show the masked and cropped image.
subplot(2, 2, 3:4);
imshow(maskedRgbImage)
impixelinfo;
axis('on', 'image');
caption = sprintf('Final Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
%===============================================================================================
function [BW,maskedRGBImage] = createMask(RGB)
%createMask Threshold RGB image using auto-generated code from colorThresholder app.
% [BW,MASKEDRGBIMAGE] = createMask(RGB) thresholds image RGB using
% auto-generated code from the colorThresholder app. The colorspace and
% range for each channel of the colorspace were set within the app. The
% segmentation mask is returned in BW, and a composite of the mask and
% original RGB images is returned in maskedRGBImage.
% Auto-generated by colorThresholder app on 26-Oct-2024
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.465;
channel1Max = 0.303;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.539;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
end
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!