I need to find contour-specific coordinates, as can be seen in this image. I am able to draw a specific contour (highlighted in red) but the .txt
file is giving the same set of coordinates, even if I am changing the contour.
我需要找到轮廓特定的坐标,如图中所示。我能够绘制一个特定的轮廓(以红色突出显示),但.txt文件提供相同的坐标集,即使我正在改变轮廓。
import numpy as np
import cv2
im = cv2.imread("ceramic.bmp")
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,55,255,0)
_th,contours, hierarchy =
cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print" Number of contours detected %d.>"%len(contours)
cv2.drawContours(im,contours,55,(1,70,255)) #-1 fills it
area = cv2.contourArea(contours[55])
print (area)
cv2.imshow("Contours",im)
text_file = open("contour_list_55.txt", "w") ##### Write the
coordinates (x,y) of a contour in an txt file python
text_file.write( "%s"% contours)
text_file.close()
cv2.waitKey(0)
cv2.destroyAllWindows()`
1 个解决方案
#1
0
This is because you are writing all the contours to file, not just a single contour.
这是因为您要将所有轮廓都写入文件,而不仅仅是单个轮廓。
You are drawing a contour with index 55:
您正在绘制索引为55的轮廓:
area = cv2.contourArea(contours[55])
But your command for the .txt file is:
但是你对.txt文件的命令是:
text_file.write( "%s"% contours)
It should be something like:
它应该是这样的:
text_file.write( "%s" % contours[55] )
That is if you want to get coordinates of the same contour that you draw.
也就是说,如果您想获得绘制的相同轮廓的坐标。
I also generally suggest you to follow the DRY (Don't Repeat Yourself) principle in programming. If you have a constant (like 55) you should define it and use it by name throughout your code. For example:
我还建议你在编程中遵循DRY(不要重复自己)的原则。如果你有一个常量(如55),你应该定义它并在整个代码中按名称使用它。例如:
import numpy as np
import cv2
THRESHOLD = 55
CONTOUR = 55
im = cv2.imread("ceramic.bmp")
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,THRESHOLD,255,0)
_th,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print "Number of contours detected = %d" % len(contours)
cv2.drawContours(im,contours,CONTOUR,(1,70,255))
area = cv2.contourArea(contours[CONTOUR])
print (area)
cv2.imshow("Contours",im)
np.set_printoptions(threshold=np.inf)
coords = np.array2string(contours[CONTOUR])
open("contour_%d.txt" % CONTOUR, "w").write(coords)
cv2.waitKey(0)
cv2.destroyAllWindows()`
It will write the coordinates to file contour_55.txt
. And if you want to change to another contour, you just have to change the constant CONTOUR.
它会将坐标写入文件contour_55.txt。如果你想换到另一个轮廓,你只需要改变常量轮廓。
#1
0
This is because you are writing all the contours to file, not just a single contour.
这是因为您要将所有轮廓都写入文件,而不仅仅是单个轮廓。
You are drawing a contour with index 55:
您正在绘制索引为55的轮廓:
area = cv2.contourArea(contours[55])
But your command for the .txt file is:
但是你对.txt文件的命令是:
text_file.write( "%s"% contours)
It should be something like:
它应该是这样的:
text_file.write( "%s" % contours[55] )
That is if you want to get coordinates of the same contour that you draw.
也就是说,如果您想获得绘制的相同轮廓的坐标。
I also generally suggest you to follow the DRY (Don't Repeat Yourself) principle in programming. If you have a constant (like 55) you should define it and use it by name throughout your code. For example:
我还建议你在编程中遵循DRY(不要重复自己)的原则。如果你有一个常量(如55),你应该定义它并在整个代码中按名称使用它。例如:
import numpy as np
import cv2
THRESHOLD = 55
CONTOUR = 55
im = cv2.imread("ceramic.bmp")
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,THRESHOLD,255,0)
_th,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
print "Number of contours detected = %d" % len(contours)
cv2.drawContours(im,contours,CONTOUR,(1,70,255))
area = cv2.contourArea(contours[CONTOUR])
print (area)
cv2.imshow("Contours",im)
np.set_printoptions(threshold=np.inf)
coords = np.array2string(contours[CONTOUR])
open("contour_%d.txt" % CONTOUR, "w").write(coords)
cv2.waitKey(0)
cv2.destroyAllWindows()`
It will write the coordinates to file contour_55.txt
. And if you want to change to another contour, you just have to change the constant CONTOUR.
它会将坐标写入文件contour_55.txt。如果你想换到另一个轮廓,你只需要改变常量轮廓。