-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrawer.cpp
85 lines (76 loc) · 2.62 KB
/
drawer.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
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <algorithm>
#include "drawer.h"
bool Drawer::Is_Boundary(const QImage &Label, int x, int y){
if (y > 0) {
if (qGray(Label.pixel(x, y)) != qGray(Label.pixel(x, y-1))) {
return true;
}
}
if (y < Label.height() - 1) {
if (qGray(Label.pixel(x, y)) != qGray(Label.pixel(x, y+1))) {
return true;
}
}
if (x > 0) {
if (qGray(Label.pixel(x, y)) != qGray(Label.pixel(x-1, y))) {
return true;
}
}
if (x < Label.width() - 1) {
if (qGray(Label.pixel(x, y)) != qGray(Label.pixel(x+1, y))) {
return true;
}
}
return false;
}
QImage Drawer::Draw_Contours(const QImage &Label, const QImage &Original_Image, uint color){
QImage image(Original_Image.convertToFormat(QImage::Format_RGB32));
for(int y = 0; y < image.height(); y++) {
for(int x = 0; x < image.width(); x++) {
if (Is_Boundary(Label, x, y)) {
image.setPixel(x, y, color);
}
}
}
return image;
}
QImage Drawer::Filter(const QImage &Original_Image){
int Width = Original_Image.width();
int Height = Original_Image.height();
int N = Width*Height;
QImage image(Original_Image.convertToFormat(QImage::Format_RGB32));
int *Image_Matrix = (int*)malloc(N*sizeof(int*));
if (Image_Matrix == NULL) {
return Original_Image;
}
for(int y = 0; y < Height; y++) {
for(int x = 0; x < Width; x++) {
int n = Width*y + x;
Image_Matrix[n] = qGray(image.pixel(x, y));
}
}
QColor color;
QColor orig;
int *neighborhood = (int*)malloc(9*sizeof(int));
int median;
for(int y = 1; y < Height-1; y++) {
for(int x = 1; x < Width-1; x++) {
neighborhood[0] = Image_Matrix[(y-1)*Width + (x-1)];
neighborhood[1] = Image_Matrix[(y-1)*Width + (x)];
neighborhood[2] = Image_Matrix[(y-1)*Width + (x+1)];
neighborhood[3] = Image_Matrix[(y)*Width + (x-1)];
neighborhood[4] = Image_Matrix[(y)*Width + (x)];
neighborhood[5] = Image_Matrix[(y)*Width + (x+1)];
neighborhood[6] = Image_Matrix[(y+1)*Width + (x-1)];
neighborhood[7] = Image_Matrix[(y+1)*Width + (x)];
neighborhood[8] = Image_Matrix[(y+1)*Width + (x+1)];
std::sort(neighborhood, neighborhood+9);
median = neighborhood[5];
orig = image.pixel(x, y);
color = QColor::fromHsv(orig.hue(), orig.saturation(), median);
image.setPixelColor(x, y, color);
}
}
return image;
}