Skip to content

Instantly share code, notes, and snippets.

@Hamza5
Created October 23, 2016 18:27
Show Gist options
  • Save Hamza5/4094e672549aa2791ae2eeae2a2260ca to your computer and use it in GitHub Desktop.
Save Hamza5/4094e672549aa2791ae2eeae2a2260ca to your computer and use it in GitHub Desktop.
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <stdio.h>
#include <stdlib.h>
void etirerHistogramme(IplImage ** img){
IplImage * image = *img;
if (image->nChannels != 1) return;
int p_min = 255;
int p_max = 0;
CvScalar pixel;
for (int x = 0; x < image->width; x++) {
for (int y = 0; y < image->height; y++) {
pixel = cvGet2D(image, y, x);
if (pixel.val[0] < p_min) p_min = pixel.val[0];
if (pixel.val[0] > p_max) p_max = pixel.val[0];
}
}
printf("Min = %d, Max = %d\n", p_min, p_max);
for (int x = 0; x < image->width; x++) {
for (int y = 0; y < image->height; y++) {
pixel = cvGet2D(image, y, x);
pixel.val[0] = 255 * (pixel.val[0] - p_min) / (p_max - p_min);
cvSet2D(image, y, x, pixel);
}
}
}
int main(int argc, char const *argv[]) {
if (argc == 2 || argc == 3) {
IplImage * original = cvLoadImage(argv[1]);
double ratio = 1;
if (argc == 3) {
ratio = atof(argv[2]);
}
IplImage * colored = cvCreateImage(
cvSize(original->width * ratio, original->height * ratio),
original->depth,
original->nChannels
);
cvResize(original, colored);
IplImage * grayscale = cvCreateImage(cvGetSize(colored), colored->depth, 1);
int flip = 0;
if (colored->origin != IPL_ORIGIN_TL) flip = CV_CVTIMG_FLIP;
cvConvertImage(colored, grayscale, flip);
IplImage * normalized = cvCloneImage(grayscale);
etirerHistogramme(&normalized);
cvNamedWindow("Colored", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Grayscale", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Normalized", CV_WINDOW_AUTOSIZE);
cvShowImage("Colored", colored);
cvShowImage("Grayscale", grayscale);
cvShowImage("Normalized", normalized);
cvWaitKey(0);
cvDestroyAllWindows();
cvReleaseImage(&colored);
cvReleaseImage(&grayscale);
cvReleaseImage(&normalized);
} else {
printf("Usage : %s <image> [ratio]\n", argv[0]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment