Zaktualizuj 'examples/example_win32_directx11/main.cpp'

This commit is contained in:
Jerzy Kwiatkowski 2023-07-08 11:04:53 +02:00
parent 21218837b3
commit d7a8b8dada

View File

@ -12,6 +12,14 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
struct Pixel {
int x;
int y;
float value;
Pixel(int x, int y, float value) : x(x), y(y), value(value) {}
};
static ID3D11Device* g_pd3dDevice = nullptr;
static ID3D11DeviceContext* g_pd3dDeviceContext = nullptr;
static IDXGISwapChain* g_pSwapChain = nullptr;
@ -68,6 +76,81 @@ bool LoadTextureFromFile(unsigned char* image_data, int image_width, int image_h
#include <iostream>
#include <cmath>
#include <vector>
#include <queue>
void watershedSegmentation(unsigned char* image, int width, int height) {
const float INFINITY = std::numeric_limits<float>::max();
int* labels = new int[width * height];
for (int i = 0; i < width * height; ++i) {
labels[i] = -1;
}
float* copy = new float[width * height];
for (int i = 0; i < width * height; ++i) {
copy[i] = static_cast<float>(image[i]);
}
std::priority_queue<Pixel, std::vector<Pixel>, [](const Pixel& p1, const Pixel& p2) {
return p1.value < p2.value;
}> queue;
for (int i = 0; i < width * height; ++i) {
queue.push(Pixel(i % width, i / width, copy[i]));
}
while (!queue.empty()) {
Pixel pixel = queue.top();
queue.pop();
int x = pixel.x;
int y = pixel.y;
int index = y * width + x;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int nx = x + i;
int ny = y + j;
int nIndex = ny * width + nx;
if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] != -1) {
if (labels[index] == -1) {
labels[index] = labels[nIndex];
} else if (labels[index] != labels[nIndex]) {
labels[index] = -2;
}
}
}
}
if (labels[index] == -1) {
labels[index] = index;
copy[index] = INFINITY;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int nx = x + i;
int ny = y + j;
int nIndex = ny * width + nx;
if (nx >= 0 && nx < width && ny >= 0 && ny < height && labels[nIndex] == -1) {
queue.push(Pixel(nx, ny, copy[nIndex]));
}
}
}
}
}
for (int i = 0; i < width * height; ++i) {
if (labels[i] == -2) {
image[i] = static_cast<unsigned char>(INFINITY);
}
}
delete[] labels;
delete[] copy;
}
void dilation(unsigned char* image, int width, int height) {
int structuringElement[3][3] = {