Is it possible to have black and white and color image on same window by using opencv?

前端 未结 4 2019
南旧
南旧 2020-12-05 08:35

Is it possible to have black-and-white and color image on same window by using opencv libraray? How can I have both of these images on same window?

相关标签:
4条回答
  • 2020-12-05 08:46

    fraxel's answer has solved the problem with old cv interface. I would like to show it using cv2 interface, just to understand how this easy in new cv2 module. (May be it would be helpful for future visitors). Below is the code:

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

    And below is the output I got:

    enter image description here

    0 讨论(0)
  • 2020-12-05 08:48
    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.
    
    0 讨论(0)
  • 2020-12-05 08:56

    Small improvement to the code with modern writing

    concatenate

    instead of

    hstack

    that is discontinued (stack can also be used)

    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()
    
    0 讨论(0)
  • 2020-12-05 08:59

    Yes it is, here is an example, expaination in the comments:

    enter image description here

    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)
    

    Its in python, but easy to convert to whatever..

    0 讨论(0)
提交回复
热议问题