对哪些通道进行阈值分隔
猜测一:只对第一个通道进行阈值分割,可能有多个区域。
猜测二,对三个通道分别进行阈值分割,有多个区域。
个人感觉猜测一比较合理。假定猜测二正确,对结果用ReduceDomain ,各通道的结果会受其它通道干扰。
开发及运行环境。操作系统win7 开发工具:VS2022 图像处理库:halcon12CreateImage函数用于 指定宽度和byte数组创建图像
HObject CreateImage(byte[] barr,int width) { IntPtr p = Marshal.AllocHGlobal(barr.Length); Marshal.Copy(barr, 0, p, barr.Length); HObject img = new HObject(); HOperatorSet.GenImage1(out img, "byte", width, barr.Length/ width,p); //有内存泄漏 return img; } int wh = 2; byte[] barr = new byte[wh * wh]; HObject imgR = CreateImage(barr, wh); barr[0] = 1; HObject imgG = CreateImage(barr, wh); barr[1] = 1; HObject imgB = CreateImage(barr, wh); HObject imgRGB; HOperatorSet.Compose3(imgR, imgG, imgB, out imgRGB); HObject region; HOperatorSet.Threshold(imgRGB, out region, 1, 1); HTuple tNum; HOperatorSet.CountObj(region, out tNum); HTuple tArea = WHCSHalCon.Base.Area(region);
结果:
区域数量1,面积0。
修改组成多通道的语句为如下:
HOperatorSet.Compose3(imgG, imgB, imgR, out imgRGB);
区域数量1,面积1。
修改组成多通道的语句为如下:
HOperatorSet.Compose3( imgB, imgR, imgG, out imgRGB);
区域数量1,面积2。
结论证明猜测一正确。
用时
新问题,直接二值化imgRGB用时会不会大幅增加呢?
int wh = 1024*10; byte[] barr = new byte[wh * wh]; HObject imgR = CreateImage(barr, wh); barr[0] = 1; HObject imgG = CreateImage(barr, wh); barr[1] = 1; HObject imgB = CreateImage(barr, wh); HObject imgRGB; HOperatorSet.Compose3( imgB, imgR, imgG, out imgRGB); double dUse1, dUse2, dUse3; { System.DateTime begin = System.DateTime.Now; HObject region; HOperatorSet.Threshold(imgRGB, out region, 1, 1); HTuple tNum; HOperatorSet.CountObj(region, out tNum); HTuple tArea = WHCSHalCon.Base.Area(region); dUse1 = (System.DateTime.Now - begin).TotalMilliseconds; } { System.DateTime begin = System.DateTime.Now; HObject region; HOperatorSet.Threshold(imgR, out region, 1, 1); HTuple tNum; HOperatorSet.CountObj(region, out tNum); HTuple tArea = WHCSHalCon.Base.Area(region); dUse2 = (System.DateTime.Now - begin).TotalMilliseconds; } { System.DateTime begin = System.DateTime.Now; HObject tmp1, tmp2, tmp3; HOperatorSet.Decompose3(imgRGB, out tmp1, out tmp2, out tmp3); HObject region; HOperatorSet.Threshold(tmp1, out region, 1, 1); HTuple tNum; HOperatorSet.CountObj(region, out tNum); HTuple tArea = WHCSHalCon.Base.Area(region); dUse3 = (System.DateTime.Now - begin).TotalMilliseconds; } }
对于100M的大img,dUse1到15毫秒,dUse2大约7.8毫秒,dUse3大约8.2毫秒。增加的用时不能忽略