Preform separation and image segmentation

asked 2020-09-25 05:48:23 -0600

jast_r gravatar image

updated 2020-09-25 09:05:59 -0600

Hi everyone! I'm beginner at OpenCV and Computer Vision.


I need to solve the problem of separating connected preforms. I'm write code that allows me to handle a single preform fairly well, but if there are more than 1 of them, then there are problems, which you can see in the picture. How can I solve this problem? I read about the watershed, but its application did not give the desired result.

Only one preform

Two preform

Source code

If u want u can get source image this: click

#include "mainwindow.h"

using namespace std;
using namespace cv;

// Найти уравнение прямой
void line_equation(Point src1, Point src2, double& k, int& b) {
    if ((src1.x != src2.x) && (src1.y != src2.y)) {
        k = (double) (src1.y - src2.y) / (double) (src1.x - src2.x);
        b = src1.y - (k * src1.x);
    } else {
        k = 0;
    }
}

void find_parallel(Point src, vector<Point>& dst, double k, int rows) {
    int b = src.y - k * src.x;
    dst.push_back(Point(-b/k, 0));
    dst.push_back(Point((rows - b) / k, rows));
}

// Найти точки пересечения прямой и контура
void line_interseption(vector<Point> src_line, Point p, Point q, vector<Point>& dst_point) {
    int d = 0;
    bool first_point_flag = false;
    bool second_point_flag = false;
    for(size_t i = 1; i < src_line.size(); i++) {
        d = abs((p.y - q.y)*src_line[i].x - (p.x - q.x)*src_line[i].y + p.x * q.y - p.y * q.x)
            /sqrt((p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
        if(p.x != q.x) {
            if((d == 0) && !first_point_flag && (p.x > src_line[i].x)) {
                dst_point.push_back(src_line[i]);
                first_point_flag = true;
            }
            if((d == 0) && !second_point_flag && (p.x < src_line[i].x)) {
                dst_point.push_back(src_line[i]);
                second_point_flag = true;
            }
        } else {
            if((d == 0) && !first_point_flag && (p.y > src_line[i].y)) {
                dst_point.push_back(src_line[i]);
                first_point_flag = true;
            }
            if((d == 0) && !second_point_flag && (p.y < src_line[i].y)) {
                dst_point.push_back(src_line[i]);
                second_point_flag = true;
            }
        }
    }
}

// Найти точку пересечения прямых
void intersection_points(Point flp1, Point flp2, Point slp1, Point slp2, Point& dst_point) {
    double k1 = (double)(flp2.y - flp1.y) / (flp2.x - flp1.x);
    double k2 = (double)(slp2.y - slp1.y) / (slp2.x - slp1.x);
    double b1 = k1 * flp1.x - flp1.y;
    double b2 = k2 * slp1.x - slp1.y;
    dst_point.x = -(b1 - b2) / (k2 - k1);
    dst_point.y = -(k2 * b1 - k1 * b2) / (k2 - k1);
}


// Найти максимально удаленную точку от прямой
void find_max(vector<Point> sepCont, Point p, Point q, Point &dst) {
    dst = sepCont[0];
    int d = 0;
    int d_max = abs((p.y - q.y)*sepCont[0].x - (p.x - q.x)*sepCont[0].y + p.x * q.y - p.y * q.x)
                /sqrt((p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q.x));
    for(size_t i = 1; i < sepCont.size(); i++) {
        d = abs((p.y - q.y)*sepCont[i].x - (p.x - q.x)*sepCont[i].y + p.x * q.y - p.y * q.x)
            /sqrt((p.y - q.y) * (p.y - q.y) + (p.x - q.x) * (p.x - q ...
(more)
edit retag flag offensive close merge delete

Comments

what exactly is a "preform" ?

can you explain, what you want to achieve here ?

your images need some explanation, what should we see there ?

I read about the waterfall

what do you mean ? (watershed ?)

berak gravatar imageberak ( 2020-09-25 06:50:43 -0600 )edit
1

yep, watershed, sry for this. this is preform: click

jast_r gravatar imagejast_r ( 2020-09-25 09:08:39 -0600 )edit

you google drive link is not shareable, please put the image here, anyway.

berak gravatar imageberak ( 2020-09-25 09:55:58 -0600 )edit
1

oh, I'm sorry, I didn't notice ... I just have quite a lot of photos and I thought it would not be very good to post them here

jast_r gravatar imagejast_r ( 2020-09-26 06:56:06 -0600 )edit