using Matrox.MatroxImagingLibrary;
using MilCommon;
using System;
using System.Collections.Generic;
using System.Linq;
using VisionCommon;
namespace MilPattern {
///
/// 마스크 영역 분할을 위한 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 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);
}
///
/// 마스크 이미지에 마스크 영역을 설정한다.
///
/// 마스크 이미지
/// 행 개수
/// 열 개수
/// 마스크 영역 행 인덱스
/// 마스크 영역 열 인덱스
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);
}
///
/// 오버레이 이미지에 마스크 영역을 설정한다.
///
/// MIL 그래픽 컨텍스트
/// 오버레이 이미지
/// 마스크 컬러
/// 행 개수
/// 열 개수
/// 마스크 영역 1차원 인덱스
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);
}
///
/// 마스크 영역에 해댱되는 Rectangle을 구한다.
///
/// 이미지 너비
/// 이미지 높이
/// 행 개수
/// 열 개수
/// 마스크 영역 1차원 인덱스
/// 마스크 영역
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);
}
///
/// 마스크 영역에 해댱되는 Rectangle을 구한다.
///
/// 이미지 너비
/// 이미지 높이
/// 행 개수
/// 열 개수
/// 마스크 영역 행 인덱스
/// 마스크 영역 열 인덱스
/// 마스크 영역
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;
}
}
}