Zaktualizuj 'examples/example_win32_directx11/main.cpp'
This commit is contained in:
parent
d7a8b8dada
commit
a5e5003040
@ -78,106 +78,106 @@ bool LoadTextureFromFile(unsigned char* image_data, int image_width, int image_h
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
|
||||||
void watershedSegmentation(unsigned char* image, int width, int height) {
|
//void watershedSegmentation(unsigned char* image, int width, int height) {
|
||||||
const float INFINITY = std::numeric_limits<float>::max();
|
// const float INFINITY = std::numeric_limits<float>::max();
|
||||||
|
//
|
||||||
int* labels = new int[width * height];
|
// int* labels = new int[width * height];
|
||||||
for (int i = 0; i < width * height; ++i) {
|
// for (int i = 0; i < width * height; ++i) {
|
||||||
labels[i] = -1;
|
// labels[i] = -1;
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
float* copy = new float[width * height];
|
// float* copy = new float[width * height];
|
||||||
for (int i = 0; i < width * height; ++i) {
|
// for (int i = 0; i < width * height; ++i) {
|
||||||
copy[i] = static_cast<float>(image[i]);
|
// copy[i] = static_cast<float>(image[i]);
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
std::priority_queue<Pixel, std::vector<Pixel>, [](const Pixel& p1, const Pixel& p2) {
|
// std::priority_queue<Pixel, std::vector<Pixel>, [](const Pixel& p1, const Pixel& p2) {
|
||||||
return p1.value < p2.value;
|
// return p1.value < p2.value;
|
||||||
}> queue;
|
// }> queue;
|
||||||
|
//
|
||||||
for (int i = 0; i < width * height; ++i) {
|
// for (int i = 0; i < width * height; ++i) {
|
||||||
queue.push(Pixel(i % width, i / width, copy[i]));
|
// queue.push(Pixel(i % width, i / width, copy[i]));
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
while (!queue.empty()) {
|
// while (!queue.empty()) {
|
||||||
Pixel pixel = queue.top();
|
// Pixel pixel = queue.top();
|
||||||
queue.pop();
|
// queue.pop();
|
||||||
|
//
|
||||||
int x = pixel.x;
|
// int x = pixel.x;
|
||||||
int y = pixel.y;
|
// int y = pixel.y;
|
||||||
int index = y * width + x;
|
// int index = y * width + x;
|
||||||
|
//
|
||||||
for (int i = -1; i <= 1; ++i) {
|
// for (int i = -1; i <= 1; ++i) {
|
||||||
for (int j = -1; j <= 1; ++j) {
|
// for (int j = -1; j <= 1; ++j) {
|
||||||
int nx = x + i;
|
// int nx = x + i;
|
||||||
int ny = y + j;
|
// int ny = y + j;
|
||||||
int nIndex = ny * width + nx;
|
// int nIndex = ny * width + nx;
|
||||||
|
//
|
||||||
if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] != -1) {
|
// if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] != -1) {
|
||||||
if (labels[index] == -1) {
|
// if (labels[index] == -1) {
|
||||||
labels[index] = labels[nIndex];
|
// labels[index] = labels[nIndex];
|
||||||
} else if (labels[index] != labels[nIndex]) {
|
// } else if (labels[index] != labels[nIndex]) {
|
||||||
labels[index] = -2;
|
// labels[index] = -2;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
if (labels[index] == -1) {
|
// if (labels[index] == -1) {
|
||||||
labels[index] = index;
|
// labels[index] = index;
|
||||||
|
//
|
||||||
copy[index] = INFINITY;
|
// copy[index] = INFINITY;
|
||||||
|
//
|
||||||
for (int i = -1; i <= 1; ++i) {
|
// for (int i = -1; i <= 1; ++i) {
|
||||||
for (int j = -1; j <= 1; ++j) {
|
// for (int j = -1; j <= 1; ++j) {
|
||||||
int nx = x + i;
|
// int nx = x + i;
|
||||||
int ny = y + j;
|
// int ny = y + j;
|
||||||
int nIndex = ny * width + nx;
|
// int nIndex = ny * width + nx;
|
||||||
|
//
|
||||||
if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] == -1) {
|
// if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] == -1) {
|
||||||
queue.push(Pixel(nx, ny, copy[nIndex]));
|
// queue.push(Pixel(nx, ny, copy[nIndex]));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
for (int i = 0; i < width * height; ++i) {
|
// for (int i = 0; i < width * height; ++i) {
|
||||||
if (labels[i] == -2) {
|
// if (labels[i] == -2) {
|
||||||
image[i] = static_cast<unsigned char>(INFINITY);
|
// image[i] = static_cast<unsigned char>(INFINITY);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
//
|
||||||
delete[] labels;
|
// delete[] labels;
|
||||||
delete[] copy;
|
// delete[] copy;
|
||||||
}
|
//}
|
||||||
|
//
|
||||||
void dilation(unsigned char* image, int width, int height) {
|
//void dilation(unsigned char* image, int width, int height) {
|
||||||
int structuringElement[3][3] = {
|
// int structuringElement[3][3] = {
|
||||||
{0, 255, 0},
|
// {0, 255, 0},
|
||||||
{255, 255, 255},
|
// {255, 255, 255},
|
||||||
{0, 255, 0}
|
// {0, 255, 0}
|
||||||
};
|
// };
|
||||||
|
//
|
||||||
for (int y = 0; y < height; ++y) {
|
// for (int y = 0; y < height; ++y) {
|
||||||
for (int x = 0; x < width; ++x) {
|
// for (int x = 0; x < width; ++x) {
|
||||||
int maxPixelValue = 0;
|
// int maxPixelValue = 0;
|
||||||
for (int j = -1; j <= 1; ++j) {
|
// for (int j = -1; j <= 1; ++j) {
|
||||||
for (int i = -1; i <= 1; ++i) {
|
// for (int i = -1; i <= 1; ++i) {
|
||||||
if (x + i >= 0 && x + i < width && y + j >= 0 && y + j < height) {
|
// if (x + i >= 0 && x + i < width && y + j >= 0 && y + j < height) {
|
||||||
int imagePixelValue = image[(y + j) * width + (x + i)];
|
// int imagePixelValue = image[(y + j) * width + (x + i)];
|
||||||
int structuringElementValue = structuringElement[j + 1][i + 1];
|
// int structuringElementValue = structuringElement[j + 1][i + 1];
|
||||||
int newPixelValue = imagePixelValue + structuringElementValue;
|
// int newPixelValue = imagePixelValue + structuringElementValue;
|
||||||
if (newPixelValue > maxPixelValue) {
|
// if (newPixelValue > maxPixelValue) {
|
||||||
maxPixelValue = newPixelValue;
|
// maxPixelValue = newPixelValue;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
image[y * width + x] = static_cast<unsigned char>(maxPixelValue > 255 ? 255 : maxPixelValue);
|
// image[y * width + x] = static_cast<unsigned char>(maxPixelValue > 255 ? 255 : maxPixelValue);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
void erosion(unsigned char* image, int width, int height) {
|
void erosion(unsigned char* image, int width, int height) {
|
||||||
|
Loading…
Reference in New Issue
Block a user