Malte
Malte

Reputation: 111

Python OpenCV throws cv::OutOfMemoryError

I am currently trying to use some pattern matching using python and opencv. I have copied the example code from the opencv website and tried to run it on my image. However when doing so I get an out of memory error due to the system not being able to allocate about 700mb, however I have more than enough free ram (32gb) and everything is running in 64-bit. I have looked at various posts with similar issues but could not find a solution that works for me.

Python code

import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('***.jpg',0)
img2 = img.copy()
template = cv2.imread('***.png',0)
w, h = template.shape[::-1]

# All the 6 methods for comparison in a list
img = img2.copy()
method = eval('cv2.TM_CCOEFF')

# Apply template Matching
res = cv2.matchTemplate(img,template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
    top_left = min_loc
else:
    top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img,top_left, bottom_right, 255, 2)

plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(meth)

plt.show()

Before anyone mentions it, yes there are valid filenames where the *s are.

Image im trying to use as a base has a size of about 11mb

Upvotes: 0

Views: 4221

Answers (1)

Danial Khilji
Danial Khilji

Reputation: 51

I recently had a similar issue. Computers use some amount of ROM to store huge size variables which are out of range of RAM size or capability.

Check the link below to increase your ROM memory size which can be used as a RAM memory to store huge size variables.

https://stackoverflow.com/a/58686879/13460812

Upvotes: 1

Related Questions