图像处理 Vivado HLS C 实现边缘检测和肤色检测

基于YCrCb颜色空间的CrCb范围筛选法

  • 资料显示,正常黄种人的Cr分量大约在133至173之间,Cb分量大约在77至127之间。
  • 可以根据自己的项目需求放大或缩小这两个分量的范围。
void hls::hls_skin_dection(RGB_IMAGE &src, RGB_IMAGE &dst, int rows, int cols, int y_lower, int y_upper, int cb_lower, int cb_upper, int cr_lower, int cr_upper)
{

	for (int row = 0; row < rows; row++)
	{
	//#pragma HLS loop_flatten off
		for (int col = 0; col < cols; col++)
		{
            #pragma HLS pipeline II = 1 off 
			RGB_PIXEL src_data;
			RGB_PIXEL pix;
			RGB_PIXEL dst_data;
			bool skin_region;

			if (row < rows && col < cols)
			{
				src >> src_data;
			}


			uchar B = src_data.val[0];
			uchar G = src_data.val[1];
			uchar R = src_data.val[2];


			uchar y = (76 * R + 150 * G + 29 * B) >> 8;
			uchar cb = ((128 * B - 43 * R - 85 * G) >> 8) + 128;
			uchar cr = ((128 * R - 107 * G - 21 * B) >> 8) + 128;


			if (y > y_lower && y < y_upper && cb > cb_lower && cb < cb_upper && cr > cr_lower && cr < cr_upper)
				skin_region = 1;
			else
				skin_region = 0;

			uchar temp0 = (skin_region == 1) ? (uchar)255 : 0;
			uchar temp1 = (skin_region == 1) ? (uchar)255 : 0;
			uchar temp2 = (skin_region == 1) ? (uchar)255 : 0;

			dst_data.val[0] = temp0;
			dst_data.val[1] = temp1;
			dst_data.val[2] = temp2;
			dst << dst_data;
		}
	}
}

图像边缘检测与重心提取

void hls::hls_edge(RGB_IMAGE &input, RGB_IMAGE &output, int rows, int cols)
{
	RGB_PIXEL lastinPix;
	double sum_x = 0, sum_y = 0, area = 0;

	for (int row = 0; row < rows; row++)
	{
		lastinPix.val[0] = 0;
		lastinPix.val[1] = 0;
		lastinPix.val[2] = 0;

		for (int col = 0; col < cols; col++)
		{
            #pragma HLS pipeline II = 1 off 
			RGB_PIXEL curinPix;
			RGB_PIXEL outPix;

			if (row < rows && col < cols)
			{
				input >> curinPix;
			}
			else
			{
			}

			if (curinPix.val[0] == 255 && curinPix.val[1] == 255 && curinPix.val[2] == 255)
			{
				sum_x = sum_x + row; 
				sum_y = sum_y + col; 
				area = area + 1;
			}
			if (curinPix.val[0] == 0 && curinPix.val[1] == 0 && curinPix.val[2] == 0 && lastinPix.val[0] == 255 && lastinPix.val[1] == 255 && lastinPix.val[2] == 255)
			{
				outPix.val[0] = 255;
				outPix.val[1] = 255;
				outPix.val[2] = 255;
			}
			else
			{
				if (curinPix.val[0] == 255 && curinPix.val[1] == 255 && curinPix.val[2] == 255 && lastinPix.val[0] == 0 && lastinPix.val[1] == 0 && lastinPix.val[2] == 0)
				{
					outPix.val[0] = 255;
					outPix.val[1] = 255;
					outPix.val[2] = 255;

				}
				else
				{
					outPix.val[0] = 0;
					outPix.val[1] = 0;
					outPix.val[2] = 0;
				}
			}

			lastinPix.val[0] = curinPix.val[0];
			lastinPix.val[1] = curinPix.val[1];
			lastinPix.val[2] = curinPix.val[2];

			output << outPix;
		}
	}
	center.y = round(sum_x / area);
	center.x = round(sum_y / area);
	cout << center.x << '\t' << center.y << endl;
}

你可能感兴趣的:(FPGA,计算机视觉,边缘检测,FPGA,Vivado,HLS)