Complete image contrast enhancement code under opencv

Source: Internet
Author: User

// Enforce. cpp: defines the entry point for the console application.
//

# Include "stdafx. H"

# Include "cv. H"
# Include "highgui. H"
# Include
# Include
Int imagestretchbyhistogram (iplimage * SRC, iplimage * DST );

Int _ tmain (INT argc, _ tchar * argv [])
{
Iplimage * pimg;
Pimg = cvloadimage ("C: \ lena.jpg",-1 );

// Create a grayscale image
Iplimage * grayimage = cvcreateimage (cvgetsize (pimg), ipl_depth_8u, 1 );
Iplimage * dstgrayimage = cvcreateimage (cvgetsize (pimg), ipl_depth_8u, 1 );
Cvcvtcolor (pimg, grayimage, cv_bgr2gray );
Imagestretchbyhistogram (grayimage, dstgrayimage );

Cvnamedwindow ("dstgrayimage", 1); // create a window
Cvnamedwindow ("grayimage", 1); // create a window
Cvshowimage ("dstgrayimage", dstgrayimage); // display the image
Cvshowimage ("grayimage", grayimage); // display the image
Cvwaitkey (0); // wait for the key

Cvdestroywindow ("dstgrayimage"); // destroy the window
Cvdestroywindow ("grayimage"); // destroy the window
Cvreleaseimage (& pimg); // release the image
Cvreleaseimage (& grayimage); // release the image
Cvreleaseimage (& dstgrayimage); // release the image

Return 0;
}

Int imagestretchbyhistogram (iplimage * SRC, iplimage * DST)
/*************************************** **********
Function:
Description: due to the poor image quality of the camera, image enhancement is required based on the histogram,
Stretch the grayscale Domain value to 0-255.
Cballs:
Called:
Input: Single Channel grayscale image
Output: Single Channel grayscale images of the same size
Return:
Others: http://www.xiaozhou.net/ReadNews.asp? Newsid= 771
Date: 2007-1-5
**************************************** *********/
{
// P [] the appearance probability of each gray level of the image;
// P1 [] stores the probability and prior to each gray level for histogram transformation;
// Num [] Number of gray-level occurrences of the image;

Assert (SRC-> width = DST-> width );
Float P [1, 256], P1 [2, 256], num [2, 256];
// Clear three Arrays
Memset (p, 0, sizeof (p ));
Memset (P1, 0, sizeof (P1 ));
Memset (Num, 0, sizeof (Num ));

Int Height = Src-> height;
Int width = Src-> width;
Long wmulh = height * width;

// Find the number of times each gray level of the image appears
// To do use OpenMP
For (INT x = 0; X {
For (INT y = 0; y {
Uchar v = (uchar *) (SRC-> imagedata + Src-> widthstep * y) [x];
Num [v] ++;
}
}

// Calculate the appearance probability of each gray level of the stored image
For (INT I = 0; I <256; I ++)
{
P [I] = num [I]/wmulh;
}

// Calculate the probability and
For (INT I = 0; I <256; I ++)
{
For (int K = 0; k <= I; k ++)
P1 [I] + = P [k];
}

// Histogram Transformation
// To do use OpenMP
For (INT x = 0; X {
For (INT y = 0; y {
Uchar v = (uchar *) (SRC-> imagedata + Src-> widthstep * y) [x];
(Uchar *) (DST-> imagedata + DST-> widthstep * y) [x] = P1 [v] * 255 + 0.5;
}
}

Return 0;

}

The imagestretchbyhistogram function comes from:

Http://blog.csdn.net/hardVB/archive/2007/01/05/1474880.aspx

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/soarnic/archive/2008/09/26/2985116.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.