HOG特征提取代码(opencv处理基本数据,c语言实现基本算法)
2017-07-10 17:50
1111 查看
#include<cv.h>
#include<highgui.h>
#include<math.h>
#include<opencv.hpp>
using namespace cv;
using namespace std;
IplImage * GRBtoGrey(IplImage* img)//RGB图像转为灰度图像
{
IplImage* dest=cvCreateImage(cvSize(img->width, img->height),img->depth,1);
uchar* data = (uchar*)img->imageData;
int b,g,r;
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
b = data[i*img->widthStep+j*img->nChannels+0];
g = data[i*img->widthStep+j*img->nChannels+1];
r = data[i*img->widthStep+j*img->nChannels+2];
((uchar*)(dest->imageData+i*dest->widthStep))[j]=0.3*r+0.59*g+0.11*b;
}
}
return dest;
}
IplImage* nomolize(IplImage *img)//必须是灰度图
{
uchar* gM = (uchar*)img->imageData;
int max = 0,min = 255;
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
int num=gM[i*img->widthStep+j*img->nChannels];
if(num>max){
max = num;
}
if(num<min){
min = num;
}
}
}
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
int num = (int)(float)(gM[i*img->widthStep+j*img->nChannels]-min)*
((float)(255)/(float)(max-min));
if(num>255){
num = 255;
}
((uchar*)(img->imageData+i*img->widthStep))[j] = num;
}
}
return img;
}
IplImage *greygamma(IplImage *greyimage){
IplImage *gammaimage = cvCreateImage(cvSize(greyimage->width,greyimage->height),
greyimage->depth,greyimage->nChannels);
uchar* data = (uchar*)greyimage->imageData;
for(int i = 0;i<greyimage->height;i++){
for(int j = 0;j<greyimage->width;j++){
int gamma=data[i*greyimage->widthStep+j*greyimage->nChannels];
((uchar*)(gammaimage->imageData+i*gammaimage->widthStep))[j] = (int)sqrt((float)gamma);
}
}
return nomolize(gammaimage);
}
IplImage *gradientimg(IplImage *img){
uchar * data = (uchar*)img->imageData;
for(int i = 1;i<img->height-1;i++){
for(int j = 1;j<img->width-1;j++){
int Gx = data[i*img->widthStep+(j+1)*img->nChannels]-data[i*img->widthStep+(j-1)*img->nChannels];
int Gy = data[(i+1)*img->widthStep+j*img->nChannels]-data[(i-1)*img->widthStep+j*img->nChannels];
int gradient = (int)sqrt((float)(Gx*Gx+Gy*Gy));
int theta = (int)(atan((float)Gy/(float)Gx)*180/3.14);
((uchar*)(img->imageData+i*img->widthStep))[j] = theta;
}
}
return nomolize(img);
}
int *CellFeature(IplImage *img ,int w,int h){//8*8 cell
int cell_w = 8;
int f_n = 9;
int *feature = new int[f_n];
for(int i = 0;i<f_n;i++){
f_n = 0;
}
for(int i = h;i<h+cell_w;i++){
for(int j = w;j<w+cell_w;j++){
feature[((uchar*)(img->imageData+i*img->widthStep))[j]/40]++;
}
}
return feature;
}
int *HogFeature(IplImage *img){
int cell_w = 8;
int cell_fn = 9;
int cell_wn = img->width/cell_w;
int cell_hn = img->height/cell_w;
int *hog_f = new int[cell_wn*cell_hn*cell_fn];
int m = 0;
for(int i = 0;i<cell_hn;i++){
for(int j = 0;j<cell_wn;j++){
int *feature = CellFeature(img,j*cell_w,i);
for(int k = 0;k<cell_fn;k++){
hog_f[m++] = feature[k];
}
}
}
for(int i = 0;i<cell_wn*cell_hn*cell_fn;i++){
if(i%9==0){
printf("\n");
}
if(i%(2*2*9)==0){
printf("-------------------------------------\n\n");
}
printf("%d ",hog_f[i]);
}
return hog_f;
}
int main()
{
IplImage *img=cvLoadImage("Img2.jpg");
if(img==NULL){
printf("img is null");
return 0;
}
//cvNamedWindow("greyimage");
IplImage* greyimg = GRBtoGrey(img);
//cvShowImage("greyimage",greyimg);
//cvWaitKey();
//cvNamedWindow("gammaimage");
IplImage* gammaimg = greygamma(greyimg);
//cvShowImage("gammaimage",gammaimg);
printf("gamma end\n");
//cvWaitKey();
//cvNamedWindow("gradientimage");
IplImage* gradientimage = gradientimg(gammaimg);
//cvShowImage("gradientimage",gradientimage);
printf("gradient end\n");
//cvWaitKey();
int *hog_f = HogFeature(gradientimage);
return 1;
}
#include<highgui.h>
#include<math.h>
#include<opencv.hpp>
using namespace cv;
using namespace std;
IplImage * GRBtoGrey(IplImage* img)//RGB图像转为灰度图像
{
IplImage* dest=cvCreateImage(cvSize(img->width, img->height),img->depth,1);
uchar* data = (uchar*)img->imageData;
int b,g,r;
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
b = data[i*img->widthStep+j*img->nChannels+0];
g = data[i*img->widthStep+j*img->nChannels+1];
r = data[i*img->widthStep+j*img->nChannels+2];
((uchar*)(dest->imageData+i*dest->widthStep))[j]=0.3*r+0.59*g+0.11*b;
}
}
return dest;
}
IplImage* nomolize(IplImage *img)//必须是灰度图
{
uchar* gM = (uchar*)img->imageData;
int max = 0,min = 255;
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
int num=gM[i*img->widthStep+j*img->nChannels];
if(num>max){
max = num;
}
if(num<min){
min = num;
}
}
}
for(int i = 0;i<img->height;i++){
for(int j = 0;j<img->width;j++){
int num = (int)(float)(gM[i*img->widthStep+j*img->nChannels]-min)*
((float)(255)/(float)(max-min));
if(num>255){
num = 255;
}
((uchar*)(img->imageData+i*img->widthStep))[j] = num;
}
}
return img;
}
IplImage *greygamma(IplImage *greyimage){
IplImage *gammaimage = cvCreateImage(cvSize(greyimage->width,greyimage->height),
greyimage->depth,greyimage->nChannels);
uchar* data = (uchar*)greyimage->imageData;
for(int i = 0;i<greyimage->height;i++){
for(int j = 0;j<greyimage->width;j++){
int gamma=data[i*greyimage->widthStep+j*greyimage->nChannels];
((uchar*)(gammaimage->imageData+i*gammaimage->widthStep))[j] = (int)sqrt((float)gamma);
}
}
return nomolize(gammaimage);
}
IplImage *gradientimg(IplImage *img){
uchar * data = (uchar*)img->imageData;
for(int i = 1;i<img->height-1;i++){
for(int j = 1;j<img->width-1;j++){
int Gx = data[i*img->widthStep+(j+1)*img->nChannels]-data[i*img->widthStep+(j-1)*img->nChannels];
int Gy = data[(i+1)*img->widthStep+j*img->nChannels]-data[(i-1)*img->widthStep+j*img->nChannels];
int gradient = (int)sqrt((float)(Gx*Gx+Gy*Gy));
int theta = (int)(atan((float)Gy/(float)Gx)*180/3.14);
((uchar*)(img->imageData+i*img->widthStep))[j] = theta;
}
}
return nomolize(img);
}
int *CellFeature(IplImage *img ,int w,int h){//8*8 cell
int cell_w = 8;
int f_n = 9;
int *feature = new int[f_n];
for(int i = 0;i<f_n;i++){
f_n = 0;
}
for(int i = h;i<h+cell_w;i++){
for(int j = w;j<w+cell_w;j++){
feature[((uchar*)(img->imageData+i*img->widthStep))[j]/40]++;
}
}
return feature;
}
int *HogFeature(IplImage *img){
int cell_w = 8;
int cell_fn = 9;
int cell_wn = img->width/cell_w;
int cell_hn = img->height/cell_w;
int *hog_f = new int[cell_wn*cell_hn*cell_fn];
int m = 0;
for(int i = 0;i<cell_hn;i++){
for(int j = 0;j<cell_wn;j++){
int *feature = CellFeature(img,j*cell_w,i);
for(int k = 0;k<cell_fn;k++){
hog_f[m++] = feature[k];
}
}
}
for(int i = 0;i<cell_wn*cell_hn*cell_fn;i++){
if(i%9==0){
printf("\n");
}
if(i%(2*2*9)==0){
printf("-------------------------------------\n\n");
}
printf("%d ",hog_f[i]);
}
return hog_f;
}
int main()
{
IplImage *img=cvLoadImage("Img2.jpg");
if(img==NULL){
printf("img is null");
return 0;
}
//cvNamedWindow("greyimage");
IplImage* greyimg = GRBtoGrey(img);
//cvShowImage("greyimage",greyimg);
//cvWaitKey();
//cvNamedWindow("gammaimage");
IplImage* gammaimg = greygamma(greyimg);
//cvShowImage("gammaimage",gammaimg);
printf("gamma end\n");
//cvWaitKey();
//cvNamedWindow("gradientimage");
IplImage* gradientimage = gradientimg(gammaimg);
//cvShowImage("gradientimage",gradientimage);
printf("gradient end\n");
//cvWaitKey();
int *hog_f = HogFeature(gradientimage);
return 1;
}
相关文章推荐
- HOG特征提取代码(opencv处理基本数据,c语言实现基本算法)
- 图像处理中,SIFT,FAST,MSER,STAR等特征提取算法的比较与分析(利用openCV实现)
- 图像处理中,SIFT,FAST,MSER,STAR等特征提取算法的比较与分析(利用openCV实现)
- 【图像特征提取12】OpenCv的SIFT图像局部特征提取描述算法C++代码的实现
- HOG特征提取C++ OpenCV代码
- 【机器学习PAI实践十二】机器学习实现男女声音识别分类(含语音特征提取数据和代码)
- 要点初见:OpenCV3中ORB特征提取算法的实现与分析
- 基于vlfeat的HOG特征提取c++代码实现
- Zedboardwebcam设计问题篇(五)opencv处理帧数据,函数代码实现
- OpenCV中HOG特征的提取实现
- 图像特征检测描述(一):SIFT、SURF、ORB、HOG、LBP特征的原理概述及OpenCV代码实现
- 基于C++和OpenCv的SIFT_图像局部特征检测算法代码的实现
- OpenCV特征提取与图像检索实现(附代码)
- HOG特征提取算法的实现过程
- ORB特征提取匹配opencv3代码实现
- 数据结构 - 线性表(顺序表)C语言代码实现-处理整型数据(附详细解释)。 _清风明月
- 要点初见:OpenCV3中CUDA ORB特征提取算法的实现(GPU加速的ORB算法)
- 数据挖掘之文本特征提取【理论+部分java代码实现】
- 特征提取方法(一):HOG原理及OpenCV实现
- 【算法】哈希摘要算法,CRC冗余算法,MD摘要算法,纯JAVA基本数据类型代码实现,面向对象