forked from zl2002006/PasteLCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adjust.cs
152 lines (144 loc) · 5.22 KB
/
Adjust.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using HalconDotNet;
namespace PasteLCM
{
/// <summary>
/// 灰度值调整小助手
/// </summary>
public partial class Adjust : Form
{
public HObject Ho_img { get; set; }
//窗体间传值
private FrmMain Fm;
[DllImport("kernel32", EntryPoint = "RtlMoveMemory", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
private static extern void CopyMemory(int Destination, int Source, int length);
public Adjust(HObject ho_img,FrmMain fm)
{
InitializeComponent();
this.Ho_img = ho_img;
Fm = fm;
}
private void @try_Load(object sender, EventArgs e)
{
checkBox1.Checked = false;
trackBar1.Enabled = false;
trackBar2.Enabled = false;
pictureBox1.Image = H2Bitmap(HobjectToHimage(this.Ho_img));
}
HObject hregion;
void Threshold(int a,int b)
{
//阈值化,输出region.
HOperatorSet.Threshold(this.Ho_img, out hregion, a, b);
//region转换为image.
HOperatorSet.RegionToBin(hregion, out hregion, 255, 0, 512, 512);
}
#region Himage转Bitmap
public Bitmap H2Bitmap(HImage img1)
{
IntPtr pt;
int mwidth, mheight;
string mtype = "";
Bitmap img;
ColorPalette pal;
int i;
const int Alpha = 255;
BitmapData bitmapData;
Rectangle rect;
int[] ptr = new int[2];
int PixelSize;
//this.Ho_image.ReadImage("clip");
pt = img1.GetImagePointer1(out mtype, out mwidth, out mheight);
img = new Bitmap(mwidth, mheight, PixelFormat.Format8bppIndexed);
pal = img.Palette;
for (i = 0; i <= 255; i++)
{
pal.Entries[i] = Color.FromArgb(Alpha, i, i, i);
}
img.Palette = pal;
rect = new Rectangle(0, 0, mwidth, mheight);
bitmapData = img.LockBits(rect, ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
PixelSize = Bitmap.GetPixelFormatSize(bitmapData.PixelFormat) / 8;
ptr[0] = bitmapData.Scan0.ToInt32();
ptr[1] = pt.ToInt32();
if (mwidth % 4 == 0)
CopyMemory(ptr[0], ptr[1], mwidth * mheight * PixelSize);
else
{
for (i = 0; i < mheight; i++)
{
ptr[1] += mwidth;
CopyMemory(ptr[0], ptr[1], mwidth * PixelSize);
ptr[0] += bitmapData.Stride;
}
}
img.UnlockBits(bitmapData);
img1.Dispose();
return img;
}
#endregion
/// <summary>
/// hobject转himage
/// </summary>
/// <param name="hobject">hobject</param>
/// <returns>himage</returns>
private HImage HobjectToHimage(HObject hobject)
{
HImage image = new HImage();
HTuple pointer, type, width, height;
HOperatorSet.GetImagePointer1(hobject, out pointer, out type, out width, out height);
image.GenImage1(type, width, height, pointer);
return image;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
trackBar1.Enabled = true;
trackBar2.Enabled = true;
Threshold(trackBar1.Value,trackBar2.Value);
Bitmap image = H2Bitmap(HobjectToHimage(hregion));
pictureBox1.Image = image;
}
else
{
trackBar1.Enabled = false;
trackBar2.Enabled = false;
}
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
trackBar1.Maximum = trackBar2.Value;
label3.Text = trackBar1.Value.ToString();
Threshold(trackBar1.Value, trackBar2.Value);
Bitmap image = H2Bitmap(HobjectToHimage(hregion));
pictureBox1.Image = image;
}
private void trackBar2_Scroll(object sender, EventArgs e)
{
trackBar2.Minimum = trackBar1.Value;
label4.Text = trackBar2.Value.ToString();
Threshold(trackBar1.Value, trackBar2.Value);
Bitmap image = H2Bitmap(HobjectToHimage(hregion));
pictureBox1.Image = image;
}
private void Adjust_FormClosed(object sender, FormClosedEventArgs e)
{
Fm.textBox4.Text = this.trackBar1.Value.ToString();
Fm.textBox3.Text = this.trackBar2.Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}