Professional Documents
Culture Documents
h"
#include "highgui.h"
#include <stdio.h>
IplImage *image=0, *image2=0;
void his_equi() {
int H[256]={0,}; //Histogram Array
int HC[256]={0,}; //Cumulative Histogram Array
int T[256]; //Transfer Function Array
uchar *pix; // To store pixel value temporarily
// histogram
for ( int posY=0; posY<image->height;posY++) {
for ( int posX=0; posX<image->width;posX++) {
pix=&((uchar *)(image2->imageData+posY*image2->widthStep))[posX];
H[*pix]++;
}
}
// Cumulative histogram
HC[0]=H[0];
for (int i=1; i<256;i++) {
HC[i]=HC[i-1]+H[i];
}
// transfer function
for (int i=0;i<256;i++) {
T[i]=(int)round((255.00/((image->width)*(image->height)))*HC[i]);
}
// adapting pixel of original image according to transfer function.
for ( int posY=0; posY<image->height;posY++) {
for ( int posX=0; posX<image->width;posX++) {
pix=&((uchar *)(image2->imageData+posY*image2->widthStep))[posX];
*pix=T[*pix];
}
}
}
===============================
BGR to Gray
------------------------void bgr2gray( const Mat& bgr, Mat& gray )
{
gray = Mat::zeros(bgr.rows, bgr.cols, CV_8UC1 );
for (int y = 0; y < bgr.rows; y++) {
for (int x = 0; x < bgr.cols; x++) {
unsigned char blue = bgr.data[3*bgr.cols*y + x]*0.114f; //0.114
unsigned char green = bgr.data[3*bgr.cols*y + x+sizeof(char)]*0.587f;
unsigned char red = bgr.data[3*bgr.cols*y + x+2*sizeof(char)]*0.299f;
gray.data[gray.cols*y+x] = red + green + blue;
}
}
}
=================================