Last active
June 7, 2022 07:24
-
-
Save Lmy0217/3c550a624d09bbd759d24adf0d54d912 to your computer and use it in GitHub Desktop.
Implementation Of The Concatenation Domain Mark Function Bwlabel (Matlab) In C++. Input: binary (binary image array), row (image high), col (image width). Output: bwlabel (an array of the connected domain, and a serial number for each connected domain in the image. In particular, bwlabel[0] stores the serial number of the maximum connected area).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<vector> | |
| #include<string> | |
| #include<stdio.h> | |
| using namespace std; | |
| typedef struct Run { | |
| int row; | |
| int startCol; | |
| int endCol; | |
| int flag; | |
| }Run; | |
| int* c_bwlabel(int *binary, int row, int col) { | |
| Run run; | |
| vector<Run> runSet; | |
| vector<int> runFlag; | |
| vector< vector<int> > runFlagSet; | |
| int probin, bin; | |
| int runNo = -1, flag = 1, proRowEndRunNo; | |
| int i, j, k; | |
| for(i = 0; i < row; i++) { | |
| bin = 0; | |
| proRowEndRunNo = runNo; | |
| for(j = 0; j < col; j++) { | |
| probin = bin; | |
| bin = binary[j + i * col]; | |
| if(probin == 0 && bin == 1) { | |
| run.row = i; | |
| run.startCol = j; | |
| } | |
| if((probin == 1 && bin == 0) || (bin == 1 && j == col - 1)) { | |
| run.endCol = (bin == 1 && j == col - 1) ? j : j - 1; | |
| // compute flag | |
| run.flag = -1; | |
| if(proRowEndRunNo == -1 || (proRowEndRunNo >= 0 && runSet[proRowEndRunNo].row != i - 1)) { | |
| run.flag = flag++; | |
| runFlag.clear(); | |
| runFlag.push_back(run.flag); | |
| runFlagSet.push_back(runFlag); | |
| } | |
| for(k = proRowEndRunNo; k >= 0 && i > 0 && runSet[k].row == i - 1; k--) { | |
| if(runSet[k].endCol + 1 < run.startCol) { | |
| if(run.flag == -1) { | |
| run.flag = flag++; | |
| runFlag.clear(); | |
| runFlag.push_back(run.flag); | |
| runFlagSet.push_back(runFlag); | |
| } | |
| break; | |
| } else if (run.endCol < runSet[k].startCol - 1) { | |
| if(k == 0 || runSet[k - 1].row != i - 1) { | |
| run.flag = flag++; | |
| runFlag.clear(); | |
| runFlag.push_back(run.flag); | |
| runFlagSet.push_back(runFlag); | |
| } | |
| } else { | |
| if(run.flag == -1) { | |
| run.flag = runSet[k].flag; | |
| } else { | |
| int vnothis = -1; | |
| int vnok = -1; | |
| for(int ti = 0; ti < runFlagSet.size(); ti++) { | |
| if(find(runFlagSet[ti].begin(), runFlagSet[ti].end(), run.flag) != runFlagSet[ti].end()) { | |
| vnothis = ti; | |
| break; | |
| } | |
| } | |
| for(int tj = 0; tj < runFlagSet.size(); tj++) { | |
| if(find(runFlagSet[tj].begin(), runFlagSet[tj].end(), runSet[k].flag) != runFlagSet[tj].end()) { | |
| vnok = tj; | |
| break; | |
| } | |
| } | |
| if(vnothis != vnok) { | |
| runFlagSet[vnothis].insert(runFlagSet[vnothis].end(), runFlagSet[vnok].begin(), runFlagSet[vnok].end()); | |
| runFlagSet.erase(runFlagSet.begin() + vnok); | |
| } | |
| } | |
| } | |
| } | |
| runSet.push_back(run); | |
| runNo++; | |
| } | |
| } | |
| } | |
| int* bwlabel = new int[row * col]; | |
| for(i = 0; i < row * col; i++) | |
| bwlabel[i] = 0; | |
| for(i = 0; i <= runNo; i++) { | |
| for(k = runSet[i].startCol; k <= runSet[i].endCol; k++) { | |
| for(int t = 0; t < runFlagSet.size(); t++) { | |
| if(find(runFlagSet[t].begin(), runFlagSet[t].end(), runSet[i].flag) != runFlagSet[t].end()) { | |
| bwlabel[k + runSet[i].row * col] = t + 1; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| ///* | |
| int* temp = new int[runFlagSet.size() + 1]; | |
| int index = 0; | |
| for(int t = 0; t < runFlagSet.size() + 1; t++) { | |
| temp[t] = 0; | |
| } | |
| for(i = 0; i < row * col; i++) { | |
| int tb = bwlabel[i]; | |
| if(tb != 0 && temp[tb] == 0) { | |
| temp[tb] = ++index; | |
| } | |
| bwlabel[i] = temp[tb]; | |
| } | |
| delete[] temp; | |
| //*/ | |
| int* size = new int[index + 1]; | |
| for(int t = 0; t < index + 1; t++) { | |
| size[t] = 0; | |
| } | |
| for(i = 0; i < row * col; i++) { | |
| if(bwlabel[i] != 0) | |
| size[bwlabel[i]]++; | |
| } | |
| int t = 0; | |
| for(i = 1; i < index + 1; i++) { | |
| if(size[i] > size[t]) | |
| t = i; | |
| } | |
| bwlabel[0] = t; | |
| return bwlabel; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment