CNN-图像卷积c++实现-Day 2

 

class img_u8_t
{
public:
	img_u8_t(s32 w, s32 h, s32 s);
	~img_u8_t();

	void initBuf();
	void unBuf();
	void getParam(s32& w, s32& h, s32 s);
	void setData(u8* d, s32 bufSize){ memcpy(data, d, bufSize * sizeof(u8)); }
	u8* data;
	s32 width;
	s32 height;
	s32 step;
};

class img_f32_t
{
public:
	img_f32_t(s32 w, s32 h, s32 s);
	~img_f32_t();

	void initBuf();
	void unBuf();
	void getParam(s32& w, s32& h, s32 s);
	f32* data;
	s32 width;
	s32 height;
	s32 step;
};

img_u8_t::img_u8_t(s32 w, s32 h, s32 s) : width(w), height(h), step(s)
{
	initBuf();
}

img_u8_t::~img_u8_t()
{
	free(data);
	data = NULL;
}

void img_u8_t::initBuf()
{
	data = (u8*)malloc(width * height * sizeof(u8));
}

void img_u8_t::getParam(s32& w, s32& h, s32 s)
{
	w = width;
	h = height;
	s = step;
}

img_f32_t::img_f32_t(s32 w, s32 h, s32 s) : width(w), height(h), step(s)
{
	initBuf();
}

img_f32_t::~img_f32_t()
{
	free(data);
	data = NULL;
}

void img_f32_t::initBuf()
{
	data = (f32*)malloc(width * height * sizeof(f32));
}

void conv(img_u8_t* src, img_f32_t* dst, img_f32_t * filter)
{
	s32 srcW    = src->width;
	s32 srcH    = src->height;
	s32 srcStep = src->step;

	s32 fw = filter->width;
	s32 fh = filter->height;
	s32 rw = (filter->width - 1) / 2;
	s32 rh = (filter->height - 1) / 2;

	if (fw % 2 == 0 || fh % 2 == 0)
	{
		cout << "the kernel size should be odd" << endl;
		return;
	}

	u8* pSrcRow = src->data;
	s32 idx = 0;

	for (s32 i = rh; i < srcH - rh; i++)
	{   
		u8* pSrcCol = pSrcRow;
		for (s32 j = rw; j < srcW - rw; j++)
		{
			f32 sum = 0.f;
			f32* filterRow = filter->data;
			u8*  srcBoxRow = pSrcCol;

			for (s32 h = 0; h < filter->height; h++)
			{
				for (s32 w = 0; w < filter->width; w++)
				{
					sum += filterRow[w] * srcBoxRow[w];
				}
				srcBoxRow += src->step;
				filterRow += filter->step;
			}

			sum = sum < 0 ? 0 : sum;
			dst->data[idx] = sum;
			pSrcCol++;
			idx++;
		}
		pSrcRow += srcStep;
	}
}

测试函数:

void main()
{
	VideoCapture cap;
	cap.open(0);
	if (!cap.isOpened())
	{
		return;
	}

	Mat srcImg;
	while (1)
	{
		cap >> srcImg;
		namedWindow("src", 0);
		imshow("src", srcImg);

		Mat srcGray;
		cvtColor(srcImg, srcGray, CV_BGR2GRAY);
		img_f32_t  filter(3, 3, 3);
		img_u8_t  convShow(filter.width, filter.height, filter.step);

		convInit(&filter);
		filter.data[0] = -1;
		filter.data[1] = 0;
		filter.data[2] = 1;

		filter.data[3] = -2;
		filter.data[4] = 0;
		filter.data[5] = 2;

		filter.data[6] = -1;
		filter.data[7] = 0;
		filter.data[8] = 1;

		imgf2u(&filter, &convShow);
		string winName = "conv_param";
		showGrayImg(&convShow, winName);

		blur(srcGray, srcGray, Size(5, 5));
		medianBlur(srcGray, srcGray, 5);

		Mat sobelImg = srcGray.clone();
		Sobel(srcGray, sobelImg, srcGray.depth(), 1, 0);
		namedWindow("sobel_x", 0);
		imshow("sobel_x", sobelImg);
		waitKey(10);
		img_u8_t src(srcGray.cols, srcGray.rows, srcGray.cols);
		src.setData(srcGray.data, srcGray.cols * srcGray.rows);

		img_f32_t  convLay1(src.width - filter.width + 1, src.height - filter.height + 1, 
			                src.width - filter.width + 1);

		conv(&src, &convLay1, &filter);
		img_u8_t convLay1Show(convLay1.width, convLay1.height, convLay1.step);

		imgf2u(&convLay1, &convLay1Show);
		string winName1 = "convLay1";
		showGrayImg(&convLay1Show, winName1);
	}

	cout << "hello world" << endl;
	system("pause");
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值