Generating histogram of a gradient image: histogram bar shows a white blank space within a histogram bar

4 views (last 30 days)
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?

Accepted Answer

Image Analyst
Image Analyst on 2 Mar 2013
Edited: Image Analyst on 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 Comments
Image Analyst
Image Analyst on 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);
Sat m
Sat m on 3 Mar 2013
hello sir, i have edited the code. now the sobel image code is
grayImage= rgb2gray(rgbImage);
size(grayImage);
C=double(grayImage);
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
resX = conv2(C, maskX, 'same');
resY = conv2(C, maskY, 'same');
sobelImage = sqrt(resX.^2 + resY.^2);
direction = atan(resY./resX);
thresh = sobelImage < 10;
sobelImage(thresh) = 0;
imshow(sobelImage,[]);
drawnow();
but it is showing the same blank image with scale.

Sign in to comment.

More Answers (2)

Sat m
Sat m on 3 Mar 2013
hello sir, i have edited the code. now the sobel image code is
grayImage= rgb2gray(rgbImage);
size(grayImage);
C=double(grayImage);
maskX = [-1 0 1 ; -2 0 2; -1 0 1];
maskY = [-1 -2 -1 ; 0 0 0 ; 1 2 1] ;
resX = conv2(C, maskX, 'same');
resY = conv2(C, maskY, 'same');
sobelImage = sqrt(resX.^2 + resY.^2);
direction = atan(resY./resX);
thresh = sobelImage < 10;
sobelImage(thresh) = 0;
imshow(sobelImage,[]);
drawnow();
but it is showing the same blank image with scale.
  17 Comments

Sign in to comment.


Yahye abukar
Yahye abukar on 11 May 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

Community Treasure Hunt

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

Start Hunting!