163 lines
6.3 KiB
C#
163 lines
6.3 KiB
C#
/*
|
|
* 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 {
|
|
/// <summary>
|
|
/// 마스크 영역 분할을 위한 Uitl 클래스
|
|
/// </summary>
|
|
public static class MaskRegionUtil {
|
|
/// <summary>
|
|
/// 지원되는 마스크 레이아웃 리스트를 얻는다.
|
|
/// </summary>
|
|
/// <returns>마스크 레이아웃 리스트</returns>
|
|
public static List<string> GetMaskLayout() {
|
|
return new List<string>() {
|
|
"1 x 2", "1 x 3",
|
|
"2 x 1", "2 x 2", "2 x 3",
|
|
"3 x 1", "3 x 2", "3 x 3",
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 레이아웃의 영역의 총 개수를 구한다.
|
|
/// </summary>
|
|
/// <param name="maskLayout"></param>
|
|
/// <returns>영역의 총 개수</returns>
|
|
public static int GetRegionCount(string maskLayout) {
|
|
return GetMaskLayoutRowCount(maskLayout) * GetMaskLayoutColCount(maskLayout);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 레이아웃의 행개수를 구한다.
|
|
/// </summary>
|
|
/// <param name="maskLayout">마스크 레이아웃</param>
|
|
/// <returns>행 개수</returns>
|
|
public static int GetMaskLayoutRowCount(string maskLayout) {
|
|
return Convert.ToInt32(maskLayout.Substring(0, 1));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 레이아웃의 열개수를 구한다.
|
|
/// </summary>
|
|
/// <param name="maskLayout">마스크 레이아웃</param>
|
|
/// <returns>열 개수</returns>
|
|
public static int GetMaskLayoutColCount(string maskLayout) {
|
|
return Convert.ToInt32(maskLayout.Substring(4, 1));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 영역별 서브 패턴 자동 생성을 위한
|
|
/// 레이아웃 리스트를 얻는다.
|
|
/// </summary>
|
|
/// <returns>마스크 레이아웃 리스트</returns>
|
|
public static List<string> GetMaskLayoutForSubPatternWithMask() {
|
|
var maskLayout = GetMaskLayout();
|
|
return maskLayout
|
|
.Where(layout => GetRegionCount(layout) < PatternConstants.SUB_PATTERN_COUNT)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 오버레이 이미지에 마스크 영역을 설정한다.
|
|
/// </summary>
|
|
/// <param name="maskImage">마스크 이미지</param>
|
|
/// <param name="maskValue">마스크 값</param>
|
|
/// <param name="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="index1d">마스크 영역 1차원 인덱스</param>
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 영역에 해댱되는 Rectangle을 구한다.
|
|
/// </summary>
|
|
/// <param name="imageWidth">이미지 너비</param>
|
|
/// <param name="imageHeight">이미지 높이</param>
|
|
/// <param name="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="index1d">마스크 영역 1차원 인덱스</param>
|
|
/// <returns>마스크 영역</returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 영역에 해댱되는 Rectangle을 구한다.
|
|
/// </summary>
|
|
/// <param name="imageWidth">이미지 너비</param>
|
|
/// <param name="imageHeight">이미지 높이</param>
|
|
/// <param name="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="rowIndex">마스크 영역 행 인덱스</param>
|
|
/// <param name="colIndex">마스크 영역 열 인덱스</param>
|
|
/// <returns>마스크 영역</returns>
|
|
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;
|
|
}
|
|
}
|
|
}
|