为什么我的图像在Opencv-Python中被绘制出来了?

时间:2017-06-09 01:35:01

标签: python image opencv matplotlib

我正在尝试拍摄图像并将其转换为灰度,为该图像添加一些高斯模糊,并检测边缘。我无法使用matplotlib的{​​{1}}显示图片。

pyplot

import cv2 import matplotlib.pyplot as plt def read_image_and_print_dims(image_path): """Reads and returns image. Helper function to examine ow an image is represented""" #reading an image image=cv2.imread(image_path) #printing out some stats and plottin print('This image is ',type(image),' with dinmesions',image.shape) plt.subplot(2,2,3) plt.imshow(image) return image image_path='fall-leaves.png' img=read_image_and_print_dims(image_path) #Make a blurred/smoothed version def gaussian_blur(img,kernel_size): """Applies a Gaussian Noise Kernel""" print ('Inside Gaussian') return cv2.GaussianBlur(img,(kernel_size,kernel_size),4) #Gray Scale Image def grayscale(img): """Applies the Grayscale transform This will return an image with only one color channel but NOTE: to see the returned image as grayscale you should call plimshow(gray, cmap='gray')""" print ('Inside gray sale') return cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray scale it greyscaled_image = grayscale(img) plt.subplot(2, 2, 1) plt.imshow(greyscaled_image, cmap='gray') # smooth it a bit with Gaussian blur kernal_size = 11 blur_gray = gaussian_blur(img, kernal_size) plt.subplot(2, 2, 2) plt.imshow(blur_gray) cv2.waitKey(0) cv2.destroyAllWindows() 上运行代码时,会生成以下消息:

Pycharm

但它没有绘制图像。

修改

我使用('This image is ', <type 'numpy.ndarray'>, ' with dinmesions', (320L, 400L, 3L)) Inside gray sale Inside Gaussian 显示它。但是,现在我遇到了另一个问题。我从plt.show获得this figure,但使用pyplot,我得到了这些:top two imagesbottom two images

这是我cv2.imshow的代码:

plt.show

这是我使用#REad Image import numpy as np import cv2 import matplotlib.pyplot as plt def read_image_and_print_dims(image_path): """Reads and returns image. Helper function to examine ow an image is represented""" #reading an image image=cv2.imread(image_path) #printing out some stats and plottin print('This image is ',type(image),' with dinmesions',image.shape) plt.subplot(2,2,1) #cv2.imshow('Original Image',image) plt.imshow(image) return image image_path='fall-leaves.png' img=read_image_and_print_dims(image_path) #Make a blurred/smoothed version def gaussian_blur(img,kernel_size): """Applies a Gaussian Noise Kernel""" print ('Inside Gaussian') return cv2.GaussianBlur(img,(kernel_size,kernel_size),4) #Gray Scale Image def grayscale(img): """Applies the Grayscale transform This will return an image with only one color channel but NOTE: to see the returned image as grayscale you should call plimshow(gray, cmap='gray')""" print ('Inside gray sale') gray_image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) return gray_image def canny(img,low_threshold,high_threshold): """Applies the Canny Transform""" return cv2.Canny(img,low_threshold,high_threshold) # gray scale it greyscaled_image = grayscale(img) plt.subplot(2, 2, 2) plt.imshow(greyscaled_image) #cv2.imshow('grey scale',greyscaled_image) # smooth it a bit with Gaussian blur kernal_size = 11 blur_gray = gaussian_blur(img, kernal_size) plt.subplot(2, 2, 3) plt.imshow(blur_gray) #cv2.imshow('gaussian ',blur_gray) #Canny image detection edges_image=canny(blur_gray,50,150) plt.subplot(2, 2, 4) plt.imshow(edges_image) plt.show() #cv2.imshow('Canny image detection',edges_image) # # cv2.waitKey(0) # cv2.destroyAllWindows() 的代码:

cv2.imshow

使用#REad Image import numpy as np import cv2 import matplotlib.pyplot as plt def read_image_and_print_dims(image_path): """Reads and returns image. Helper function to examine ow an image is represented""" #reading an image image=cv2.imread(image_path) #printing out some stats and plottin print('This image is ',type(image),' with dinmesions',image.shape) #plt.subplot(2,2,3) cv2.imshow('Original Image',image) return image image_path='fall-leaves.png' img=read_image_and_print_dims(image_path) #Make a blurred/smoothed version def gaussian_blur(img,kernel_size): """Applies a Gaussian Noise Kernel""" print ('Inside Gaussian') return cv2.GaussianBlur(img,(kernel_size,kernel_size),4) #Gray Scale Image def grayscale(img): """Applies the Grayscale transform This will return an image with only one color channel but NOTE: to see the returned image as grayscale you should call plimshow(gray, cmap='gray')""" print ('Inside gray sale') gray_image=cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) return gray_image def canny(img,low_threshold,high_threshold): """Applies the Canny Transform""" return cv2.Canny(img,low_threshold,high_threshold) # gray scale it greyscaled_image = grayscale(img) #plt.subplot(2, 2, 1) cv2.imshow('grey scale',greyscaled_image) # smooth it a bit with Gaussian blur kernal_size = 11 blur_gray = gaussian_blur(img, kernal_size) #plt.subplot(2, 2, 2) cv2.imshow('gaussian ',blur_gray) #Canny image detection edges_image=canny(blur_gray,50,150) cv2.imshow('Canny image detection',edges_image) cv2.waitKey(0) cv2.destroyAllWindows() pyplot获取不同的图像。不应该获得相同的图像吗?

1 个答案:

答案 0 :(得分:1)

创建plt.show()后,您应该使用subplots来显示要显示的情节。

Matplotlib假设RGB顺序,而OpenCV使用BGR排序。要使Matplotlib图像具有正确的颜色,您需要交换第一个和最后一个通道。您可以使用内置的OpenCV方法rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB)在显示它们之前更改它们。

plt.imshow()右侧的图像也不使用灰色色彩图,即使它们是灰色图像。您需要使用plt.imshow(blur_gray, cmap='gray')plt.imshow(edges_image, cmap='gray')来使用灰度色彩图。当只有一个频道时,cv2.imshow()始终显示灰度。您的顶级代码集使用正确的色彩映射。