猜测一:只对第一个通道进行阈值分割,可能有多个区域。
猜测二,对三个通道分别进行阈值分割,有多个区域。
个人感觉猜测一比较合理,否则用ReduceDomain取那个通道呢?
开发环境:win7 VS2022 halcon12
CreateImage 指定宽度和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毫秒。