Ask Your Question
0

Translating C++ code into python. Facing some problem

asked 2018-12-28 18:07:39 -0600

Sky31 gravatar image

updated 2018-12-29 01:47:42 -0600

Hello guys, I need your help.

I'm trying to translate c++ code into python but stuck somewhere.

Below is the code, which I have to convert.

oid Detection::findBestContour(Mat edge , vector<point> & contour ,vector<point> &hull,bool vis ) { vector<vector<point> > contours; vector<vec4i> hierarchy; int maxIndex=0,maxArea=0,tmpArea=0,minArea=100; // find the contours findContours( edge , contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) ); //compute the convex hull vector<vector<point> >hulls( contours.size() ); for( int i = 0; i < contours.size(); i++ ) { convexHull( Mat(contours[i]), hulls[i], false ); }

        vector<int> indices(contours.size());
        iota(indices.begin(), indices.end(), 0);

        sort(indices.begin(), indices.end(), [&contours,&hulls](int lhs, int rhs) {
            return contourArea(hulls[lhs]) > contourArea( hulls[rhs]);
        });
        Mat res = Mat::zeros(edge.rows, edge.cols, CV_8UC3);
        int N = 5; // set number of largest contours
        N = min(N, int(contours.size()));



    if(contours.size()==0)
    return;
    drawContours(res, hulls, indices[0], Scalar{255,0,0});
    //hull=hulls[indices[0]];
    contour=contours[indices[0]];
    for (int i =1; i < N; ++i)
        {
            //hull.insert( hull.end(), hulls[indices[i]].begin(), hulls[indices[i]].end() );
            double area=contourArea(hulls[indices[i]]);
            double arc=arcLength(contours[indices[i]], true);
            if((area/arc)>5)
            {
                vector<Point> tmpContour;
                vector<Point> tmpHull1;
                vector<Point> tmpHull2;
                tmpContour.insert( tmpContour.end(), contours[indices[i]].begin(), contours[indices[i]].end() );
                tmpContour.insert( tmpContour.end(), contour.begin(),contour.end() );
                convexHull( Mat(contour), tmpHull1, false );
                convexHull( Mat(tmpContour), tmpHull2, false );
                if((contourArea(tmpHull1)/contourArea(tmpHull2))<0.95&&
                (contourArea(tmpHull1)/contourArea(tmpHull2))>0.8)
                {
                    Scalar color(rand() & 255, rand() & 255, rand() & 255);
                    //drawContours(res, contours, indices[i], color);
                    drawContours(res, hulls, indices[i], color);
                    contour.insert( contour.end(), contours[indices[i]].begin(), contours[indices[i]].end() );
                }
            }
        }
    convexHull( Mat(contour), hull, false );
    if(vis)
    imshow("contour",res);

}

This is what I did.

def findBestContour(edges,contor,hull):

r,c = edges.shape
print(r,c)
_,contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
hull =[]
for i in range(len(contours)):
    hull.append(cv2.convexHull(contours[i], False))

indices = len(contours)
indices = range(0,len(contours))
   # p#rint("indices",indices)
res = np.zeros([r,c], dtype='uint8')
N = 5
N = min(N,len(contours))

if(len(contours)==0):
    return 1
cv2.drawContours(res,hull,indices[0],(255,0,0))
contour =contours[indices[0]]
print("contour",contour)
i =1
while i<N:
    area = cv2.contourArea(hull[indices[i]])
    print("area",area)
    arc = cv2.arcLength(contours[indices[i]],True)
    print("arc",arc)
    if((area/arc)>5):



        tmpContour = [] 
        tmphull1 = []
        tmphull2 = []


        _ =[ tmpContour.insert(len(tmpContour),contours[indices[i]][j]) for j in range(len(contours[indices[i]])) ]

         _ =[ tmpContour.insert(len(tmpContour),contour[j]) for j in range(len(contour)) ]

        tmphull1 =(cv2.convexHull(contour,False))
        print("tmphull1",tmphull1)

        if (cv2.contourArea(tmphull1)/cv2.contourArea(tmphull2))<0.95 and (cv2.contourArea(tmphull1)/cv2.contourArea(tmphull2) > 0.8):
            cv2.drawContours(res,hull,i,(255,255,255))
            _ =[ contour.insert(len(contour),contours[indices[i]][j]) for j in range(len(contours[indices[i]]))]

    i=1+i

cv2.convexHull ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-12-30 04:50:40 -0600

LBerger gravatar image

updated 2018-12-30 05:33:41 -0600

About contours and sort I think you can use this :

import numpy as np
import cv2  as cv

def surface(p1):
    x1 = cv.contourArea(p1[2])
    return x1

img = cv.imread("g:/lib/opencv/samples/data/pic1.png",cv.IMREAD_GRAYSCALE)
if img is None:
    print("Check file path")
    exit()
ret,imgbin =cv.threshold(img,50,255,cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(imgbin,  cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

ctrhullList=[]
for idx,p in enumerate(contours):
    hull = cv.convexHull(p)
    ctrhullList.append([idx,p,hull])
print ('before sort')
for p in ctrhullList:
    print (p[0],' : Surface = ', cv.contourArea(p[1]),'  HullSurface = ',cv.contourArea(p[2]))
ctrhullList.sort(key=surface)
print ('after sort')
for p in ctrhullList:
    print (p[0],' : Surface = ', cv.contourArea(p[1]),'  HullSurface = ',cv.contourArea(p[2]))

I use surface as lambda function

or you can forget lambda function :

import numpy as np
import cv2  as cv


img = cv.imread("g:/lib/opencv/samples/data/pic1.png",cv.IMREAD_GRAYSCALE)
if img is None:
    print("Check file path")
    exit()
ret,imgbin =cv.threshold(img,50,255,cv.THRESH_BINARY)
contours, hierarchy = cv.findContours(imgbin,  cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)

ctrhullList=[]
for idx,p in enumerate(contours):
    hull = cv.convexHull(p)
    ctrhullList.append(cv.contourArea(hull))
print ('before sort')
for idx,p in enumerate(ctrhullList):
    print (idx,' : Surface = ', cv.contourArea(contours[idx]),'  HullSurface = ',p)
ctrhullList = np.array(ctrhullList)
indice=ctrhullList.argsort()
print ('after sort')
for p in indice:
#    print (ctrhullList[p,0],' : Surface = ', cv.contourArea(ctrhullList[p,1]),'  HullSurface = ',cv.contourArea(ctrhullList[p,2]))
    print (p,' : Surface = ', cv.contourArea(contours[p]),'  HullSurface = ',ctrhullList[p])
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-12-28 18:06:00 -0600

Seen: 269 times

Last updated: Dec 30 '18