Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unsigned long components(int *gimage) {
- int width = image.width();
- int height = image.height();
- int curComp = 0;
- int compNum = 0;
- int leftComp, upComp;
- std::map<int, int> comps;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- int pos = i * width + j;
- if (gimage[pos] == 1) {
- if (j - 1 >= 0) {
- leftComp = gimage[i * image.width() + (j - 1)];
- }
- if (i - 1 >= 0) {
- upComp = gimage[(i - 1) * image.width() + j];
- }
- if (leftComp == 0 && upComp == 0) {
- ++curComp;
- ++compNum;
- comps[curComp] = curComp;
- gimage[pos] = curComp;
- } else {
- if (leftComp == 0) {
- gimage[pos] = comps[upComp];
- } else if (upComp == 0 || comps[leftComp] == comps[upComp]) {
- gimage[pos] = comps[leftComp];
- }
- else {
- if (leftComp < upComp) {
- gimage[pos] = comps[leftComp];
- if (comps[upComp] != comps[leftComp]) {
- comps[upComp] = comps[leftComp];
- }
- } else {
- gimage[pos] = comps[upComp];
- if (comps[leftComp] != comps[upComp]) {
- comps[leftComp] = comps[upComp];
- }
- }
- }
- }
- }
- }
- }
- std::set<int> difComps;
- for (int i = 0; i < height; i++) {
- for (int j = 0; j < width; j++) {
- int pos = i * width + j;
- if (gimage[pos] != 0) {
- gimage[pos] = comps[gimage[pos]];
- difComps.insert(gimage[pos]);
- }
- }
- }
- return difComps.size();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement