SSE图像算法优化系列1-RGB转灰度图

前言

ImageShop博主是我的Idol。我会逐渐出一些SSE优化一些图像领域的算法的文章,并且将代码开源到Github,希望可以为我多多star。https://github.com/BBuf/Image-processing-algorithm-Speed

算法原理

RGB转灰度图没啥说的了,这里的重点在于使用SSE优化,但为了和普通以及多线程优化的程序做一个速度对比这里也提供了一下对应的程序实现,我在实现这个代码的时候主要参考了ImageShop博主的这篇文章,https://www.cnblogs.com/Imageshop/p/6261719.html ,他只提供了一些代码片段,我依靠自己的逻辑和理解完整实现了整个代码并做了速度测试,我认为指令集加速无论是在图像领域还是在CV领域都是非常重要的,所以我也愿意将代码分享出来,接下来我们就开始今天的主题吧。

朴素的RGB转灰度算法

//1920 * 1080 3.71ms
void RGB2Y_1(unsigned char *Src, unsigned char *Dest, int Width, int Height, int Stride) {
	const int B_WT = int(0.114 * 256 + 0.5);
	const int G_WT = int(0.587 * 256 + 0.5);
	const int R_WT = 256 - B_WT - G_WT;
	for (int Y = 0; Y < Height; Y++) {
		unsigned char *LinePS = Src + Y * Stride;
		unsigned char *LinePD = Dest + Y * Width;
		for (int X = 0; X < Width; X++, LinePS += 3) {
			LinePD[X] = (B_WT * LinePS[0] + G_WT * LinePS[1] + R_WT * LinePS[2]) >> 8;
		}
	}
}

这个我不用解释了,只说速度测试,在我的PC(core-i7-3770处理器的速度为3.95ms)。

四路循环展开算法

//1920 * 1080 2.0ms
void RGB2Y_2(unsigned char *Src, unsigned char *Dest, int Width, int Height, int Stride) {
	const int B_WT = int(0.114 * 256 + 0.5);
	const int G_WT = int(0.587 * 256 + 0.5);
	const int R_WT = 256 - B_WT - G_WT; // int(0.299 * 256 + 0.5)
	for (int Y = 0; Y < Height; Y++) {
		unsigned char *LinePS = Src + Y * Stride;
		unsigned char *LinePD = Dest + Y * Width;
		int X = 0;
		for (; X < Width - 4; X += 4, LinePS += 12) {
			LinePD[X + 0] = (B_WT * LinePS[0] + G_WT * LinePS[1] + R_WT * LinePS[2]) >> 8;
			LinePD[X + 1] = (B_WT * L
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值