无法获取输出图像

时间:2019-04-03 14:33:38

标签: python opencv

我是python和openCV的新手,我一直在尝试一些我在网上找到的代码。所以在此先感谢您的帮助 尽管即时通讯使用了opencv中定义的imshow()函数,即时通讯无法显示图像

from __future__ import division
from __future__ import print_function

import random
import numpy as np
import cv2


def main():
    "put img into target img of size imgSize, transpose for TF and normalize gray-values"

    img=cv2.imread("C:\\Users\\bnsid\\Desktop\\a01-003-00-02.png", cv2.IMREAD_GRAYSCALE)
    imgSize=(128,32)
    dataAugmentation = True
    if img is None:
        img = np.zeros([imgSize[1], imgSize[0]])

    # dataaugmentation
    if dataAugmentation:
        stretch = (random.random() - 0.5) # -0.5 .. +0.5
        wStretched = max(int(img.shape[1] * (1 + stretch)), 1) # random width, but at least 1
        img = cv2.resize(img, (wStretched, img.shape[0])) # stretch horizontally by factor 0.5 .. 1.5

    # create target image and copy sample image into it
    (wt, ht) = imgSize
    (h, w) = img.shape
    fx = w / wt
    fy = h / ht
    f = max(fx, fy)
    newSize = (max(min(wt, int(w / f)), 1), max(min(ht, int(h / f)), 1)) # scale according to f (result at least 1 and at most wt or ht)
    img = cv2.resize(img, newSize)
    target = np.ones([ht, wt]) * 255
    target[0:newSize[1], 0:newSize[0]] = img

    # transpose for TF
    img = cv2.transpose(target)

    # normalize
    (m, s) = cv2.meanStdDev(img)
    m = m[0][0]
    s = s[0][0]
    img = img - m
    img = img / s if s>0 else img
    cv2.imshow('Greyscale_Stretched', img)
    k= cv2.waitKey(0) & 0xFF
    if k == 27:  # wait for ESC key to exit
        cv2.destroyAllWindows()
    elif k == ord('s'):  # wait for 's' key to save and exit
        cv2.imwrite('grey.png', img)
        cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

只需测试您的代码即可。您需要在某个地方调用RelativeLayout函数。由于尚未执行此操作,因此不会执行该功能。

只需在代码末尾添加main(),一切正常。

main()

您在此处声明和定义的def main(): #your code here print("placeholder") main() 函数与C ++中的main()入口函数不同。如果您想要类似的行为,请使用以下方法:

main()