I am trying to create a program that calculates the center of gravity of the contour from the contour data and calculates the angle θ between the center of gravity and the coordinates of the contour.
There are the coordinates x_CoG [i] and y_CoG [i] of the center of gravity of the contour contours1 [i], which are given in advance, and I am trying to find θ by the following program using them.
In order to obtain θ, the x-direction distance x_d and the y-direction distance y_d from the center of gravity to the contour coordinates are obtained, and these are included in the function for obtaining the arc tangent. The data of x_d is stored in x_d_list, and the data of arctangent is stored in theta, and the numbers of those elements should match, but they do not match and the result is as follows.
I think I made a mistake in the for statement processing, but I don't know what to improve.
Result of print (len (theta))
2932
Result of print (len (x_d_list))
1466
x_d_list = []
y_d_list = []
theta = []
for i in range (0, len (contours1)):
buf_np = contours1 [i] .flatten () # Since it is a multiple array of numpy, expand it once.
for j, elem in enumerate (buf_np): #j is a loop that outputs θ for the coordinates in contours [i]
if j% 2 == 0: Divide into # x, y
x_d = elem --x_CoG1 [i] # x_d = x-direction distance from the center of gravity to the contour coordinates
x_d_list.append (x_d)
else: else:
y_d = elem --y_CoG1 [i]
y_d_list.append (y_d)
theta.append (math.atan2 (y_d, x_d)) #thetaθ = atan2 (y, x)
print (len (theta))
print (len (x_d_list))
What I tried
Please describe here what you have tried for the problem.
Supplementary information (FW/tool version, etc.)Please provide more detailed information here.
-
Answer # 1
Related articles
- about python tutorial if statement
- python - please tell me about the processing contents of opencv background subtraction
- python - about multiple processing and loop processing in discordpy
- python 3x - about functions used in python for statement
- about processing to exclude the character string group specified from list in python
- python 3x - i'm not sure about asynchronous processing (asyncio)
- python about iterative processing with specified numbers
- about list for statement in python
- about decoding with python opencv
- python - about write loop to csv
- python 3x - processing to jump to the link destination using chrome driver in python
- python - about x-axis adjustment in matplotlib
- python: of the results obtained by the for statement, i want to make an empty array like [] into [''']
- python - about "" "of" "" select === = "" "
- python, about the fire spread step of forest fire simulation
- python - about hamiltonian neural networks
- python for statement conversion how to use the line below
- about image output of python
- about python argument and data definition
- python 3x - about downloading anaconda
- python - i don't know the cause of the error
- python - image display from the variable to which the file name is assigned
- i want to run opencv on python
- python - program error removal with opencv tracker
- python - i would like to discuss a method for detecting circles in multiple images
- about decoding with python opencv
- python - how to stream images processed by opencv in real time
- python - when i copy and divert a program whose operation has been confirmed, an error is displayed and it is not processed
- i want to start the next process when the first process starts in python
- python - opencv: continuous or simultaneous key input method
The if statement is processed incorrectly.
At this rate, the logic is theta.append for each of x_d processing and y_d processing. The list length of theta will be doubled.
If you indent theta.append so that theta.append is executed only when y_d is processed, it works well.