首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python OpenCV:按下某个键时刷新图像

Python OpenCV:按下某个键时刷新图像
EN

Stack Overflow用户
提问于 2014-04-04 14:45:47
回答 1查看 8.1K关注 0票数 0

我已经使用python opencv2模块开发了一个程序。

每当按下某个键时,该程序都会上载图像。

下面是伪代码:

代码语言:javascript
运行
复制
 import cv2
    from msvcrt import getch


    while True:

    k = getch()

    if k == 'w':

          img = cv2.imread(filedir + orange.jpg)
          cv2.namedWindow
          cv2.imshow(img)
          waitkey()
          destroyAllWindows

    elif k == 'a'

          img = cv2.imread(filedir + banana.jpg)
          cv2.namedWindow
          cv2.imshow(img)
          waitkey()
          destroyAllWindows

这是不言而喻的,因为当按下'w‘时,我正在尝试上传一个'orange.jpg’文件。

我真正的问题是:如何以这样的方式设计程序,用户不必按两次键,按一次键关闭图像文件,然后按其他键打开文件。这导致设计失败,因为我希望处理过程只需一次击键即可完成。即使用户按下'w‘并且'orange.jpg’已经上传,也应该刷新该文件,而不是关闭该文件。类似地,当用户按下'a‘时,'orange.jpg’是打开的,那么'orange.jpg‘文件应该会关闭,而banana.jpg应该会自动打开,这应该是一次性操作。到目前为止,我必须按两次键才能执行此任务。

我已经实现了代码,所以即使有人建议我去pygtk并使用它按下键上传图像,我也没有问题。我唯一的目标是在没有太多用户干预的情况下销毁上传的图像,即处理过程应该看起来是自主的。

正如beark所说,在程序中使用getch()意味着焦点将始终在控制台上。我对此并不满意,只想通过按键上传图像,但console阻碍了这一操作。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2014-04-24 17:10:27

我已经解决了这个问题:

代码语言:javascript
运行
复制
import sys
import cv2
import os

def main():

    File_Lst =[]

    plat = sys.platform
    #print plat

    if plat == 'win32': #for windows operating system

        File_dir = "C:\\Users\\user\\Desktop\\fruit\\"


    elif plat == 'linux2': # for linux

        File_dir = "/host/Users/user/Desktop/fruit/"

    for file in os.listdir(File_dir):

        File_Lst.append(file)

    print File_Lst


    welcome_index = File_Lst.index('welcome.jpg')           
    welcome_str = File_Lst[welcome_index]

    orange_index = File_Lst.index('orange.jpg')         
    orange_str = File_Lst[orange_index]


    apple_index = File_Lst.index('apple.jpg')           
    apple_str = File_Lst[apple_index]

    banana_index = File_Lst.index('banana.jpg')         
    banana_str = File_Lst[banana_index]

    doughnuts_index = File_Lst.index('doughnuts.jpg')           
    doughnuts_str = File_Lst[doughnuts_index]

    img = cv2.imread(File_dir + welcome_str )
    cv2.destroyAllWindows()         
    cv2.imshow("Press KEYS to know which food is good or bad", img)

    while True:

        k = cv2.waitKey(0)

        if k == ord('w'): # wait for 'w' key to upload orange nutrition information

            img = cv2.imread(File_dir + orange_str) 
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Orange Nutritional Information", img)

        elif k == ord('a'): # wait for 'w' key to upload apple nutrition information

            img = cv2.imread(File_dir + apple_str)  
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Apple Nutritional Information", img)

        elif k == ord('s'): # wait for 'w' key to upload apple nutrition information

            img = cv2.imread(File_dir + banana_str) 
            newx,newy = img.shape[1]/2,img.shape[0]/2 #new size (w,h)
            img = cv2.resize(img,(newx,newy))
            cv2.destroyAllWindows()     
            cv2.imshow("Banana Nutritional Information", img)


        elif k == 32:

            break
            cv2.destroyAllWindows()

        else:

            img = cv2.imread(File_dir + doughnuts_str)
            cv2.destroyAllWindows()
            cv2.imshow("Bad, Have good eating habits CHUMP", img)
            continue    







main()

我正在破坏每个图像显示的窗口,这样,每个按键笔画对应的新图像上传的一致性得到维护

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22855475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档