[cpp] Renklendirilmiş

Viewer

copydownloadembedprintName: Renklendirilmiş
  1. #include <opencv2/opencv.hpp>
  2. #include <unordered_map>
  3. #include "opencv2/core/core.hpp"
  4. #include "opencv2/imgproc/imgproc.hpp"
  5. #include "opencv2/highgui/highgui.hpp"
  6. #include <stack>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10.  
  11. class BinaryColoring {
  12.  
  13. private:
  14.     
  15.     Mat image;
  16.     vector<vector<unsigned int>> intensityArray;
  17.     unordered_map<unsigned intint> valueCountMap;
  18.     float initial1;
  19.     float initial2;
  20.  
  21. public:
  22.     
  23.     BinaryColoring(const string& imagePath, float initialCentroid1, float initialCentroid2) {
  24.         image = imread(imagePath);
  25.  
  26.         if (image.empty()) {
  27.             cerr << "Error: Could not read the image." << endl;
  28.             exit(-1);
  29.         }
  30.  
  31.         int rows = image.rows;
  32.         int cols = image.cols;
  33.         intensityArray.resize(rows, vector<unsigned int>(cols, 0.0));
  34.  
  35.         for (int i = 0; i < rows; ++i) {
  36.             for (int j = 0; j < cols; ++j) {
  37.                 Vec3b pixel = image.at<Vec3b>(i, j);
  38.                             unsigned int intensity = 0.21 * pixel[2] + 0.72 * pixel[1] + 0.07 * pixel[0];
  39.                             intensityArray[i][j] = intensity;
  40.             }
  41.         }
  42.  
  43.         for (const auto& row : intensityArray) {
  44.             for (float value : row) {
  45.                 valueCountMap[value]++;
  46.             }
  47.         }
  48.  
  49.         initial1 = initialCentroid1;
  50.         initial2 = initialCentroid2;
  51.     }
  52.  
  53.     void runKMeans(float threshold = 0.0) {
  54.         float tempInitial1 = initial1;
  55.         float tempInitial2 = initial2;
  56.  
  57.         do {
  58.             initial1 = tempInitial1;
  59.             initial2 = tempInitial2;
  60.  
  61.             unordered_map<unsigned intint> nearInitial1Map;
  62.             unordered_map<unsigned intint> nearInitial2Map;
  63.  
  64.             for (const auto& pair : valueCountMap) {
  65.                 float value = pair.first;
  66.  
  67.                 float distanceToInitial1 = abs(value - tempInitial1);
  68.                 float distanceToInitial2 = abs(value - tempInitial2);
  69.  
  70.                 if (distanceToInitial1 < distanceToInitial2) {
  71.                     nearInitial1Map[value] = pair.second;
  72.                 } else {
  73.                     nearInitial2Map[value] = pair.second;
  74.                 }
  75.             }
  76.  
  77.             tempInitial1 = calculateNewCentroid(nearInitial1Map);
  78.             tempInitial2 = calculateNewCentroid(nearInitial2Map);
  79.  
  80.         } while (abs(initial1 - tempInitial1) > threshold || abs(initial2 - tempInitial2) > threshold);
  81.     }
  82.  
  83.     Mat getResultImage() {
  84.         int rows = image.rows;
  85.         int cols = image.cols;
  86.         Mat resultImage(rows, cols, CV_8UC1, Scalar(0));
  87.  
  88.         for (int i = 0; i < rows; ++i) {
  89.             for (int j = 0; j < cols; ++j) {
  90.                 float intensity = intensityArray[i][j];
  91.  
  92.                 if (abs(intensity - initial1) < abs(intensity - initial2)) {
  93.                     resultImage.at<uchar>(i, j) = 255;
  94.                 } else {
  95.                     resultImage.at<uchar>(i, j) = 0;
  96.                 }
  97.             }
  98.         }
  99.  
  100.         return resultImage;
  101.     }
  102.  
  103. private:
  104.     float calculateNewCentroid(const unordered_map<unsigned intint>& intensityMap) {
  105.         float sum = 0.0;
  106.         int totalIntensity = 0;
  107.  
  108.         for (const auto& pair : intensityMap) {
  109.             sum += pair.first * pair.second;
  110.             totalIntensity += pair.second;
  111.         }
  112.  
  113.         return totalIntensity > 0 ? sum / totalIntensity : 0.0;
  114.     }
  115. };
  116.  
  117. vector<vector<int>> createBinaryImage(const Mat& resultImage) {
  118.     
  119.     int rows = resultImage.rows;
  120.     int cols = resultImage.cols;
  121.  
  122.     vector<vector<int>> binaryArray(rows, vector<int>(cols, 0));
  123.  
  124.     for (int i = 0; i < rows; ++i) {
  125.         for (int j = 0; j < cols; ++j) {
  126.             if (resultImage.at<uchar>(i, j) == 255) {
  127.                 binaryArray[i][j] = 1;
  128.             }
  129.         }
  130.     }
  131.  
  132.     return binaryArray;
  133. }
  134.  
  135.  
  136. void dfsLabel(const vector<vector<int>>& binaryImage, vector<vector<int>>& labeledImage, int row, int col, int label) {
  137.     
  138.     int rows = int(binaryImage.size());
  139.     int cols = int(binaryImage[0].size());
  140.  
  141.     stack<pair<intint>> stk;
  142.     stk.push({row, col});
  143.     
  144.     vector<pair<intint>> directions = {{-10}{10}{0-1}{01}};
  145.     
  146.     while (!stk.empty()) {
  147.         auto [r, c] = stk.top();
  148.         stk.pop();
  149.  
  150.         labeledImage[r][c] = label;
  151.  
  152.         for (const auto& dir : directions) {
  153.             int nr = r + dir.first;
  154.             int nc = c + dir.second;
  155.  
  156.             if (nr >= 0 && nr < rows && nc >= 0 && nc < cols &&
  157.                 binaryImage[nr][nc] == 1 && labeledImage[nr][nc] == 0) {
  158.                 stk.push({nr, nc});
  159.             }
  160.         }
  161.     }
  162. }
  163.  
  164. void labelComponents(const vector<vector<int>>& binaryImage, vector<vector<int>>& labeledImage) {
  165.     int currentLabel = 1;
  166.     
  167.     int rows = int(binaryImage.size());
  168.     int cols = int(binaryImage[0].size());
  169.  
  170.     labeledImage.assign(rows, vector<int>(cols, 0));
  171.  
  172.     for (int i = 0; i < rows; ++i) {
  173.         for (int j = 0; j < cols; ++j) {
  174.             if (binaryImage[i][j] == 1 && labeledImage[i][j] == 0) {
  175.                 dfsLabel(binaryImage, labeledImage, i, j, currentLabel);
  176.                 currentLabel++;}
  177.         }
  178.     }
  179. }
  180.  
  181.  
  182. vector<Vec3b> createColorMap(int numLabels) {
  183.     vector<Vec3b> colors(numLabels + 1);
  184.     for (int i = 1; i <= numLabels; ++i) {
  185.  
  186.         colors[i] = Vec3b(rand() % 256rand() % 256rand() % 256);
  187.     }
  188.     return colors;
  189. }
  190.  
  191. Mat createColoredImage(const vector<vector<int>>& labeledImage) {
  192.     
  193.     int rows = int(labeledImage.size());
  194.     int cols = int(labeledImage[0].size());
  195.  
  196.     int numLabels = 0;
  197.     for (int i = 0; i < rows; ++i) {
  198.         for (int j = 0; j < cols; ++j) {
  199.             if (labeledImage[i][j] > numLabels) {
  200.                 numLabels = labeledImage[i][j];
  201.             }
  202.         }
  203.     }
  204.  
  205.     vector<Vec3b> colors = createColorMap(numLabels);
  206.  
  207.     Mat coloredImage(rows, cols, CV_8UC3, Scalar(000));
  208.  
  209.     for (int i = 0; i < rows; ++i) {
  210.         for (int j = 0; j < cols; ++j) {
  211.             int label = labeledImage[i][j];
  212.             if (label > 0) {
  213.                 coloredImage.at<Vec3b>(i, j) = colors[label];
  214.             }
  215.         }
  216.     }
  217.  
  218.     return coloredImage;
  219. }
  220.  
  221. int main() {
  222.     string imagePath = "/Users/daghan/Desktop/şekiller.jpeg";
  223.     float initialCentroid1 = 40.0;
  224.     float initialCentroid2 = 120.0;
  225.  
  226.     // Load the image and perform k-means binary coloring
  227.     BinaryColoring binaryImage(imagePath, initialCentroid1, initialCentroid2);
  228.     binaryImage.runKMeans();
  229.  
  230.     // Get the resulting binary image
  231.     Mat resultImage = binaryImage.getResultImage();
  232.  
  233.     // Convert the result image to a binary array
  234.     vector<vector<int>> binaryImageArray = createBinaryImage(resultImage);
  235.  
  236.     // Create a labeled image
  237.     vector<vector<int>> labeledImage;
  238.     labelComponents(binaryImageArray, labeledImage);
  239.  
  240.     // Create a colored output image based on the labeled regions
  241.     Mat coloredOutput = createColoredImage(labeledImage);
  242.  
  243.     // Display the colored output image
  244.     namedWindow("Colored Output", WINDOW_NORMAL);
  245.     imshow("Colored Output", coloredOutput);
  246.     waitKey(0);
  247.     destroyAllWindows();
  248.  
  249.     return 0;
  250. }
  251.  

Editor

You can edit this paste and save as new:


File Description
  • Renklendirilmiş
  • Paste Code
  • 25 Apr-2024
  • 7.21 Kb
You can Share it: