當(dāng)前位置:
首頁 >
使用openCV画出一幅图像的直方图
發(fā)布時(shí)間:2023/12/8
37
豆豆
生活随笔
收集整理的這篇文章主要介紹了
使用openCV画出一幅图像的直方图
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
// 對單通道圖像做直方圖
// #include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <ctype.h> int main( int argc, char** argv )
{ IplImage *src = 0; IplImage *histimg = 0; CvHistogram *hist = 0; int hdims = 50; // 劃分HIST的個(gè)數(shù),越高越精確 float hranges_arr[] = {0,255}; float* hranges = hranges_arr; int bin_w; float max_val; int i; if( argc != 2 || (src=cvLoadImage(argv[1], 0)) == NULL) // force to gray imagereturn -1; cvNamedWindow( "Histogram", 1 ); hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 ); // 計(jì)算直方圖 histimg = cvCreateImage( cvSize(320,200), 8, 3 ); cvZero( histimg ); cvCalcHist( &src, hist, 0, 0 ); // 計(jì)算直方圖 cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 ); // 只找最大值 cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 ); // 縮放 bin 到區(qū)間 [0,255]cvZero( histimg ); bin_w = histimg->width / hdims; // hdims: 條的個(gè)數(shù),則 bin_w 為條的寬度 // 畫直方圖 for( i = 0; i < hdims; i++ ) { double val = ( cvGetReal1D(hist->bins,i)*histimg->height/255 ); CvScalar color = CV_RGB(255,255,0); //(hsv2rgb(i*180.f/hdims); cvRectangle( histimg, cvPoint(i*bin_w,histimg->height), cvPoint((i+1)*bin_w,(int)(histimg->height - val)), color, 1, 8, 0 ); } cvShowImage( "Histogram", histimg ); cvWaitKey(0); cvDestroyWindow("Histogram"); cvReleaseImage( &src ); cvReleaseImage( &histimg ); cvReleaseHist ( &hist ); return 0; }
總結(jié)
以上是生活随笔為你收集整理的使用openCV画出一幅图像的直方图的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前锋html5费用,足坛转会费最高的5位
- 下一篇: e3 v3服务器芯片组,最保值的E3-1