Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8629641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:51:27+00:00 2026-06-12T08:51:27+00:00

I am trying to implement contrast stretching function in php gd and I want

  • 0

I am trying to implement contrast stretching function in php gd and I want to set 5th percent of the image as min value and 95th as max value. Anyone know how to get the histogram and those values using php gd?

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:51:28+00:00Added an answer on June 12, 2026 at 8:51 am

    I’m afraid you’ll need to count pixels one by one.

    $gd = // Create image of same size, copy original image into $gd
    // Make grayscale
    imageFilter($gd, IMG_FILTER_GRAYSCALE);
    
    $pre = array_fill(0, 256, 0);
    
    for ($y = 0; $y < ImageSY($gd); $y++)
    {
        for ($x = 0; $x < ImageSX($gd); $x++)
        {
            $luma = (imageColorAt($x, $y) & 0xFF); // Grayscale, so R=G=B=luma
            $pre[$luma]++;
        }
    }
    
    // Then you need to build the cumulative histogram:
    
    $max = $pre[0];
    $hist[0] = $pre[0];
    for ($i = 1; $i < 256; $i++)
    {
        $hist[$i] = $hist[$i-1]+$pre[$i];
        if ($max < $pre[$i])
                $max = $pre[$i];
    }
    // Now scale to 100%
    for ($i = 0; $i < 256; $i++)
    {
        $hist[$i] = ($hist[$i]*100.0)/((float)$hist[255]);
        if ($hist[$i] >= 5)
            if ((0 == $i) || ($hist[$i-1] < 5))
                print "Fifth percentile ends at index $i (not included)\n";
        if ($hist[$i] >= 95)
            if ($hist[$i-1] < 95)
                print "Ninety-fifth percentile begins at index $i (included)\n";
    }
    
    // Create graphics, just to check.
    // Frequency is red, cumulative histogram is green
    
    $ck = ImageCreateTrueColor(255, 100);
    $w  = ImageColorAllocate($ck, 255, 255, 255);
    $r  = ImageColorAllocate($ck, 255,   0,   0);
    $g  = ImageColorAllocate($ck,   0, 255,   0);
    ImageFilledRectangle($ck, 0, 0, 255, 100, $w);
    for ($i = 0; $i < 256; $i++)
    {
        ImageLine($ck, $i, 100-$hist[$i], $i, 100, $g);
        ImageLine($ck, $i, 100.0-100.0*((float)$pre[$i]/$max), $i, 100, $r);
    }
    ImagePNG($ck, 'histograms.png');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok well I'm trying implement something similar to the 'undo' function in many image
Trying to implement 3-layer (not: tier, I just want to separate my project logically,
trying to implement the following but its not working <script> function loadmap(lat,lng) { //alert(lat,lang);
I am doing short-term contract work for a company that is trying to implement
I'm trying implement A* Start path finding in my games(which are written with JavaScript,
I am trying implement a most recent widget into my tumblr post. So far,
Trying to implement LoaderManager + CursorLoader. In onFinish method adapter should swap its cursor
Trying to implement google C2DM service. registrationIntent.putExtra(app, PendingIntent.getBroadcast(context,0,new Intent(), 0)); registrationIntent.putExtra(sender,example@gmail.com); context.startService(registrationIntent); Almost every
Trying to implement a search similar to here .This searches properties based on city,locality,property
Trying to implement a rating system of users and postings. What is the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.