Skip to content

Instantly share code, notes, and snippets.

@EduardoLezano
Created November 30, 2016 00:00
Show Gist options
  • Save EduardoLezano/42fa5f6fe4be4e4dbb23096b798e4019 to your computer and use it in GitHub Desktop.
Save EduardoLezano/42fa5f6fe4be4e4dbb23096b798e4019 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv/cv.h>
using namespace cv;
using namespace std;
Mat calcularHistograma(Mat imageRecived);
int main(int argc, char *argv[]){
Mat src, histograma;
src = imread("/home/eduardo/Imágenes/Imagen1.jpg",1);
if(!src.data){cout<<"no image";}
namedWindow("Imagen",WINDOW_AUTOSIZE);
namedWindow("Histograma",WINDOW_AUTOSIZE);
imshow("Imagen",src);
imshow("Histograma",histograma);
}
///
/// FUNCION PARA EL CALCULO DE HISTOGRAMAS
///
///
Mat calcularHistograma(Mat imRec){
vector<Mat> planosBGR;
split(imRec,planosBGR);
Mat bHist, gHist, rHist;
int histSize = 255;
float range[] = {0,255};
const float* histRanges = {range};
bool uniform = TRUE, accumulate = FALSE;
calcHist(&planosBGR[0],1,0,Mat(),bHist,1,&histSize,&histRanges,uniform,accumulate);
calcHist(&planosBGR[1],1,0,Mat(),gHist,1,&histSize,&histRanges,uniform,accumulate);
calcHist(&planosBGR[2],1,0,Mat(),rHist,1,&histSize,&histRanges,uniform,accumulate);
// calcHist(const Mat* images, int nimages, const int* channels, InputArray mask, SparseMat& hist, int dims, const int* histSize, const float** ranges, bool uniform=true, bool accumulate=false )
int hist_w = 512;
int hist_h = 400;
int delta = cvRound((double) hist_w/histSize);
Mat histImgen(hist_h,hist_w,CV_8UC3,Scalar(0,0,0));
normalize(bHist, bHist, 0,histImgen.rows,NORM_MINMAX, -1, Mat());
normalize(gHist, gHist, 0,histImgen.rows,NORM_MINMAX, -1, Mat());
normalize(rHist, rHist, 0,histImgen.rows,NORM_MINMAX, -1, Mat());
for(int i = 1; i < histSize ; i++){
line( histImgen,
Point( delta*(i-1), hist_h - cvRound(bHist.at<float>(i-1)) ) ,
Point( delta*(i), hist_h - cvRound(bHist.at<float>(i)) ),
Scalar( 255, 0, 0), 2, 8, 0 );
line( histImgen, Point( delta*(i-1), hist_h - cvRound(gHist.at<float>(i-1)) ) ,
Point( delta*(i), hist_h - cvRound(gHist.at<float>(i)) ),
Scalar( 0, 255, 0), 2, 8, 0 );
line( histImgen, Point( delta*(i-1), hist_h - cvRound(rHist.at<float>(i-1)) ) ,
Point( delta*(i), hist_h - cvRound(rHist.at<float>(i)) ),
Scalar( 0, 0, 255), 2, 8, 0 );
}
return histImgen;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment