作者GN00505257 (...)
看板C_and_CPP
标题[问题] opencv范例请教
时间Thu Aug 13 22:53:53 2009
#include "stdafx.h"
#include <cv.h>
#include <highgui.h>
int main( int argc, char** argv ) {
IplImage* image;
image=cvLoadImage("image", 1);
{
// Compute the HSV image and decompose it into separate planes.
//
IplImage* hsv = cvCreateImage( cvGetSize(image), 8, 3 );
cvCvtColor( image, hsv, CV_BGR2HSV );
IplImage* h_plane = cvCreateImage( cvGetSize(image), 8, 1 );
IplImage* s_plane = cvCreateImage( cvGetSize(image), 8, 1 );
IplImage* v_plane = cvCreateImage( cvGetSize(image), 8, 1 );
IplImage* planes[] = { h_plane, s_plane };
cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
// Build the histogram and compute its contents.
//
int h_bins = 30, s_bins = 32;
CvHistogram* hist;
{
int hist_size[] = { h_bins, s_bins };
float h_ranges[] = { 0, 180 }; // hue is [0,180]
float s_ranges[] = { 0, 255 };
float* ranges[] = { h_ranges, s_ranges };
hist = cvCreateHist(
2,
hist_size,
CV_HIST_ARRAY,
ranges,
1
);
}
cvCalcHist( planes, hist, 0, 0 ); //Compute histogram
cvNormalizeHist( hist, 1.0 ); //Normalize it
// Create an image to use to visualize our histogram.
//
int scale = 10;
IplImage* hist_img = cvCreateImage(cvSize( h_bins * scale, s_bins * scale )
,8 , 3);
cvZero( hist_img );
// populate our visualization with little gray squares.
//
float max_value = 0;
cvGetMinMaxHistValue( hist, 0, &max_value, 0, 0 );
for( int h = 0; h < h_bins; h++ ) {
for( int s = 100; s < s_bins; s++ ) {
float bin_val = cvQueryHistValue_2D( hist, h, s );
int intensity = cvRound( bin_val * 255 / max_value );
cvRectangle(
hist_img,
cvPoint( h*scale, s*scale ),
cvPoint( (h+1)*scale - 1, (s+1)*scale - 1),
CV_RGB(intensity,intensity,intensity),
CV_FILLED
);
}
}
cvNamedWindow( "HSVtest1", 1 );
cvShowImage( "HSVtest1", image );
cvWaitKey(0);
}
}
这是从书上拿下来练习的范例
想练习如何让图片只显示想要的HUE值
但很多部分看不懂为什麽要这麽做
以及函数的功用
烦请高手帮个忙
谢谢@@
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 163.25.118.133
1F:→ Jockey66666:先查查document吧,应该不会全部都看不懂吧 08/13 23:34
2F:→ Jockey66666:不知道你不懂的地方是哪一段,讲清楚点比较好回答 08/13 23:35
※ 编辑: GN00505257 来自: 163.25.118.133 (08/14 00:33)
3F:→ GN00505257:大概是标示起来的地方 08/14 00:34
4F:→ GN00505257:以及不知道为什麽要使用直方图做转换 08/14 00:35
5F:→ Jockey66666:直方图只是视觉化H跟S的值而已 08/14 01:07
6F:→ Jockey66666:这范例不就是要教你怎麽使用histogram吗? 08/14 01:08
7F:→ GN00505257:抱歉因为我是先找跟图像空间转换相关的范例 08/16 01:42
8F:→ GN00505257:所以也没特别去注意范例的真正用意...现在知道了.谢谢 08/16 01:42