Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Following Shambool's suggested approach, here's what I've come up with. I used a Ramer-Douglas-Peucker algorithm to simplify the contour in the region of interest and identified the two turning points. I was going to use the two turning points to get my three unknowns (xoffset, yoffset and angle of rotation), but the 2nd turning point is a bit too far toward the right because RDP simplified away the smoother curve in this region. So instead I used the angle of the line segment leading up to the 1st turning point. Differencing this angle between image1 and image2 gives me the rotation angle. I'm still not completely happy with this solution. It worked well enough for these two images, but I'm not sure it will work well on the entire image sequence. We'll see. </p> <p>It would really be better to fit the contour to the known shape of the black border.</p> <pre><code># select region of interest from largest contour ind1=where((x1&gt;190.) &amp; (y1&gt;200.) &amp; (y1&lt;900.))[0] ind2=where((x2&gt;190.) &amp; (y2&gt;200.) &amp; (y2&lt;900.))[0] figure(figsize=(10,10)) imshow(gray1,cmap=cm.gray, alpha=0.5);plot(x1[ind1],y1[ind1],'b-') imshow(gray2,cmap=cm.gray, alpha=0.5);plot(x2[ind2],y2[ind2],'g-') axis([0,1500,1000,0]) </code></pre> <p><img src="https://i.stack.imgur.com/Ynebo.png" alt="enter image description here"></p> <pre><code>def angle(x1,y1): # Returns angle of each segment along an (x,y) track return array([math.atan2(y,x) for (y,x) in zip(diff(y1),diff(x1))]) def simplify(x,y, tolerance=40, min_angle = 60.*pi/180.): """ Use the Ramer-Douglas-Peucker algorithm to simplify the path http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm Python implementation: https://github.com/sebleier/RDP/ """ from RDP import rdp points=vstack((x,y)).T simplified = array(rdp(points.tolist(), tolerance)) sx, sy = simplified.T theta=abs(diff(angle(sx,sy))) # Select the index of the points with the greatest theta # Large theta is associated with greatest change in direction. idx = where(theta&gt;min_angle)[0]+1 return sx,sy,idx sx1,sy1,i1 = simplify(x1[ind1],y1[ind1]) sx2,sy2,i2 = simplify(x2[ind2],y2[ind2]) fig = plt.figure(figsize=(10,6)) ax =fig.add_subplot(111) ax.plot(x1, y1, 'b-', x2, y2, 'g-',label='original path') ax.plot(sx1, sy1, 'ko-', sx2, sy2, 'ko-',lw=2, label='simplified path') ax.plot(sx1[i1], sy1[i1], 'ro', sx2[i2], sy2[i2], 'ro', markersize = 10, label='turning points') ax.invert_yaxis() plt.legend(loc='best') </code></pre> <p><img src="https://i.stack.imgur.com/g2xxx.png" alt="enter image description here"></p> <pre><code># determine x,y offset between 1st turning points, and # angle from difference in slopes of line segments approaching 1st turning point xoff = sx2[i2[0]] - sx1[i1[0]] yoff = sy2[i2[0]] - sy1[i1[0]] iseg1 = [i1[0]-1, i1[0]] iseg2 = [i2[0]-1, i2[0]] ang1 = angle(sx1[iseg1], sy1[iseg1]) ang2 = angle(sx2[iseg2], sy2[iseg2]) ang = -(ang2[0] - ang1[0]) print xoff, yoff, ang*180.*pi -28 14 5.07775871644 # 2x3 affine matrix M M=array([cos(ang),sin(ang),xoff,-sin(ang),cos(ang),yoff]).reshape(2,3) print M [[ 9.99959685e-01 8.97932821e-03 -2.80000000e+01] [ -8.97932821e-03 9.99959685e-01 1.40000000e+01]] # warp 2nd image into coordinate frame of 1st Minv = cv2.invertAffineTransform(M) gray2b = cv2.warpAffine(gray2,Minv,shape(gray2.T)) figure(figsize=(10,10)) imshow(gray1,cmap=cm.gray, alpha=0.5);plot(x1[ind1],y1[ind1],'b-') imshow(gray2b,cmap=cm.gray, alpha=0.5); axis([0,1500,1000,0]); title('image1 and transformed image2 overlain with 50% transparency'); </code></pre> <p><img src="https://i.stack.imgur.com/n2ZuP.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload