opencv - Draw convex hull on android -
i try draw convex hull on image.for this, find contours , select max area contour. drawcontours
accepts matofpoint
convexhull
gives me matofint
. read question , run codes. of them draw convex hull, cannot find convex points.
i use below code, has runtime error on last line m.fromarray(hullpoints.get(i));
. hullpoints.get(i)
has 1 point , code cannot create matofpoint
object. how can convert matofpoint
matofint
?
// find convex hull list<matofint> hull = new arraylist<matofint>(); for(int j=0; j < contours.size(); j++){ hull.add(new matofint()); } for(int j=0; j < contours.size(); j++){ imgproc.convexhull(contours.get(j), hull.get(j)); } // convert matofint matofpoint drawing convex hull // loop on contours list<point[]> hullpoints = new arraylist<point[]>(); for(int j=0; j < hull.size(); j++){ point[] points = new point[hull.get(j).rows()]; // loop on points need hulled in current contour for(int k=0; k < hull.get(j).rows(); k++){ int index2 = (int)hull.get(j).get(k, 0)[0]; points[k] = new point(contours.get(j).get(index2, 0)[0], contours.get(j).get(index2, 0)[1]); } hullpoints.add(points); } // convert point arrays matofpoint list<matofpoint> hullmop = new arraylist<matofpoint>(); for(int j=0; j < hullpoints.size(); j++){ matofpoint m = new matofpoint(); m.fromarray(hullpoints.get(i)); hullmop.add(m); }
fwiw, i've changed fastconvexhull
here use com.google.android.gms.maps.model.latlong
. minimally tested, no guarantees.
interface:
import com.google.android.gms.maps.model.latlng; import java.util.arraylist; public interface convexhullalgorithm { arraylist<latlng> execute(arraylist<latlng> points); }
class:
public class fastconvexhull implements convexhullalgorithm { @override public arraylist<latlng> execute(arraylist<latlng> points) { arraylist<latlng> xsorted = (arraylist<latlng>) points.clone(); collections.sort(xsorted, new xcompare()); int n = xsorted.size(); latlng[] lupper = new latlng[n]; lupper[0] = xsorted.get(0); lupper[1] = xsorted.get(1); int luppersize = 2; (int = 2; < n; i++) { lupper[luppersize] = xsorted.get(i); luppersize++; while (luppersize > 2 && !rightturn(lupper[luppersize - 3], lupper[luppersize - 2], lupper[luppersize - 1])) { // remove middle point of 3 last lupper[luppersize - 2] = lupper[luppersize - 1]; luppersize--; } } latlng[] llower = new latlng[n]; llower[0] = xsorted.get(n - 1); llower[1] = xsorted.get(n - 2); int llowersize = 2; (int = n - 3; >= 0; i--) { llower[llowersize] = xsorted.get(i); llowersize++; while (llowersize > 2 && !rightturn(llower[llowersize - 3], llower[llowersize - 2], llower[llowersize - 1])) { // remove middle point of 3 last llower[llowersize - 2] = llower[llowersize - 1]; llowersize--; } } arraylist<latlng> result = new arraylist<latlng>(); (int = 0; < luppersize; i++) { result.add(lupper[i]); } (int = 1; < llowersize - 1; i++) { result.add(llower[i]); } return result; } private boolean rightturn(latlng a, latlng b, latlng c) { return (b.latitude - a.latitude) * (c.longitude - a.longitude) - (b.longitude - a.longitude) * (c.latitude - a.latitude) > 0; } private class xcompare implements comparator<latlng> { @override public int compare(latlng o1, latlng o2) { return (new float(o1.latitude)).compareto(new float(o2.latitude)); } } }
Comments
Post a Comment