Generating histogram of a gradient image: histogram bar shows a white blank space within a histogram bar
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have generated a sobel gradient map of an image and now i have to generate the histogram of that gradient map. i did this
[rows, columns, numberOfColorBands] = size(sobelImage);
[pixelCounts, channels] = imhist(sobelImage, 256);
bar(channels, pixelCounts);
xlim([0 255]);
drawnow();
The sobel image is coming good, there is no error. but when i generate the histogram, i can only see a the color scale and a white blank space. can you please tell me how to work through this? and i am asked to only keep certain percentage of pixels from the histogram (e.g. 5% of the highest gradient values) as edge pixels (edgels) and then to use the percentage to find a threshold for the gradient magnitudes. can you please tell me how to solve this?
0 commentaires
Réponse acceptée
Image Analyst
le 2 Mar 2013
Modifié(e) : Image Analyst
le 2 Mar 2013
Is your sobel image the true sobel edge filtered image, or is it the thresholded, skeletonized image returned by edge()? If it's the latter, well, that has only two values. Is sobelImage uint8 or double? You can use cumsum() to find the top 5% of the pixels
cdf = cumsum(pixelCounts) / sum(pixelCounts);
index95 = find(cdf>= 0.95, 1, 'first');
gl95 = channels(index95);
5 commentaires
Image Analyst
le 3 Mar 2013
Alright, time for a full demo:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');
if ~hasIPT
% User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
button = menu('Use which demo image?', 'CameraMan', 'Moon', 'Eight', 'Coins', 'Pout');
if button == 1
baseFileName = 'cameraman.tif';
elseif button == 2
baseFileName = 'moon.tif';
elseif button == 3
baseFileName = 'eight.tif';
elseif button == 4
baseFileName = 'coins.png';
else
baseFileName = 'pout.tif';
end
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
SobelX = conv2(double(grayImage), maskX, 'same');
SobelY = conv2(double(grayImage), maskY, 'same');
sobelImage = sqrt(SobelX.^2 + SobelY.^2);
% Display the image.
subplot(2, 2, 2);
imshow(sobelImage, []);
title('Sobel-Filtered Image', 'FontSize', fontSize);
% direction = atan(SobelY./SobelX);
% Threshold the image to find edges less than 10,
% in other words, the smooth areas.
binaryImage = sobelImage < 10;
% Display the image.
subplot(2, 2, 3);
imshow(binaryImage, []);
title('Thresholded Image (White = Smooth areas)', 'FontSize', fontSize);
sobelImage(binaryImage) = 0;
% Display the image.
subplot(2, 2, 4);
imshow(sobelImage, []);
title('Masked (smooth areas zeroed out)', 'FontSize', fontSize);
Plus de réponses (2)
Yahye abukar
le 11 Mai 2016
hellow sir i run your code it works fine, but i need to change this code into Prewitt's and Roberts Operators, how i can change this code? thanks
1 commentaire
Image Analyst
le 12 Mai 2016
Yahye, simply use imgradient() - those methods are options for this function built-in to the Image Processing Toolbox.
Voir également
Catégories
En savoir plus sur Orange dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!