[cpp] OpenCV Binary Image
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/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include <opencv2/opencv.hpp>
- #include <unordered_map>
- #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 << "Resim okuma hatası!" << 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;
- }
- vector<vector<int>> labelComponentsOnImage(const vector<vector<int>>& binaryImage) {
- int matris_rows = int(binaryImage.size());
- int matris_cols = int(binaryImage[0].size());
- vector<vector<int>> labeledImage(matris_rows, vector<int>(matris_cols, 0));
- vector<int> labelTable(1, 0);
- int numLabel = 1;
- for(int r = 0; r < matris_rows; r++)
- {
- for(int c = 0; c < matris_cols; c++)
- {
- if(binaryImage[r][c] == 0){labeledImage[r][c] = 0;}
- else{
- labeledImage[r - 1][c] = (r > 0) ? labeledImage[r - 1][c] : 0;
- labeledImage[r][c - 1] = (c > 0) ? labeledImage[r][c - 1] : 0;
- if(labeledImage[r - 1][c] == 0
- && labeledImage[r][c - 1] == 0)
- {labeledImage[r][c] = numLabel;
- labelTable.push_back(numLabel);
- numLabel++;}
- else if(labeledImage[r - 1][c] != 0
- && labeledImage[r][c - 1] == 0)
- {labeledImage[r][c] = labeledImage[r - 1][c];}
- else if(labeledImage[r - 1][c] == 0
- && labeledImage[r][c - 1] != 0)
- {labeledImage[r][c] = labeledImage[r][c - 1];}
- else
- {labeledImage[r][c] = min(labeledImage[r - 1][c],labeledImage[r][c - 1]);
- if(labeledImage[r - 1][c] != labeledImage[r][c - 1]) {
- int max_label = max(labeledImage[r - 1][c], labeledImage[r][c - 1]);
- int min_label = min(labeledImage[r - 1][c], labeledImage[r][c - 1]);
- for(int i = 1; i < labelTable.size(); i++)
- {
- if(labelTable[i] == max_label)
- labelTable[i] = min_label;
- }
- }
- }
- }
- }
- }
- for(int r = 0; r < matris_rows; r++)
- {
- for(int c = 0; c < matris_cols; c++)
- {
- if(labeledImage[r][c] != 0)
- labeledImage[r][c] = labelTable[labeledImage[r][c]];
- }
- }
- return labeledImage;
- }
- int main() {
- string imagePath = "/Users/daghan/Desktop/şekiller2.jpeg";
- float initialCentroid1 = 40.0;
- float initialCentroid2 = 120.0;
- BinaryColoring myImage(imagePath, initialCentroid1, initialCentroid2);
- myImage.runKMeans();
- Mat resultImage = myImage.getResultImage();
- vector<vector<int>> binaryImage = createBinaryImage(resultImage);
- vector<vector<int>> labeledImage;
- labeledImage = labelComponentsOnImage(binaryImage);
- return 0;
- }
Editor
You can edit this paste and save as new: