图像细化操作的并行优化

10 篇文章 0 订阅
6 篇文章 2 订阅

参考文章:

A Fast parallel algorithm for thinning digital patternshttps://dl.acm.org/doi/pdf/10.1145/357994.358023【opencv】图像细化https://blog.csdn.net/qianchenglenger/article/details/19332011Windows 下使用了自带的并行库ppl,跨平台的话使用 tbb 也是可以无痛切换的。参考了https://blog.csdn.net/qianchenglenger/article/details/19332011 的文章,进行了边界和并行优化。

#include <opencv2/core/core.hpp>
#include <iostream>
#include <vector>
#include <ppl.h>

/*
 * 参考论文:A Fast parallel algorithm for thinning digital patterns
 * 论文出处:https://dl.acm.org/doi/pdf/10.1145/357994.358023
 */
cv::Mat ThinImage(const cv::Mat& src)
{
	assert(src.type() == CV_8UC1);
	cv::Mat dst;
	const int width = src.cols - 1;
	const int height = src.rows - 1;
	src.copyTo(dst);

	while (true)
	{
		std::vector<std::vector<uchar*>> mFlags;
		auto work = [&](const int index)
		{
			mFlags.resize(height);
			Concurrency::parallel_for(1, height, [&](int i)
				{
					std::vector<uchar*> local_flag;
					uchar* p = dst.ptr<uchar>(i);
					for (int j = 1; j < width; ++j)
					{
						//  进行8邻域标记
						const uchar p1 = p[j];
						if (p1 != 1) continue;
						const uchar p4 = *(p + j + 1);
						const uchar p8 = *(p + j - 1);
						const uchar p2 = *(p - dst.step + j);
						const uchar p3 = *(p - dst.step + j + 1);
						const uchar p9 = *(p - dst.step + j - 1);
						const uchar p6 = *(p + dst.step + j);
						const uchar p5 = *(p + dst.step + j + 1);
						const uchar p7 = *(p + dst.step + j - 1);
						if ((p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) >= 2 && (p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9) <= 6)
						{
							int ap = 0;
							if (p2 == 0 && p3 == 1) ++ap;
							if (p3 == 0 && p4 == 1) ++ap;
							if (p4 == 0 && p5 == 1) ++ap;
							if (p5 == 0 && p6 == 1) ++ap;
							if (p6 == 0 && p7 == 1) ++ap;
							if (p7 == 0 && p8 == 1) ++ap;
							if (p8 == 0 && p9 == 1) ++ap;
							if (p9 == 0 && p2 == 1) ++ap;

							bool flag = false;
							switch (index)
							{
							case 0:
								flag = ap == 1 && p2 * p4 * p6 == 0 && p4 * p6 * p8 == 0;
								break;
							case 1:
								flag = ap == 1 && p2 * p4 * p8 == 0 && p2 * p6 * p8 == 0;
								break;

							default:
								break;
							}
							if (flag)
							{
								//暂存标记点
								local_flag.push_back(p + j);
							}
						}
					}
					mFlags[i] = local_flag;
				});

			//删除标记点
			for (auto it = mFlags.begin(); it != mFlags.end(); ++it)
			{
				auto& local = (*it);
				for (auto it_local = local.begin(); it_local != local.end(); ++it_local) {
					**it_local = 0;
				}
			}

			int finish = 0;
			for (auto it = mFlags.begin(); it != mFlags.end(); ++it)
			{
				finish += it->empty();
			}
			if (finish == height)
			{
				return true;
			}
			else {
				mFlags.clear();
				return false;
			}
		};

		if (work(0))
			return dst;
		if (work(1))
			return dst;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值