Re: [問題] opencv範例請教
※ 引述《GN00505257 (...)》之銘言:
僅針對紅色部份進行解說
註:還不太熟怎樣用顏色去做強調...
: #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 );
: //
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
對於以上cvCvtPixToPlane, 內容在於把讀取到的image, 透過
cvCvtColor將image轉換為HSV, 透過cvCvtPixToPlane轉換到前面
所設定的h_plane, s_plane, v_plane.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: cvCalcHist( planes, hist, 0, 0 ); //Compute histogram
//
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
這邊在於利用cvCalcHist提供計算Historgram的函式, 可以把
planes所指向的影像之Historgram存放於CvHistogram的結構中,
至於後面參數部份, 請參閱Doc
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: cvNormalizeHist( hist, 1.0 ); //Normalize it
//
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
如果有學過影像處理, 應該都會知道Histogram的正規化, 這函式
就是在處理這些東西
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: // 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 );
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
這函式, 可以直接取得CvHistogram中最大的元素或是最小元素
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: 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
: );
: }
: }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
這部份應該是你的死角, 簡單說他只是把Histogram藉由畫圖表示出來而已.
主要是將數值量化為你的視窗可以裝得範圍, 拿筆起來算一下應該就會比較
明白, cvRectangle是在繪圖矩形, 但我認為cvRect應該也有同樣效果. 看
每個人的取捨. 這邊就是將量化後的數值進行座標轉換到視窗上, 要注意
繪圖的方向是從左上角開始計算或是右下角開始計算.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
以上, 若有遺漏或是錯誤的地方, 煩請指教更正, 謝謝.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.124.182.10
→
08/14 10:15, , 1F
08/14 10:15, 1F
→
08/14 14:19, , 2F
08/14 14:19, 2F
→
08/14 14:49, , 3F
08/14 14:49, 3F
推
08/15 02:00, , 4F
08/15 02:00, 4F
→
08/16 01:43, , 5F
08/16 01:43, 5F
討論串 (同標題文章)