【问题标题】:Is it possible to have black and white and color image on same window by using opencv?是否可以使用opencv在同一窗口上显示黑白和彩色图像?
【发布时间】:2012-06-19 13:03:34
【问题描述】:

是否可以使用 opencv libraray 在同一窗口上显示黑白和彩色图像?如何在同一个窗口中同时显示这两个图像?

【问题讨论】:

    标签: image opencv colors window grayscale


    【解决方案1】:
    import cv2
    img = cv2.imread("image.jpg" , cv2.IMREAD_GRAYSCALE)
    cv2.imshow("my image",img)
    cv2.waitkey(0)
    cv2.destroyAllWindow
    
    
    #The image file should be in the application folder.
    #The output file will be 'my image' name.
    #The bottom line is to free up memory.
    

    【讨论】:

      【解决方案2】:

      现代写作对代码的小改进

      连接

      而不是

      hstack

      已停产(也可以使用堆栈)

      import cv2
      import numpy as np
      
      im = cv2.imread('kick.jpg')
      img = cv2.imread('kick.jpg',0)
      
      # Convert grayscale image to 3-channel image,so that they can be stacked together    
      imgc = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
      both = np.concatenate((im,imgc), axis=1)   #1 : horz, 0 : Vert. 
      
      cv2.imshow('imgc',both)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      

      【讨论】:

      • 有没有人分析过cv2.hconcat()np.concatenate()?我使用前者,因为我更喜欢在我的代码中尽可能坚持使用 opencv 而不是 numpy,但我想我应该运行这些数字。
      【解决方案3】:

      fraxel 的回答解决了旧 cv 界面的问题。我想使用 cv2 接口来展示它,只是为了了解在新的 cv2 模块中这有多容易。 (可能对未来的访客有帮助)。下面是代码:

      import cv2
      import numpy as np
      
      im = cv2.imread('kick.jpg')
      img = cv2.imread('kick.jpg',0)
      
      # Convert grayscale image to 3-channel image,so that they can be stacked together    
      imgc = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
      both = np.hstack((im,imgc))
      
      cv2.imshow('imgc',both)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
      

      下面是我得到的输出:

      【讨论】:

      • @abid-rahman-k 可以对视频进行同样的操作吗?没有把一个变成灰度?
      【解决方案4】:

      是的,这是一个例子,在 cmets 中的解释:

      import cv
      #open color and b/w images
      im = cv.LoadImageM('1_tree_small.jpg')
      im2 = cv.LoadImageM('1_tree_small.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
      #set up our output and b/w in rgb space arrays:
      bw = cv.CreateImage((im.width,im.height), cv.IPL_DEPTH_8U, 3)
      new = cv.CreateImage((im.width*2,im.height), cv.IPL_DEPTH_8U, 3)
      #create a b/w image in rgb space
      cv.Merge(im2, im2, im2, None, bw)
      #set up and add the color image to the left half of our output image
      cv.SetImageROI(new, (0,0,im.width,im.height))
      cv.Add(new, im, new)
      #set up and add the b/w image to the right half of output image
      cv.SetImageROI(new, (im.width,0,im.width,im.height))
      cv.Add(new, bw, new)
      cv.ResetImageROI(new)
      cv.ShowImage('double', new)
      cv.SaveImage('double.jpg', new)
      cv.WaitKey(0)
      

      它在 python 中,但很容易转换成任何东西..

      【讨论】:

      • 非常感谢飞梭。这就是我要找的。再次感谢。
      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-19
      相关资源
      最近更新 更多