197 lines
7.8 KiB
C#
197 lines
7.8 KiB
C#
using Matrox.MatroxImagingLibrary;
|
|
using MilCommon;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using VisionCommon;
|
|
|
|
namespace MilPattern {
|
|
/// <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="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="index1d">마스크 영역 1차원 인덱스</param>
|
|
public static void SetMaskToImage(
|
|
MIL_ID maskImage,
|
|
int rowCount, int colCount,
|
|
int index1d) {
|
|
int colIndex = GetColIndexFromIndex1d(index1d, colCount);
|
|
int rowIndex = GetRowIndexFromIndex1d(index1d, colIndex, colCount);
|
|
|
|
SetMaskToImage(
|
|
maskImage,
|
|
rowCount, colCount,
|
|
rowIndex, colIndex);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 마스크 이미지에 마스크 영역을 설정한다.
|
|
/// </summary>
|
|
/// <param name="maskImage">마스크 이미지</param>
|
|
/// <param name="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="rowIndex">마스크 영역 행 인덱스</param>
|
|
/// <param name="colIndex">마스크 영역 열 인덱스</param>
|
|
public static void SetMaskToImage(
|
|
MIL_ID maskImage,
|
|
int rowCount, int colCount,
|
|
int rowIndex, int colIndex) {
|
|
var imageSize = MilEnvironment.GetImageSize(maskImage);
|
|
var rectangle = GetMaskRectangle(
|
|
imageSize.Width, imageSize.Height,
|
|
rowCount, colCount, rowIndex, colIndex);
|
|
|
|
MIL.MgraRectFill(
|
|
MIL.M_DEFAULT, maskImage,
|
|
rectangle.Left,
|
|
rectangle.Top,
|
|
rectangle.Right,
|
|
rectangle.Bottom);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 오버레이 이미지에 마스크 영역을 설정한다.
|
|
/// </summary>
|
|
/// <param name="graphicContext">MIL 그래픽 컨텍스트</param>
|
|
/// <param name="overlayId">오버레이 이미지</param>
|
|
/// <param name="color">마스크 컬러</param>
|
|
/// <param name="rowCount">행 개수</param>
|
|
/// <param name="colCount">열 개수</param>
|
|
/// <param name="index1d">마스크 영역 1차원 인덱스</param>
|
|
public static void SetMaskToOverlay(
|
|
MIL_ID graphicContext, MIL_ID overlayId, MIL_ID color,
|
|
int rowCount, int colCount,
|
|
int index1d) {
|
|
var imageSize = MilEnvironment.GetImageSize(overlayId);
|
|
var rectangle = GetMaskRectangle(
|
|
imageSize.Width, imageSize.Height, rowCount, colCount, index1d);
|
|
MIL.MgraColor(graphicContext, color);
|
|
MIL.MgraRectFill(
|
|
graphicContext, overlayId,
|
|
rectangle.Left,
|
|
rectangle.Top,
|
|
rectangle.Right,
|
|
rectangle.Bottom);
|
|
}
|
|
|
|
/// <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 System.Drawing.Rectangle 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 System.Drawing.Rectangle 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 위치를 포함시킨다.
|
|
return new System.Drawing.Rectangle(left, top, roiWidth - 1, roiHeight - 1);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|