Search code examples
imagematlabimage-processinghistogram

Histogram of an image but without considering the first k pixels


I would like to create a histogram of an image but without considering the first k pixels.

Eg: 50x70 image and k = 40, the histogram is calculated on the last 3460 pixels. The first 40 pixels of the image are ignored. The order to scan the k pixels is a raster scan order (starting from the top left and proceeds by lines).

Another example is this, where k=3: enter image description here

Obviously I can't assign a value to those k pixels otherwise the histogram would be incorrect. Honestly I have no idea how to start.

How can I do that? Thanks so much


Solution

  • The vectorized solution to your problem would be

    function [trimmedHist]=histKtoEnd(image,k)
    imageVec=reshape(image.',[],1);          % Transform the image into a vector. Note that the image has to be transposed in order to achieve the correct order for your counting
    imageWithoutKPixels=imageVec(k+1:end);   % Create vector without first k pixels
    trimmedHist=accumarray(imageWithoutKPixels,1);  % Create the histogram using accumarray
    

    If you got that function on your workingdirectory you can use

    image=randi(4,4,4)
    k=6;
    trimmedHistogram=histKtoEnd(image,k)
    

    to try it.

    EDIT: If you just need the plot you can also use histogram(imageWithoutKPixels) in the 4th row of the function I wrote