Answer the question
In order to leave comments, you need to log in
How to crop extra path fragments in OpenCV?
Hello. I am engaged in the task of finding the central vein on the leaf of a plant.
For example, there are the following photographs, where I circled in red what I need to highlight:
and using the search for contours and games with color and channels, side veins are also highlighted: I
used the search for lines, but it turns out to be porridge
from which I mostly could clear the side contours by discarding them according to a certain condition,
but there are still garbage lines highlighted with red squares ....
and an attempt to convert the lines back to a contour leads to the following result:
Who has any ideas how to solve this problem?
PS> I used tips like the binarization threshold at the very first stage, and to highlight the contour of the veins I use the following code:
src = qimage2mat(img);
cvtColor(src,src, CV_RGB2HSV_FULL);
// Create a structuring element (SE)
int morph_size = 2;
Mat element = getStructuringElement( MORPH_RECT, Size(morph_size+1, morph_size+1), Point( morph_size, morph_size) );
morphologyEx( src, dst, MORPH_TOPHAT, element, Point(-1,-1),15 );
// Apply the specified morphology operation
vector<Mat> rgbChannels(3);
split(dst, rgbChannels);
bitwise_not(rgbChannels[1],tst);
short int thVmin = 190;
threshold(tst,dst, thVmin, 255, THRESH_BINARY);
vector<vector<Point> > contours;
vector<Point> leafContour;
vector<Vec4i> hierarchy;
findContours(dst, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
//ищем линии
Mat cdst;
Canny(drawing, dst, 50, 200,5);
cvtColor(dst, cdst, COLOR_GRAY2BGR);
vector<Vec4i> lines, lines1;
//параметры 20,5,30 определены эксперементально
HoughLinesP(dst, lines, 1, CV_PI/360, 20, 5, 30 );
int mmY1 = mmY-80;
int mmY2 = mmY+80;
//делаем первую операцию по очистке
for (unsigned int i = 0; i < lines.size(); i++ ) {
Vec4i l = lines[i];
if( (l[1] > mmY1 && l[1]<mmY2) &&
(l[3] > mmY1 && l[3]<mmY2)) {
lines1.push_back(l);
}
}
lines.clear();
int dmX = round(mmX/7);
for(size_t i=0; i < lines1.size(); i++) {
Vec4i l = lines1[i];
if(l[2] < dmX) { //хвостик листа
if (abs(l[0]-l[2])>abs(l[1]-l[3])/2) {
lines.push_back(l);
}
} else { //остальная часть листа
if (abs(l[1]-l[3]) < 20 && abs(l[0]-l[2]) > 20) { //откидываем острый угол
if (abs(l[0]-l[2])/2>abs(l[1]-l[3])) {
lines.push_back(l);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question