Filter löschen
Filter löschen

How to draw vertical and horizontal histogram of an image ?

3 Ansichten (letzte 30 Tage)
I need to draw vertical and horizontal histogram of a gray scale image to find out the rectangular ROI of my image. Is it possible ?

Akzeptierte Antwort

Image Analyst
Image Analyst am 6 Okt. 2018
I wouldn't call them histograms but I think you are referring to the mean vertical or horizontal profile that you get by summing or averaging gray levels horizontally or vertically. To do that you'd do:
verticalProfile = sum(grayImage, 2);
horizontalProfile = sum(grayImage, 1);
or you can use mean() instead of sum().
  2 Kommentare
Sudipto Chaki
Sudipto Chaki am 6 Okt. 2018
Thanks for your answer. But, is it possible to extract Rectangular ROI based on vertical or horizontal profile?
Sudipto Chaki
Sudipto Chaki am 6 Okt. 2018
Basically I need to extract the following ROI automatically.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 6 Okt. 2018
Try this:
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 = '6.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 demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color 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.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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;
[mask, maskedRGBImage] = createMask(rgbImage);
% Extract the largest blob only. That will be the hand.
mask = bwareafilt(mask, 1);
% Get the convex hull to get rid of boundary noise (wiggles).
mask = bwconvhull(mask);
% Display the mask image.
subplot(2, 2, 2);
imshow(mask);
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
drawnow;
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Crop the image
[maskRows, maskColumns] = find(mask);
row1 = min(maskRows);
row2 = max(maskRows);
col1 = min(maskColumns);
col2 = max(maskColumns);
% Plot the box
xBox = [col1, col2, col2, col1, col1];
yBox = [row1, row1, row2, row2, row1];
hold on;
plot(xBox, yBox, 'r-', 'LineWidth', 2);
% Crop the masked image.
maskedRgbImage = maskedRgbImage(row1:row2, col1:col2, :);
% Get the dimensions of the image.
[maskRows, maskColumns, numberOfColorChannels] = size(maskedRgbImage)
% Display the image.
subplot(2, 2, 3);
imshow(maskedRgbImage, []);
impixelinfo;
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
drawnow;
% Crop the numbers out. From the circular mask, the numbers are between
% 17.7% and 25.9% of the height of the image, and between
% 26.6 and 40.2% of the width of the image
row1 = round(.244 * maskRows)
row2 = round(.357 * maskRows)
col1 = round(.212 * maskColumns)
col2 = round(.526 * maskColumns)
% Plot the box
xBox = [col1, col2, col2, col1, col1];
yBox = [row1, row1, row2, row2, row1];
hold on;
plot(xBox, yBox, 'r-', 'LineWidth', 2);
maskedRgbImage = maskedRgbImage(row1:row2, col1:col2, :);
% Display the image.
subplot(2, 2, 4);
imshow(maskedRgbImage, []);
impixelinfo;
title('Masked Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
drawnow;
fprintf('DONE!\n');
uiwait(helpdlg('Done!'));
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 06-Oct-2018
%------------------------------------------------------
% Convert RGB image to chosen color space
I = rgb2hsv(RGB);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.201;
channel1Max = 0.000;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.236;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.303;
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
  11 Kommentare
Image Analyst
Image Analyst am 7 Okt. 2018
You can use imgradientxy().
Sudipto Chaki
Sudipto Chaki am 7 Okt. 2018
Bearbeitet: Sudipto Chaki am 7 Okt. 2018
How to convert the digit image into (3*2) blocks where I can check vertical and horizontal edges using imgradientxy()? Actually, I need to check if vertical or horizontal edges exists in each block? If exists I need to return binary value 1.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by