Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab hist function for image data

I'm new to Matlab and I want to make my own function that makes the same job as imhist(Displays histogram of image data) but I am a total newbie to this and I don't have any clues how I'm going to develop such function.. I started making something but its very incomplete.

function [ output_args ] = myhist( x )
%MYHIST Summary of this function goes here
%Detailed explanation goes here

x=imread('flower.jpg');

imshow(x);

[c,d]=hist(x(:),0:1:255);
figure,plot(d,c);
figure,plot(c,d);

%figure,imhist(x);
 end

I would be very grateful if you could give me any helpful tips..

like image 437
Dchris Avatar asked Jul 04 '26 08:07

Dchris


1 Answers

Here is my attempt at implementing the IMHIST function. It does not handle all the cases that the official function does, but it should produce very similar results in most cases:

function myimhist(img)
    img = im2uint8(img);

    [count,bin] = hist(img(:), 0:255);
    stem(bin,count, 'Marker','none')

    hAx = gca;
    set(hAx, 'XLim',[0 255], 'XTickLabel',[], 'Box','on')

    %# create axes, and draw grayscale colorbar
    hAx2 = axes('Position',get(hAx,'Position'), 'HitTest','off');
    image(0:255, [0 1], repmat(linspace(0,1,256),[1 1 3]), 'Parent',hAx2)
    set(hAx2, 'XLim',[0 255], 'YLim',[0 1], 'YTick',[], 'Box','on')

    %# resize the axis to make room for the colorbar
    set(hAx, 'Units','pixels')
    p = get(hAx, 'Position');
    set(hAx, 'Position',[p(1) p(2)+26 p(3) p(4)-26])
    set(hAx, 'Units','normalized')

    %# position colorbar at bottom
    set(hAx2, 'Units','pixels')
    p = get(hAx2, 'Position');
    set(hAx2, 'Position',[p(1:3) 26])
    set(hAx2, 'Units','normalized')

    %# link x-limits of the two axes
    linkaxes([hAx;hAx2], 'x')
    set(gcf, 'CurrentAxes',hAx)
end

Let's test it with a sample image:

I = imread('coins.png');
figure(1), myimhist(I), title('myimhist')
figure(2), imhist(I), title('imhist')

myimhist imhist

Note how IMHIST is apparently adjusting the y-limit so as to deal with the two different peaks in the histogram.

like image 118
Amro Avatar answered Jul 06 '26 21:07

Amro