Re: [問題] opencv範例請教
※ 引述《GN00505257 (...)》之銘言:
: #include "stdafx.h"
: #include <cv.h>
: #include <highgui.h>
: int main( int argc, char** argv ) {
: IplImage* image;
: image=cvLoadImage("image", 1);
讀圖檔,RGB色彩空間
:
: // Compute the HSV image and decompose it into separate planes.
: //
: IplImage* hsv = cvCreateImage( cvGetSize(image), 8, 3 );
初始化一個3 Channels的空白圖片(hsv色彩空間)
: cvCvtColor( image, hsv, CV_BGR2HSV );
將讀到的圖檔轉換成HSV色彩空間
: IplImage* h_plane = cvCreateImage( cvGetSize(image), 8, 1 );
: IplImage* s_plane = cvCreateImage( cvGetSize(image), 8, 1 );
: IplImage* v_plane = cvCreateImage( cvGetSize(image), 8, 1 );
初始化三個1 Channel空白圖片,分別代表h,s,v的空白圖片
: IplImage* planes[] = { h_plane, s_plane };
用動態陣列合併h跟s的空間平面
: cvCvtPixToPlane( hsv, h_plane, s_plane, v_plane, 0 );
這邊是將hsv 3 Channels的內容資訊置入h,s,v單通道的空白圖片
: // Build the histogram and compute its contents.
: //
: int h_bins = 30, s_bins = 32;
直方圖量化的區塊等分數,分別切割成30塊以及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 };
設定直方圖的範圍hue值0~180度,saturation值0~255
: hist = cvCreateHist(
: 2,
: hist_size,
: CV_HIST_ARRAY,
: ranges,
: 1
: );
兩個維度的輸入,分別是Hue以及saturation色彩空間
:
: cvCalcHist( planes, hist, 0, 0 ); //Compute histogram
計算hue以及saturation色彩空間平面
: cvNormalizeHist( hist, 1.0 ); //Normalize it
將它重新量化到0~1的範圍之間
: // 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值
: 但很多部分看不懂為什麼要這麼做
: 以及函數的功用
: 煩請高手幫個忙
: 謝謝@@
簡單來講,他只是一個畫出Hue以及saturation二維統計直方圖的程式
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.133.9.150
※ 編輯: wa120 來自: 140.133.9.150 (08/14 11:05)
推
08/16 01:50, , 1F
08/16 01:50, 1F
→
08/16 01:51, , 2F
08/16 01:51, 2F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章