opencv学习笔记-入门(26)hog中gridents和angle函数

Simple 1-D[ 1;0;1] masks at & =0 work best.

For colour images, we calculate separate gradients for each colour channel,and take the one with the largest norm as the pixel's gradient vector.

以最大的那个梯度计算angle:

void calGradient(Mat& gamimg,Mat& grad_ang)
{
	
	assert(!gamimg.empty());
	grad_ang.release();
	grad_ang.create(gamimg.size(),CV_32FC2);   //CV_32FC2 两维的,一维用来装梯度, 一维用来装angle
	//Mat t(gamimg.size(),CV_8UC1);
	//t.setTo(0);
	
	grad_ang.setTo(0);  //将阵列中所有的或部分的元素设置为指定的值。此处为0
	int w = gamimg.cols;  
	int h = gamimg.rows;
	int nc = gamimg.channels();
	
	for (int i = 1; i < h - 1; i++)
	{
		float *qd = grad_ang.ptr<float>(i);   //迭代式的数据操作,很重要,数据操作  //mat很值得学习的一块
		uchar *pd_pre = gamimg.ptr(i-1);
		uchar *pd    = gamimg.ptr(i);
		uchar *pd_nex = gamimg.ptr(i+1);
		//uchar *data = t.ptr(i);
		for (int j = 1; j < w - 1; j++)
		{
			float temp, gx, gy, angle;
			for (int k = 0; k < nc; k++)  //维数
			{
				gx = (float)(pd[nc*(j+1)+k] - pd[nc*(j-1)+k]);   //记住这种操作太简单了,方便!!!顶啊
				gy = (float)(pd_nex[nc*j+k] - pd_pre[nc*j+k]);

				temp = sqrt(gx*gx+gy*gy);   //梯度的大小
				angle = atan(gy/(gx+MINIMUM)) + PI/2;
				if (qd[j*2] < temp)    //每一维的寻找那个最大的维度!!
				{
					qd[j*2] = temp;
					qd[j*2+1] = angle;    //最大的装angle
					//data[j] = static_cast<uchar>(temp);
				}
			}

		}
	}





 

你可能感兴趣的:(float)