Ask Your Question
0

How to save extracted contours into a text file?

asked 2017-09-15 05:12:05 -0600

Santhosh1 gravatar image

I tried using the command in this link

Here's my code

for i in range(len(contours)):
    i_contour=contours[i]
    np.savetxt('SmallBLockXY.txt',(contours[i]),fmt="%01d",delimiter=" ",newline='\n')

This is the error I am getting

Traceback (most recent call last):
File "/Users/admin/.virtualenvs/test/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1254, in savetxt
fh.write(asbytes(format % tuple(row) + newline))
TypeError: %d format: a number is required, not numpy.ndarray

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
np.savetxt('SmallBLockXY.txt',(contours[i]),fmt="%01d",delimiter=" ",newline='\n')
  File "/Users/admin/.virtualenvs/test/lib/python3.6/site-packages/numpy/lib/npyio.py", line 1258, in savetxt
% (str(X.dtype), format))
TypeError: Mismatch between array dtype ('int32') and format specifier ('%01d')

I tried an alternative solution

i_contour.tofile('SamllBlockXY.txt',sep=' ')

But with this I'm getting the output of only the last contour which was detected that too all in one like back to back

Any suggestions on how to get the contour coordinates on each point on the contour in a separate line into a text file.

edit retag flag offensive close merge delete

Comments

Solved it! :D Can't post the answer till two days as I am a newbie here :(

Santhosh1 gravatar imageSanthosh1 ( 2017-09-15 05:34:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-18 09:07:50 -0600

Santhosh1 gravatar image
for c in range(len(contours)):
    n_contour = contours[c]
    for d in range(len(n_contour)):
        XY_Coordinates = n_contour[d]
        print(XY_Coordinates)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-15 05:12:05 -0600

Seen: 3,324 times

Last updated: Sep 18 '17