13

是否可以使用opencv libraray在同一窗口上显示黑白和彩色图像?我怎样才能在同一个窗口上同时拥有这两个图像?

4

4 回答 4

35

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

下面是我得到的输出:

在此处输入图像描述

于 2012-06-17T06:00:16.803 回答
8

是的,这是一个例子,评论中的解释:

在此处输入图像描述

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中,但很容易转换成任何东西..

于 2012-06-17T03:00:32.903 回答
2

现代写作对代码的小改进

连接

代替

堆栈

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

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()
于 2017-09-21T10:29:23.997 回答
-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.
于 2020-03-31T09:55:58.487 回答