0

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

Ismael Padilla
  • 5,246
  • 4
  • 23
  • 35
Malte
  • 111
  • 10
  • One possible problem is which version python is used? Python x86 or x64? – heLomaN Sep 14 '20 at 19:20
  • Like @heLomaN mentioned that could be a problem, aswell make sure you are using the latest version of openCV – tamerjar Apr 25 '21 at 18:04
  • Can you try looking at your available memory using `psutil.virtual_memory()`? When you "... tried to run in on my image...", do you mean you are using a virtual machine? – Florian Fasmeyer Jul 15 '21 at 10:57

1 Answers1

1

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