167 lines
6.5 KiB
C#
167 lines
6.5 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using Hermes;
|
|
using CogCommon;
|
|
using CogPattern;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Data;
|
|
using System.Windows.Documents;
|
|
using System.Windows.Input;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using System.Windows.Navigation;
|
|
using System.Windows.Shapes;
|
|
using VisionCommon;
|
|
using VisionCommonUI;
|
|
using VisionCommonUI.Resources;
|
|
using Cognex.VisionPro;
|
|
|
|
namespace CogUI {
|
|
/// <summary>
|
|
/// Interaction logic for SubMaskView.xaml
|
|
/// </summary>
|
|
public partial class SubMaskView : UserControl, IDisposable {
|
|
public Window ParentWindow { get; set; }
|
|
public IEnumerable<string> MaskLayoutItems { get; }
|
|
public ObservableCollection<string> TargetRangeItems { get; set; } = new ObservableCollection<string>();
|
|
|
|
private Logger _logger = new Logger("SubMaskView");
|
|
private PatternAgentBase _patternAgent;
|
|
private PatternInfo _patternInfo;
|
|
|
|
public SubMaskView(PatternAgentBase patternAgent, PatternInfo patternInfo) {
|
|
InitializeComponent();
|
|
|
|
_patternAgent = patternAgent;
|
|
_patternInfo = patternInfo;
|
|
MaskLayoutItems = MaskRegionUtil.GetMaskLayoutForSubPatternWithMask();
|
|
DataContext = this;
|
|
}
|
|
|
|
private void Apply_Button_Click(object sender, RoutedEventArgs e) {
|
|
var result = MessageBoxUtil.ShowMessageBoxQuestion(GetQuestionMessage());
|
|
|
|
if (result == MessageBoxResult.Yes) {
|
|
string maskLayout = GetMaskLayout();
|
|
try {
|
|
if (IsClearOthersEnabled()) {
|
|
RemoveAllSubPatterns();
|
|
}
|
|
|
|
var mainPatternMask = _patternAgent.GetPattern(_patternInfo.GetPatternIndex()) as IPatternMask;
|
|
var maskImage = mainPatternMask.GetMaskImage();
|
|
var trainParam = _patternAgent.GetTrainParam(_patternInfo) as ImageModelFinderTrainParam;
|
|
|
|
int rowCount = MaskRegionUtil.GetMaskLayoutRowCount(maskLayout);
|
|
int colCount = MaskRegionUtil.GetMaskLayoutColCount(maskLayout);
|
|
int startPatternSubId = GetStartPatternSubId();
|
|
int maskRegionIndex1d = 0;
|
|
|
|
foreach (var i in Enumerable.Range(startPatternSubId, rowCount * colCount)) {
|
|
var subPatternIndex = _patternInfo.GetPatternIndex().Clone() as PatternIndex;
|
|
subPatternIndex.PatternSubId = i;
|
|
|
|
if (E1.OK == _patternAgent.CopyPattern(_patternInfo.GetPatternIndex(), subPatternIndex)) {
|
|
var subPattern = _patternAgent.GetPattern(subPatternIndex) as IPatternMask;
|
|
var subMaskImage = maskImage.CopyBase(CogImageCopyModeConstants.CopyPixels) as CogImage8Grey;
|
|
CogEnvironment.ClearImage(subMaskImage, 255);
|
|
MaskRegionUtil.SetMaskToMaskImage(
|
|
subMaskImage, 0,
|
|
rowCount, colCount,
|
|
maskRegionIndex1d);
|
|
subPattern.SetMaskImage(subMaskImage);
|
|
subPattern.SetTrainOrigin(trainParam.TrainOrigin);
|
|
|
|
var subPatternInfo = _patternAgent.GetPatternInfo(subPatternIndex);
|
|
_patternAgent.Save(subPatternInfo);
|
|
}
|
|
maskRegionIndex1d++;
|
|
}
|
|
|
|
MessageBoxUtil.ShowMessageBoxOk(StrTokens.MSG_O_SaveCompleted);
|
|
}
|
|
catch (Exception ex) {
|
|
_logger.E(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
|
|
private string GetQuestionMessage() {
|
|
if (IsClearOthersEnabled()) {
|
|
return StrTokens.MSG_W_AllSubPatternRemoved + "\n" + StrTokens.MSG_Q_ApplyMaskToSubPatterns;
|
|
}
|
|
return StrTokens.MSG_Q_ApplyMaskToSubPatterns;
|
|
}
|
|
|
|
private bool IsClearOthersEnabled() {
|
|
if (checkBoxClearOthers.IsChecked == null) {
|
|
return false;
|
|
}
|
|
|
|
return (bool)checkBoxClearOthers.IsChecked;
|
|
}
|
|
|
|
private void RemoveAllSubPatterns() {
|
|
foreach (var patternSubId in Enumerable.Range(
|
|
PatternConstants.MAIN_PATTERN_INDEX + 1, PatternConstants.SUB_PATTERN_COUNT)) {
|
|
var subPatternIndex = _patternInfo.GetPatternIndex().Clone() as PatternIndex;
|
|
subPatternIndex.PatternSubId = patternSubId;
|
|
if (_patternAgent.IsPatternExist(subPatternIndex)) {
|
|
_patternAgent.Remove(subPatternIndex);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Cancel_Button_Click(object sender, RoutedEventArgs e) {
|
|
ParentWindow?.Close();
|
|
}
|
|
|
|
public void Dispose() {
|
|
}
|
|
|
|
private void ComboBox_MaskLayout_SelectionChanged(object sender, SelectionChangedEventArgs e) {
|
|
UpdateTargetIdRangeItems();
|
|
}
|
|
|
|
private void UpdateTargetIdRangeItems() {
|
|
var maskLayout = GetMaskLayout();
|
|
var regionCount = MaskRegionUtil.GetRegionCount(maskLayout);
|
|
|
|
TargetRangeItems.Clear();
|
|
foreach (var startPatternSubId in Enumerable.Range(1, PatternConstants.SUB_PATTERN_COUNT)) {
|
|
if ((startPatternSubId + regionCount - 1) < PatternConstants.SUB_PATTERN_COUNT) {
|
|
TargetRangeItems.Add(GetTargetIdRange(startPatternSubId, regionCount));
|
|
}
|
|
}
|
|
|
|
if (0 < TargetRangeItems.Count) {
|
|
comboBoxTargetRange.SelectedIndex = 0;
|
|
}
|
|
}
|
|
|
|
private string GetMaskLayout() {
|
|
return comboBoxMaskLayout.SelectedItem.ToString();
|
|
}
|
|
|
|
private string GetTargetIdRange(int startPatternSubId, int regionCount) {
|
|
// ex) "1 ~ 9"
|
|
return $"{startPatternSubId} ~ {startPatternSubId + regionCount - 1}";
|
|
}
|
|
|
|
private int GetStartPatternSubId() {
|
|
var targetIdRange = comboBoxTargetRange.SelectedItem.ToString();
|
|
return Convert.ToInt32(targetIdRange.Substring(0, 1));
|
|
}
|
|
}
|
|
}
|