日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用opencv作物件识别(一) —— 积分直方图加速HOG特征计算

發(fā)布時間:2025/7/25 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用opencv作物件识别(一) —— 积分直方图加速HOG特征计算 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用opencv作物件識別(一) —— 積分直方圖加速HOG特征計算

  • 博客分類:?
  • 圖像識別、機器學習、數(shù)據(jù)挖掘
CC++C#? 方向梯度直方圖(Histograms of Oriented Gradients,簡稱HOG特征)結合支持向量機( support vector machine, 簡稱SVM),被廣泛應用于圖像識別中,尤其在行人檢測中獲得了極大的成功。?

積分直方圖可以用于快速計算原始圖像矩形區(qū)域內的HOG特征。積分直方圖的概念類似與viola和jones在臉部識別中所用的積分圖像。?

下面的代碼給出了,對于一幅給定的圖像,如何快速計算積分直方圖,以及如何使用其進行HOG特征的演算(關鍵處以給出注釋):?

C代碼??
  • /*Function?to?calculate?the?integral?histogram*/??
  • IplImage**?calculateIntegralHOG(IplImage*?in)??
  • {??
  • ????/*Convert?the?input?image?to?grayscale*/??
  • ????IplImage*?img_gray?=?cvCreateImage(cvGetSize(in),?IPL_DEPTH_8U,1);??
  • ????cvCvtColor(in,?img_gray,?CV_BGR2GRAY);??
  • ????cvEqualizeHist(img_gray,img_gray);??
  • ??
  • /*Calculate?the?derivates?of?the?grayscale?image?in?the?x?and?y?directions?using?a?sobel?operator?and?obtain?2?gradient?images?for?the?x?and?y?directions*/??
  • ??
  • ????IplImage?*xsobel,?*ysobel;??
  • ????xsobel?=?doSobel(img_gray,?1,?0,?3);??
  • ????ysobel?=?doSobel(img_gray,?0,?1,?3);??
  • ????cvReleaseImage(&img_gray);??
  • ??
  • ??
  • /*?Create?an?array?of?9?images?(9?because?I?assume?bin?size?20?degrees?and?unsigned?gradient?(?180/20?=?9),?one?for?each?bin?which?will?have?zeroes?for?all?pixels,?except?for?the?pixels?in?the?original?image?for?which?the?gradient?values?correspond?to?the?particular?bin.?These?will?be?referred?to?as?bin?images.?These?bin?images?will?be?then?used?to?calculate?the?integral?histogram,?which?will?quicken?the?calculation?of?HOG?descriptors?*/??
  • ??
  • ????IplImage**?bins?=?(IplImage**)?malloc(9?*?sizeof(IplImage*));??
  • ????for?(int?i?=?0;?i?<?9?;?i++)?{??
  • ????????bins[i]?=?cvCreateImage(cvGetSize(in),?IPL_DEPTH_32F,1);??
  • ????????cvSetZero(bins);??
  • ????}??
  • ??
  • ??
  • /*?Create?an?array?of?9?images?(?note?the?dimensions?of?the?image,?the?cvIntegral()?function?requires?the?size?to?be?that),?to?store?the?integral?images?calculated?from?the?above?bin?images.?These?9?integral?images?together?constitute?the?integral?histogram?*/??
  • ??
  • ????IplImage**?integrals?=?(IplImage**)?malloc(9?*?sizeof(IplImage*));???
  • ????for?(int?i?=?0;?i?<?9?;?i++)?{??
  • ????????integrals[i]?=?cvCreateImage(cvSize(in->width?+?1,?in->height?+?1),??
  • ????????IPL_DEPTH_64F,1);??
  • ????}??
  • ??
  • /*?Calculate?the?bin?images.?The?magnitude?and?orientation?of?the?gradient?at?each?pixel?is?calculated?using?the?xsobel?and?ysobel?images.{Magnitude?=?sqrt(sq(xsobel)?+?sq(ysobel)?),?gradient?=?itan?(ysobel/xsobel)?}.?Then?according?to?the?orientation?of?the?gradient,?the?value?of?the?corresponding?pixel?in?the?corresponding?image?is?set?*/??
  • ??
  • ????int?x,?y;??
  • ????float?temp_gradient,?temp_magnitude;??
  • ????for?(y?=?0;?y?<?in->height;?y++)?{??
  • ??
  • /*?ptr1?and?ptr2?point?to?beginning?of?the?current?row?in?the?xsobel?and?ysobel?images?respectively.?ptrs?point?to?the?beginning?of?the?current?rows?in?the?bin?images?*/??
  • ??
  • ????????float*?ptr1?=?(float*)?(xsobel->imageData?+?y?*?(xsobel->widthStep));??
  • ????????float*?ptr2?=?(float*)?(ysobel->imageData?+?y?*?(ysobel->widthStep));??
  • ????????float**?ptrs?=?(float**)?malloc(9?*?sizeof(float*));??
  • ????????for?(int?i?=?0;?i?<?9?;i++){??
  • ????????????ptrs[i]?=?(float*)?(bins[i]->imageData?+?y?*?(bins->widthStep));??
  • ????????}??
  • ??
  • /*For?every?pixel?in?a?row?gradient?orientation?and?magnitude?are?calculated?and?corresponding?values?set?for?the?bin?images.?*/??
  • ??
  • ????????for?(x?=?0;?x?<in->width;?x++)?{??
  • ??
  • /*?if?the?xsobel?derivative?is?zero?for?a?pixel,?a?small?value?is?added?to?it,?to?avoid?division?by?zero.?atan?returns?values?in?radians,?which?on?being?converted?to?degrees,?correspond?to?values?between?-90?and?90?degrees.?90?is?added?to?each?orientation,?to?shift?the?orientation?values?range?from?{-90-90}?to?{0-180}.?This?is?just?a?matter?of?convention.?{-90-90}?values?can?also?be?used?for?the?calculation.?*/??
  • ??
  • ????????????if?(ptr1[x]?==?0){??
  • ????????????????temp_gradient?=?((atan(ptr2[x]?/?(ptr1[x]?+?0.00001)))?*?(180/???PI))?+?90;??
  • ????????????}??
  • ????????????else{??
  • ????????????????temp_gradient?=?((atan(ptr2[x]?/?ptr1[x]))?*?(180?/?PI))?+?90;??
  • ????????????}??
  • ????????????temp_magnitude?=?sqrt((ptr1[x]?*?ptr1[x])?+?(ptr2[x]?*?ptr2[x]));??
  • ??
  • /*The?bin?image?is?selected?according?to?the?gradient?values.?The?corresponding?pixel?value?is?made?equal?to?the?gradient?magnitude?at?that?pixel?in?the?corresponding?bin?image?*/??
  • ??
  • ????????????if?(temp_gradient?<=?20)?{??
  • ????????????????ptrs[0][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?40)?{??
  • ????????????????ptrs[1][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?60)?{??
  • ????????????????ptrs[2][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?80)?{??
  • ????????????????ptrs[3][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?100)?{??
  • ????????????????ptrs[4][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?120)?{??
  • ????????????????ptrs[5][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?140)?{??
  • ????????????????ptrs[6][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?if?(temp_gradient?<=?160)?{??
  • ????????????????ptrs[7][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????????else?{??
  • ????????????????ptrs[8][x]?=?temp_magnitude;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??
  • ????cvReleaseImage(&xsobel);??
  • ????cvReleaseImage(&ysobel);??
  • ??
  • /*Integral?images?for?each?of?the?bin?images?are?calculated*/??
  • ??
  • ????for?(int?i?=?0;?i?<9?;?i++){??
  • ????????cvIntegral(bins[i],?integrals[i]);??
  • ????}??
  • ??
  • ????for?(int?i?=?0;?i?<9?;?i++){??
  • ????????cvReleaseImage(&bins[i]);??
  • ????}??
  • ??
  • /*The?function?returns?an?array?of?9?images?which?consitute?the?integral?histogram*/??
  • ??
  • ????return?(integrals);??
  • ??
  • }??


  • 如何使用上面的函數(shù)來計算9維的方向梯度直方圖呢?如下:?

    C代碼??
  • /*?The?following?function?takes?as?input?the?rectangular?cell?for?which?the?histogram?of?oriented?gradients?has?to?be?calculated,?a?matrix?hog_cell?of?dimensions?1x9?to?store?the?bin?values?for?the?histogram,?the?integral?histogram,?and?the?normalization?scheme?to?be?used.?No?normalization?is?done?if?normalization?=?-1?*/??
  • ??
  • void?calculateHOG_rect(CvRect?cell,?CvMat*?hog_cell,??
  • IplImage**?integrals,?int?normalization)?{??
  • ??
  • /*?Calculate?the?bin?values?for?each?of?the?bin?of?the?histogram?one?by?one?*/??
  • ??
  • ????for?(int?i?=?0;?i?<?9?;?i++){??
  • ??
  • ????????float?a?=((double*)(integrals[i]->imageData?+?(cell.y)?*?(integrals->??
  • ????????????widthStep)))[cell.x];??
  • ??
  • ????????float?b?=?((double*)?(integrals->imageData?+?(cell.y?+?cell.height)?*???
  • ????????????(integrals->widthStep)))[cell.x?+?cell.width];??
  • ??
  • ????????float?c?=?((double*)?(integrals->imageData?+?(cell.y)?*?(integrals-???
  • ????????????>widthStep)))[cell.x?+?cell.width];??
  • ??
  • ????????float?d?=?((double*)?(integrals->imageData?+?(cell.y?+?cell.height)?*???
  • ????????????(integrals->widthStep)))[cell.x];??
  • ??
  • ????????((float*)?hog_cell->data.fl)?=?(a?+?b)?-?(c?+?d);??
  • ??
  • ????}??
  • ??
  • ??
  • ????/*Normalize?the?matrix*/??
  • ????if?(normalization?!=?-1){??
  • ????????cvNormalize(hog_cell,?hog_cell,?1,?0,?normalization);??
  • ????}??
  • ??
  • }??
  • 總結

    以上是生活随笔為你收集整理的使用opencv作物件识别(一) —— 积分直方图加速HOG特征计算的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。