尝试在更大的图像上运行代码时,OpenCV + Python出现此错误

时间:2017-07-14 12:40:33

标签: python opencv numpy image-processing

通过一组5472 x 3648维度(4.4mb)的大图像运行代码时出现此错误。当我的图像大约为1437 x 1243维度时,代码工作正常,我可以浏览所有图像并保存。

注意:你可以看到更大的图像,我仍然设法运行第一个文件并将其保存到1771,它只显示错误并在1772年中途停止运行。

我是否可以使用此算法处理我的图像而无需调整图像尺寸?我试图将所有图像与文件1770对齐。我怀疑它可能是一个内存/ RAM enter code here问题,或者我的算法可能有问题,我可以进一步简化它。关于这个问题的任何建议或建议????

PC规格:使用8GB RAM

错误讯息:

RESTART: C:\Users\310293649\AppData\Local\Programs\Python\Python36-32\Picture Alignment.py 
1771
1772
Traceback (most recent call last):
  File "C:\Users\310293649\AppData\Local\Programs\Python\Python36-32\Picture Alignment.py", line 65, in <module>
    alignment()
  File "C:\Users\310293649\AppData\Local\Programs\Python\Python36-32\Picture Alignment.py", line 46, in alignment
    (cc, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria)
cv2.error: D:\Build\OpenCV\opencv-3.2.0\modules\core\src\matrix.cpp:433: error: (-215) u != 0 in function cv::Mat::create

以下是我的代码:

import os, sys
import cv2
from PIL import Image
import numpy as np

path = "C:\\Users\\310293649\\Desktop\\BeforeAlignment\\"
path1 = "C:\\Users\\310293649\\Desktop\\AfterAlignment\\"

def alignment():
    for i in range(1771,1800):
        # Read the images to be aligned
        im1 =  cv2.imread(path + 'IMG_1770.jpg')
        im2 =  cv2.imread(path + 'IMG_%d.jpg' %(i))
        print(i)

        # Convert images to grayscale
        im1_gray = cv2.cvtColor(im1,cv2.COLOR_BGR2GRAY)
        im2_gray = cv2.cvtColor(im2,cv2.COLOR_BGR2GRAY)

        # Find size of image1
        sz = im1.shape

        # Define the motion model
        warp_mode = cv2.MOTION_TRANSLATION

        # Define 2x3 or 3x3 matrices and initialize the matrix to identity
        if warp_mode == cv2.MOTION_HOMOGRAPHY :
            warp_matrix = np.eye(3, 3, dtype=np.float32)
        else :
            warp_matrix = np.eye(2, 3, dtype=np.float32)

        # Specify the number of iterations.
        number_of_iterations = 10000;

        # Specify the threshold of the increment
        # in the correlation coefficient between two iterations
        termination_eps = 1e-10;

        # Define termination criteria
        criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations,  termination_eps)

        # Run the ECC algorithm. The results are stored in warp_matrix.
        (cc, warp_matrix) = cv2.findTransformECC(im1_gray, im2_gray, warp_matrix, warp_mode, criteria)

        if warp_mode == cv2.MOTION_HOMOGRAPHY :
            # Use warpPerspective for Homography 
            im2_aligned = cv2.warpPerspective (im2, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP)
        else :
            # Use warpAffine for Translation, Euclidean and Affine
            im2_aligned = cv2.warpAffine(im2, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR + cv2.WARP_INVERSE_MAP);

        cv2.imwrite(path1 + "aligned_IMG_%d.jpg" % (i), im2_aligned)
        cv2.waitKey(0)

alignment()

0 个答案:

没有答案