0

I have search for a moment but only find this Answer similar. So I make a question here. I want to scale a image by track bar, my code as below show:

import cv2
import numpy as np

img = cv2.imread('../data/logo.png')

cv2.namedWindow('scaleFactor')
cv2.namedWindow('image')

def nothing(x):
    print 'x--->', x

cv2.createTrackbar('scale','scaleFactor', 0 , 5, nothing)


while(1):
    rows, cols = img.shape[:2]
    cv2.imshow('image', img)
    scaleFactor = cv2.getTrackbarPos('scale','scaleFactor') or 1
    scaleFactor = scaleFactor if scaleFactor > 0 else 1
    img = cv2.resize(img, (int(rows * scaleFactor), int(cols * scaleFactor)), interpolation=cv2.INTER_LINEAR)


    k =cv2.waitKey(10)
    if k == 27:
        break
    print 'scaleFactor -->', scaleFactor

cv2.destroyAllWindows()

Just simple show me a very long error information:

scaleFactor --> 2
python(26953,0x7fffa5a033c0) malloc: *** mach_vm_map(size=4872572928) failed (error code=4)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

Is there anyone have meet this error before?

Frank AK
  • 1,705
  • 15
  • 28

1 Answers1

1

Todo:

change you code to this:

rows, cols = img.shape[:2]
while(1):
    #rows, cols = img.shape[:2]
    pass 

Reason:

I found that you put rows, cols = img.shape[:2] in the while loop.

This is not what you intend to do: It will scale by the scalefactor in every loop(not every time you change the trackbar value)..

...
scaleFactor --> 2
8589934592
scaleFactor --> 2
17179869184
scaleFactor --> 2
34359738368
scaleFactor --> 2
68719476736
scaleFactor --> 2
137438953472
Kinght 金
  • 17,681
  • 4
  • 60
  • 74