필터 지우기
필터 지우기

How to completely extract the green pixels from this image?

조회 수: 10 (최근 30일)
Malini
Malini 2024년 1월 8일
댓글: Image Analyst 2024년 1월 9일
I have used this code to extract only the plant canopy.
inimage = imread('AA100401.jpg'); %// type uint8
mask = inimage(:,:,1)<inimage(:,:,2) & inimage(:,:,3)<inimage(:,:,2);
outimage = bsxfun(@times, inimage, uint8(mask));
figure, imshow(outimage),title ('Extracted image');
But while converting it into binary image, few pixels were missing. How to solve this issue

채택된 답변

Image Analyst
Image Analyst 2024년 1월 8일
편집: Image Analyst 2024년 1월 8일
Try the Color Thresholder on the Apps tab of the tool ribbon. Use HSV color space, then click the Export function button on the tool ribbon.
% 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 = pwd;
baseFileName = 'Malini.png';
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
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)
rows = 478
columns = 639
numberOfColorChannels = 3
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
g.Name = 'Demo by Image Analyst';
g.NumberTitle = 'off';
drawnow;
%--------------------------------------------------------------------------------------------
% Threshold image.
[mask,maskedRGBImage] = createMask(rgbImage);
% Fill holes.
mask = imfill(mask, 'holes');
% Take largest blob.
mask = bwareafilt(mask, 1);
subplot(2, 2, 2);
imshow(mask)
impixelinfo;
axis('on', 'image');
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
subplot(2, 2, 3);
imshow(maskedRGBImage)
impixelinfo;
axis('on', 'image');
title('Masked RGBImage', '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 08-Jan-2024
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.128;
channel1Max = 0.458;
% 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.167;
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
  댓글 수: 3
Malini
Malini 2024년 1월 9일
Whether bwarea(mask) would work?
Image Analyst
Image Analyst 2024년 1월 9일
You could do that. bwarea gives a weighted area depending on the shape of the mask around its perimeter. Like if the mask is slanting through the area at 45 degrees, it might count the area as half a pixel. If you want a pure pixel count you can do
area = nnz(mask)
Neither is right or wrong or better -- they're just different definitions. The areas would be pretty close to each other. For your purposes either one would be fine.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by