怎样才能使未失真代码不适用于几张尺寸相同的棋盘图像? [英] What could make undistortion code not work for several chessboard images having the same dimensions?

查看:79
本文介绍了怎样才能使未失真代码不适用于几张尺寸相同的棋盘图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是OpenCV-Python的初学者.我想知道什么可以使我的未失真代码适用于此棋盘图片

I am beginner in OpenCV-Python. I would like to know what could make my undistortion code work for this chessboard picture

不适用于此人

知道棋盘的尺寸也相同.

knowing that the too chessboards have the same dimension.

我正在谈论的代码如下

import cv2
import numpy as np
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
cbrow = 7
cbcol = 9

objp = np.zeros((cbrow * cbcol, 3), np.float32)
objp[:, :2] = np.mgrid[0:cbcol, 0:cbrow].T.reshape(-1,2)

objpoints = []  
imgpoints = []

CHECKERBOARD =  (7,9)

img = cv2.imread("ChessUOledVGA.jpg")

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
print(corners)

if ret == True:
        objpoints.append(objp)
        corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
        imgpoints.append(corners2)
        ## Draw and display the corners
        img = cv2.drawChessboardCorners(img, (cbcol, cbrow), corners2, ret)
        cv2.imshow('img', img)
        cv2.waitKey(0) 

 ret, mtx, dist , rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
        print('mtx', mtx)
        print('dist',dist)
        img = cv2.imread('ChessUOledVGA.jpg', 1)
        h, w = img.shape[:2]  # get the size and the shape of the image h,w
        newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
        print('newcameramtx', newcameramtx)
        dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
        cv2.imwrite('result.jpg', dst)

请有人启发我.谢谢

推荐答案

您的棋盘格尺寸必须为(每行无角,每列无角)->(9,7).并且,由于问题图像的四个角处存在阴影,因此在进入查找角点"功能之前,应先对图像进行阈值处理.试试这个,它有效.

Your checkboard dimensions must be (no of corners per row, no of corners per col) ->(9,7). And, you should threshold your image before passing into the find corners function as there is shades in four corners of the problematic image. Try this, it works.

import cv2
import numpy as np
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
cbrow = 7
cbcol = 9

objp = np.zeros((cbrow * cbcol, 3), np.float32)
objp[:, :2] = np.mgrid[0:cbcol, 0:cbrow].T.reshape(-1,2)

objpoints = []  
imgpoints = []

CHECKERBOARD =  (9,7)

img = cv2.imread("ChessUOledVGA.jpg")

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, out = cv2.threshold(gray, 40, 255, cv2.THRESH_BINARY_INV)

ret, corners = cv2.findChessboardCorners(out, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH+cv2.CALIB_CB_FAST_CHECK+cv2.CALIB_CB_NORMALIZE_IMAGE)
print(corners, ret)

if ret == True:
    objpoints.append(objp)
    corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
    imgpoints.append(corners2)
    ## Draw and display the corners
    img = cv2.drawChessboardCorners(img, (cbcol, cbrow), corners2, ret)
    cv2.imshow('img', img)
    cv2.waitKey(0) 

if 0:
    ret, mtx, dist , rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
    print('mtx', mtx)
    print('dist',dist)
    img = cv2.imread('ChessUOledVGA.jpg', 1)
    h, w = img.shape[:2]  # get the size and the shape of the image h,w
    newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 1, (w, h))
    print('newcameramtx', newcameramtx)
    dst = cv2.undistort(img, mtx, dist, None, newcameramtx)
    cv2.imwrite('result.jpg', dst)

这篇关于怎样才能使未失真代码不适用于几张尺寸相同的棋盘图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆