-
Notifications
You must be signed in to change notification settings - Fork 0
/
morphology.cpp
73 lines (54 loc) · 1.99 KB
/
morphology.cpp
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
#include "morphology.h"
#include <QColor>
#include <QPainter>
// Структурные элементы
QImage* disk(int radius, QRgb color)
{
int size = radius * 2 - 1;
QImage *disk = new QImage(radius * 2 - 1, radius * 2 - 1, QImage::Format_ARGB32);
disk->fill(Qt::transparent);
QColor qcolor(color);
QPainter painter;
painter.begin(disk);
painter.setRenderHint(QPainter::Antialiasing, false);
painter.setPen(qcolor);
painter.setBrush(QBrush(qcolor));
painter.drawEllipse(0, 0, size, size);
painter.end();
return disk;
}
void dilation(QImage* origin, const QImage &element, const QRgb pixelColor, const QRgb background)
{
int width = origin->width(),
height = origin->height(),
elementSizeX= element.width(),
elementSizeY= element.height(),
elementRadX = elementSizeX / 2,
elementRadY = elementSizeY / 2;
QImage::Format origFormat = origin->format();
QVector<QRgb> colorTable;
if (origFormat == QImage::Format_Indexed8)
colorTable = origin->colorTable();
QImage *delationResult = new QImage(width, height, QImage::Format_ARGB32_Premultiplied);
*origin = origin->convertToFormat(QImage::Format_ARGB32_Premultiplied);
delationResult->fill(QColor(background));
QPainter painter;
painter.begin(delationResult);
QRgb* pixel = (QRgb*)origin->bits();
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++, pixel++)
{
// Проверяем, пиксель
if (*pixel == pixelColor)
painter.drawImage(x - elementRadX,
y - elementRadY,
element);
}
painter.end();
if (origFormat == QImage::Format_Indexed8)
*delationResult = delationResult->convertToFormat(origFormat, colorTable);
else
*delationResult = delationResult->convertToFormat(origFormat);
*origin = *delationResult;
delete delationResult;
}