我搜索了片刻,但只找到了这个Answer。所以我在这里提出一个问题。我想按轨迹栏缩放图像,我的代码如下所示:

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

只是简单的告诉我很长的错误信息:
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

有人遇到过这个错误吗?

最佳答案

待办事项:

将您的代码更改为此:

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

原因:

我发现您将rows, cols = img.shape[:2]放入了while循环中。

这不是您打算执行的操作: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

关于python - opencv如何通过跟踪栏缩放图像?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47446952/

10-12 22:15