# Preform separation and image segmentation

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.  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;
int d = 0;
int d_max = abs((p.y - q.y)*sepCont.x - (p.x - q.x)*sepCont.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 ...
edit retag close merge delete

what exactly is a "preform" ?

can you explain, what you want to achieve here ?

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