[cpp] Renklendirilmiş
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- #include <opencv2/opencv.hpp>
- #include <unordered_map>
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <stack>
- using namespace cv;
- using namespace std;
- class BinaryColoring {
- private:
- Mat image;
- vector<vector<unsigned int>> intensityArray;
- unordered_map<unsigned int, int> valueCountMap;
- float initial1;
- float initial2;
- public:
- BinaryColoring(const string& imagePath, float initialCentroid1, float initialCentroid2) {
- image = imread(imagePath);
- if (image.empty()) {
- cerr << "Error: Could not read the image." << endl;
- exit(-1);
- }
- int rows = image.rows;
- int cols = image.cols;
- intensityArray.resize(rows, vector<unsigned int>(cols, 0.0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- Vec3b pixel = image.at<Vec3b>(i, j);
- unsigned int intensity = 0.21 * pixel[2] + 0.72 * pixel[1] + 0.07 * pixel[0];
- intensityArray[i][j] = intensity;
- }
- }
- for (const auto& row : intensityArray) {
- for (float value : row) {
- valueCountMap[value]++;
- }
- }
- initial1 = initialCentroid1;
- initial2 = initialCentroid2;
- }
- void runKMeans(float threshold = 0.0) {
- float tempInitial1 = initial1;
- float tempInitial2 = initial2;
- do {
- initial1 = tempInitial1;
- initial2 = tempInitial2;
- unordered_map<unsigned int, int> nearInitial1Map;
- unordered_map<unsigned int, int> nearInitial2Map;
- for (const auto& pair : valueCountMap) {
- float value = pair.first;
- float distanceToInitial1 = abs(value - tempInitial1);
- float distanceToInitial2 = abs(value - tempInitial2);
- if (distanceToInitial1 < distanceToInitial2) {
- nearInitial1Map[value] = pair.second;
- } else {
- nearInitial2Map[value] = pair.second;
- }
- }
- tempInitial1 = calculateNewCentroid(nearInitial1Map);
- tempInitial2 = calculateNewCentroid(nearInitial2Map);
- } while (abs(initial1 - tempInitial1) > threshold || abs(initial2 - tempInitial2) > threshold);
- }
- Mat getResultImage() {
- int rows = image.rows;
- int cols = image.cols;
- Mat resultImage(rows, cols, CV_8UC1, Scalar(0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- float intensity = intensityArray[i][j];
- if (abs(intensity - initial1) < abs(intensity - initial2)) {
- resultImage.at<uchar>(i, j) = 255;
- } else {
- resultImage.at<uchar>(i, j) = 0;
- }
- }
- }
- return resultImage;
- }
- private:
- float calculateNewCentroid(const unordered_map<unsigned int, int>& intensityMap) {
- float sum = 0.0;
- int totalIntensity = 0;
- for (const auto& pair : intensityMap) {
- sum += pair.first * pair.second;
- totalIntensity += pair.second;
- }
- return totalIntensity > 0 ? sum / totalIntensity : 0.0;
- }
- };
- vector<vector<int>> createBinaryImage(const Mat& resultImage) {
- int rows = resultImage.rows;
- int cols = resultImage.cols;
- vector<vector<int>> binaryArray(rows, vector<int>(cols, 0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- if (resultImage.at<uchar>(i, j) == 255) {
- binaryArray[i][j] = 1;
- }
- }
- }
- return binaryArray;
- }
- void dfsLabel(const vector<vector<int>>& binaryImage, vector<vector<int>>& labeledImage, int row, int col, int label) {
- int rows = int(binaryImage.size());
- int cols = int(binaryImage[0].size());
- stack<pair<int, int>> stk;
- stk.push({row, col});
- vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
- while (!stk.empty()) {
- auto [r, c] = stk.top();
- stk.pop();
- labeledImage[r][c] = label;
- for (const auto& dir : directions) {
- int nr = r + dir.first;
- int nc = c + dir.second;
- if (nr >= 0 && nr < rows && nc >= 0 && nc < cols &&
- binaryImage[nr][nc] == 1 && labeledImage[nr][nc] == 0) {
- stk.push({nr, nc});
- }
- }
- }
- }
- void labelComponents(const vector<vector<int>>& binaryImage, vector<vector<int>>& labeledImage) {
- int currentLabel = 1;
- int rows = int(binaryImage.size());
- int cols = int(binaryImage[0].size());
- labeledImage.assign(rows, vector<int>(cols, 0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- if (binaryImage[i][j] == 1 && labeledImage[i][j] == 0) {
- dfsLabel(binaryImage, labeledImage, i, j, currentLabel);
- currentLabel++;}
- }
- }
- }
- vector<Vec3b> createColorMap(int numLabels) {
- vector<Vec3b> colors(numLabels + 1);
- for (int i = 1; i <= numLabels; ++i) {
- colors[i] = Vec3b(rand() % 256, rand() % 256, rand() % 256);
- }
- return colors;
- }
- Mat createColoredImage(const vector<vector<int>>& labeledImage) {
- int rows = int(labeledImage.size());
- int cols = int(labeledImage[0].size());
- int numLabels = 0;
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- if (labeledImage[i][j] > numLabels) {
- numLabels = labeledImage[i][j];
- }
- }
- }
- vector<Vec3b> colors = createColorMap(numLabels);
- Mat coloredImage(rows, cols, CV_8UC3, Scalar(0, 0, 0));
- for (int i = 0; i < rows; ++i) {
- for (int j = 0; j < cols; ++j) {
- int label = labeledImage[i][j];
- if (label > 0) {
- coloredImage.at<Vec3b>(i, j) = colors[label];
- }
- }
- }
- return coloredImage;
- }
- int main() {
- string imagePath = "/Users/daghan/Desktop/şekiller.jpeg";
- float initialCentroid1 = 40.0;
- float initialCentroid2 = 120.0;
- // Load the image and perform k-means binary coloring
- BinaryColoring binaryImage(imagePath, initialCentroid1, initialCentroid2);
- binaryImage.runKMeans();
- // Get the resulting binary image
- Mat resultImage = binaryImage.getResultImage();
- // Convert the result image to a binary array
- vector<vector<int>> binaryImageArray = createBinaryImage(resultImage);
- // Create a labeled image
- vector<vector<int>> labeledImage;
- labelComponents(binaryImageArray, labeledImage);
- // Create a colored output image based on the labeled regions
- Mat coloredOutput = createColoredImage(labeledImage);
- // Display the colored output image
- namedWindow("Colored Output", WINDOW_NORMAL);
- imshow("Colored Output", coloredOutput);
- waitKey(0);
- destroyAllWindows();
- return 0;
- }
Editor
You can edit this paste and save as new: