-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageSelector.cs
170 lines (140 loc) · 4.63 KB
/
ImageSelector.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Jeeja_ImageLabeller
{
internal class ImageSelector
{
public int imageIndex { get; private set; }
public List<string> images;
private List<List<int[]>> rectangles;
private List<List<string>> classI;
private Image currentImage;
int imageWidth;
int imageHeight;
public Image curImage
{
get
{
SetResizeImage(Image.FromFile(images[imgIndex]));
return currentImage;
}
}
public int imgIndex
{
get { return imageIndex; }
set
{
if (value < images.Count() && value >= 0)
{
imageIndex = value;
}
else
{
imageIndex = 0;
}
}
}
public List<List<int[]>> Rectangles
{
get { return rectangles; }
}
public List<List<string>> ClassI
{
get { return classI; }
}
public static Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
public void AddClass(string str)
{
classI[imgIndex].Add(str);
}
public void AddRectangle(int x1, int y1, int x2, int y2)
{
rectangles[imgIndex].Add(new int[] { x1, y1, x2, y2 });
}
public void InitRectangles()
{
rectangles.Add(new List<int[]>());
}
public void InitClasses() {
classI.Add(new List<string>());
}
public int ImagesCount() => images.Count();
public Image GetNextImage() //FIXME: fix crash after trying to get image after last
{
imgIndex++;
SetResizeImage(Image.FromFile(images[imgIndex]));
return currentImage;
}
public Image GetPrevImage()
{
imgIndex--;
SetResizeImage(Image.FromFile(images[imgIndex]));
return currentImage;
}
private void SetResizeImage(Image img)
{
imageWidth = img.Width;
imageHeight = img.Height;
currentImage = ResizeImage(img, 1054, 526);
}
public ImageSelector(string imagesPath, bool InSubdirs)
{
classI = new List<List<string>>();
rectangles = new List<List<int[]>>();
if (!InSubdirs)
{
images = new List<string>(Directory.GetFiles(imagesPath));
}
else
{
List<string> subdirs = Directory.GetDirectories(imagesPath).ToList();
images = new List<string> { };
foreach (string dir in subdirs)
{
images.AddRange(Directory.GetFiles(dir).ToList());
}
}
imgIndex = 0;
Regex regex = new Regex(@".*\.(?i)(gif|jpe?g|tiff?|png|webp|bmp)$(?-i)");
for (int i = images.Count() - 1; i >= 0; --i)
{
if (!regex.IsMatch(images[i]))
{
images.Remove(images[i]);
}
}
for (int i = 0; i < images.Count(); ++i)
{
InitRectangles();
InitClasses();
}
Image img = Image.FromFile(images.First());
SetResizeImage(img);
}
}
}