How do I read a CSV file and use the data with cv.undistort()

Hello i’m trying to calibrate opencv for measuring distance between two aruco markers. I have this code:

mtx = open("example_images_dronecalib_mtx_webcam.csv", "r")
dist = open("example_images_dronecalib_dist_webcam.csv", "r")
mtx_data = mtx.read()
dist_data = dist.read()
frame = cv2.imread(folder_drone + "\\testovaci_obrazek.jpg")
img_undist = cv2.undistort(frame, mtx_data, dist_data, None)

When i try to execute the code it shows me this error:

Traceback (most recent call last):
  File "C:\Users\Stefan\Desktop\BAKALARKA\pythonProject3\check_calibration.py", line 29, in <module>
    img_undist = cv2.undistort(image, mtx_data, dist_data, None)
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'undistort'
> Overload resolution failed:
>  - cameraMatrix is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'cameraMatrix'

This is my mtx_data:

8.299671855820477049e+02 0.000000000000000000e+00 1.341478981218368972e+03
0.000000000000000000e+00 8.299671855820477049e+02 1.332307420024589192e+03
0.000000000000000000e+00 0.000000000000000000e+00 1.000000000000000000e+00

The shape of the image is (1936, 2592, 3). The datatype of the loaded image is <class ‘numpy.ndarray’>. The datatype of the pixel is <class ‘numpy.uint8’>

I’m using opencv-contrib-python version 4.5.4.58, Python 3.7

Can you tell me what am I doing wrong? Thanks for any advice.

please check type(mtx_data) ,
it’s probably a string, not a numpy array
(maybe you want to use some numpy func to read it, not open() ?)

(same for dist_data)

you’re just reading the file, and you get a bytes object back.

python comes with a csv module to read csv files. that’s still not enough because then you just have a bunch of strings again. do you even know what’s in those csv files, in terms of the format?

please find more material teaching you basic python, then use the tutorials on https://docs.opencv.org/

Hello, you are right. mtx_data is string. I have fixed the code, now i import the file from csv to numpy array.

Hello, i have fixed the code. Now i read the csv files as numpy. Thank you for help.

you have? that’s great! now show us that.

I have added these lines:

from numpy import genfromtxt
mtx_data = genfromtxt('example_images_dronecalib_mtx_webcam.csv', delimiter=' ')
dist_data = genfromtxt('example_images_dronecalib_dist_webcam.csv', delimiter=' ')

My is my final code:

from numpy import genfromtxt
mtx_data = genfromtxt('example_images_dronecalib_mtx_webcam.csv', delimiter=' ')
dist_data = genfromtxt('example_images_dronecalib_dist_webcam.csv', delimiter=' ')
frame = cv2.imread(folder_drone + "\\testovaci_obrazek.jpg")
img_undist = cv2.undistort(frame, mtx_data, dist_data, None)

I fixed your post. use the code button, not the quote button. code needs specific formatting to remain true.

do this: print(mtx_data) and show what is printed.

ok, i will be doing so. Thank you for help :slight_smile: