OpenCV+Python getting this error when trying to run the code on bigger images

坚强是说给别人听的谎言 提交于 2021-01-28 10:04:48

问题


I am getting this error when running my code through a set of big images of 5472 x 3648 dimension(4.4mb) . The code is working fine when my images were around 1437 x 1243 dimension, I can run through all images and save it.

Notice: You can see with bigger images, I still managed to run the first file and save it to 1771, it only shown error and stop the run halfway through 1772.

Is there anyway which I can process my images using this algorithm without needing to resize the images dimension? I was trying to aligned all my images with reference to file 1770. I suspect it might be a memory/RAMenter code here issue or there might be something wrong with my algorithm which I could further simplify it. Any suggestion or advice on this issue????

PC spec: using 8GB RAM

Error message:

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

Below is my code :

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()

来源:https://stackoverflow.com/questions/45103249/opencvpython-getting-this-error-when-trying-to-run-the-code-on-bigger-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!