How to connect broken curves in matplotlab, scipy or etc

I have a little problem, when I take the outline of the image, I get this figure:

iJ9fp.pngq6l0h.png

As you can see, I can extract the path, but as soon as I get the path, it will leave these strange secants that cross the image, because there are two discontinuous areas on the curve. I wonder if there is a way to disable broken lines or is this the wrong path extraction code?

import matplotlib.pyplot as plt import numpy as np def contourPath(img, width, height): x = np.arange(0,width) y = np.arange(height,0,-1) X, Y = np.meshgrid(x,y) plot = plt.contour(X,Y,img, [0]) pathList = plot.collections[0].get_paths() x, y = [], [] for i in range(0, len(pathList)): iterPath = pathList[i].iter_segments() for point in iterPath: pt = np.rint(point[0]) x.append(pt[0]) y.append(pt[1]) X = np.hstack(x) Y = np.hstack(y) return np.dstack((X,Y))[0] 

thank you for your time

for user545424 I think. The Matplotlib outline function works correctly because there are two intermittent spots in the image that caused this small event.

I find out that these cutting lines are the cause of scypi, but there is another problem about how libraries interact with contour points

Well, I believe that you can mask the problem by finding the path and interpolating it. But I like to avoid going around the path, since the problem of moving the sallines on my computer is not very pleasant.

Do you have any suggestions?

enter image description here

+6
source share
1 answer

I think I'm answering my question. These strange secants are the result of scraping the ends of the list. By default, I believe that scipy will connect 2 consecutive points regardless of location. The vertices work correctly because he strictly adhered to the outline of this image and he surrounded the curve. The reason the secut is associated with the middle of the upper semicircle. This is the place where the last journey ends.

I suggest that converting to polar and removing the path is simple, but not entirely optimistic, but good.

I wonder if anyone has a better solution

  def cart2polar(x,y, origin=None, size=np.array([200,200])): ny, nx= size[0], size[1] print size.shape if origin is None: origin_x, origin_y = nx//2, ny//2 else: origin_x, origin_y = origin[0], origin[1] x -= origin_x y -= origin_y r = np.sqrt(x**2 + y**2) theta = np.arctan2(y, x) return r, theta def main(): index = np.argsort(theta) r, theta = r[index[:]], theta[index[:]] f = interpolate.interp1d(theta,r) theta = np.linspace(round(theta.min()+.00005,),theta.max(), 200) r = f(theta) x,y = polar2cart(r, theta) def polar2cart(r, theta, origin=None, size=np.array([200,200]) ): ny, nx= size[0], size[1] if origin is None: origin_x, origin_y = nx//2, ny//2 else: origin_x, origin_y = origin[0], origin[1] x += origin_x y += origin_y return x, y 
+3
source

Source: https://habr.com/ru/post/918308/


All Articles