/* * Copyright (c) 2025 LG Electronics Inc. * SPDX-License-Identifier: LicenseRef-LGE-Proprietary */ using CogCommon; using Cognex.VisionPro; using Cognex.VisionPro.ImageProcessing; using System; using System.Collections.Generic; using System.Linq; using VisionCommon; namespace CogPattern { /// /// 마스크 영역 분할을 위한 Uitl 클래스 /// public static class MaskRegionUtil { /// /// 지원되는 마스크 레이아웃 리스트를 얻는다. /// /// 마스크 레이아웃 리스트 public static List GetMaskLayout() { return new List() { "1 x 2", "1 x 3", "2 x 1", "2 x 2", "2 x 3", "3 x 1", "3 x 2", "3 x 3", }; } /// /// 마스크 레이아웃의 영역의 총 개수를 구한다. /// /// /// 영역의 총 개수 public static int GetRegionCount(string maskLayout) { return GetMaskLayoutRowCount(maskLayout) * GetMaskLayoutColCount(maskLayout); } /// /// 마스크 레이아웃의 행개수를 구한다. /// /// 마스크 레이아웃 /// 행 개수 public static int GetMaskLayoutRowCount(string maskLayout) { return Convert.ToInt32(maskLayout.Substring(0, 1)); } /// /// 마스크 레이아웃의 열개수를 구한다. /// /// 마스크 레이아웃 /// 열 개수 public static int GetMaskLayoutColCount(string maskLayout) { return Convert.ToInt32(maskLayout.Substring(4, 1)); } /// /// 마스크 영역별 서브 패턴 자동 생성을 위한 /// 레이아웃 리스트를 얻는다. /// /// 마스크 레이아웃 리스트 public static List GetMaskLayoutForSubPatternWithMask() { var maskLayout = GetMaskLayout(); return maskLayout .Where(layout => GetRegionCount(layout) < PatternConstants.SUB_PATTERN_COUNT) .ToList(); } /// /// 오버레이 이미지에 마스크 영역을 설정한다. /// /// 마스크 이미지 /// 마스크 값 /// 행 개수 /// 열 개수 /// 마스크 영역 1차원 인덱스 public static void SetMaskToMaskImage( ICogImage maskImage, int maskValue, int rowCount, int colCount, int index1d) { var imageSize = CogEnvironment.GetImageSize(maskImage); var rectangle = GetMaskRectangle( imageSize.Width, imageSize.Height, rowCount, colCount, index1d); CogCopyRegionTool copyRegionTool = new CogCopyRegionTool { InputImage = maskImage, DestinationImage = maskImage, Region = rectangle }; copyRegionTool.RunParams.FillRegion = true; copyRegionTool.RunParams.FillRegionValue = maskValue; copyRegionTool.RunParams.FillBoundingBox = false; copyRegionTool.RunParams.ImageAlignmentEnabled = true; copyRegionTool.Run(); } /// /// 마스크 영역에 해댱되는 Rectangle을 구한다. /// /// 이미지 너비 /// 이미지 높이 /// 행 개수 /// 열 개수 /// 마스크 영역 1차원 인덱스 /// 마스크 영역 public static CogRectangle GetMaskRectangle( int imageWidth, int imageHeight, int rowCount, int colCount, int index1d) { int colIndex = GetColIndexFromIndex1d(index1d, colCount); int rowIndex = GetRowIndexFromIndex1d(index1d, colIndex, colCount); return GetMaskRectangle( imageWidth, imageHeight, rowCount, colCount, rowIndex, colIndex); } /// /// 마스크 영역에 해댱되는 Rectangle을 구한다. /// /// 이미지 너비 /// 이미지 높이 /// 행 개수 /// 열 개수 /// 마스크 영역 행 인덱스 /// 마스크 영역 열 인덱스 /// 마스크 영역 public static CogRectangle GetMaskRectangle( int imageWidth, int imageHeight, int rowCount, int colCount, int rowIndex, int colIndex) { int roiWidth = imageWidth / colCount; int roiHeight = imageHeight / rowCount; int top = roiHeight * rowIndex; int left = roiWidth * colIndex; // 정수 단위로 분할되고 남은 픽셀을 마지막 영역에 포함시킨다. if (colIndex == (colCount - 1)) { roiWidth = imageWidth - left; } if (rowIndex == (rowCount - 1)) { roiHeight = imageHeight - top; } // Rectangle은 Right, Bottom 위치를 포함시킨다. var cogRectangle = new CogRectangle(); cogRectangle.SetXYWidthHeight(left, top, roiWidth, roiHeight); return cogRectangle; } private static int GetRowIndexFromIndex1d(int index1d, int colIndex, int colCount) { return (index1d - colIndex) / colCount; } private static int GetColIndexFromIndex1d(int index1d, int colCount) { return index1d % colCount; } } }