I am trying to calculate the center of gravity by extracting the contours of multiple figures using python and opencv.
The photo in symmetry with the program is as follows.
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread ("test.jpg")
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold (img, 127, 255, 0)
img1 = np.array (thresh)
img2 = cv2.cvtColor (img1, cv2.COLOR_BGR2GRAY)
contours, _ = cv2.findContours (img2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
M = cv2.moments (c)
x = int (M ["m10"]/M ["m00"])
y = int (M ["m01"]/M ["m00"])
plt.plot (x, y, marker ='.')
plt.imshow (img2, cmap = "gray")
plt.colorbar ()
plt.show ()
The error appears on the 6th line and is displayed as follows
gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV (4.2.0) C: \ projects \ opencv-python \ opencv \ modules \ imgproc \ src \ color.cpp: 182: error: (-215: Assertion failed)! _src.empty () in function 'cv :: cvtColor'
It's just the beginning, so I can't tell even the rudimentary mistakes. I would be happy if you could tell me in detail. Also, if you have any other code mistakes, please let me know.
-
Answer # 1
Related articles
- download multiple videos using python and youtube-dl
- i don't understand the exercises using python trigonometric functions
- python 3x - processing to jump to the link destination using chrome driver in python
- parallel processing using python multiprocessingpool and multiprocessingqueue does not work well
- python - how to calculate the winning percentage with sql
- python - about multiple processing and loop processing in discordpy
- about external libraries when using multiple versions of python
- python - i want to separate by a specific word using the split function
- python - image recognition using cnn keras multiple inputs
- about batch change of file name using python
- python - read multiple audio files (wav files)
- (python) i want to convert multiple json files with different columns into one csv format
- python 3x - i want to get the nth array with an argument using python3 argparse
- python 3x - how to rename a folder created using jupyternotebook
- python - error in image binarization using cv2adaptivethreshold function
- please explain the function using the python dictionary
- i want to adjust the execution result using the while statement in python as expected
- python - i want to put the image file path in a variable and open it using that variable
- python 3x - [python] i want to compare multiple dictionaries and remove duplicate dictionaries
- multiple classification score calculation errors, python, svm
- python - i want to divide the read image into pixels and label them
- python - i don't know the cause of the error
- python - how to save after image conversion (emboss conversion) with opencv
- i want to run opencv on python
- python - when i copy and divert a program whose operation has been confirmed, an error is displayed and it is not processed
- python - what you do not understand by calculating the precision and recall
- python - how to stream images processed by opencv in real time
- about decoding with python opencv
- python - i would like to discuss a method for detecting circles in multiple images
- python - i get an error when multiplying a matrix numpy
The image file cannot be read. Make sure it is there.