2017-06-09 42 views
0

我想拍摄一张图像并将其转换为灰度图像,为该图像添加一些高斯模糊,并检测边缘。我在使用matplotlibpyplot显示图像时遇到问题。为什么我的图像不同在Opencv-Python中绘制?

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 figurepyplot,但使用cv2.imshow,我得到了这些:top two imagesbottom two images

这是我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() 

使用pyplotcv2获得不同的图像。不应该得到相同的图像?

+0

只需添加'plt.show()'。我不认为你需要最后两行,它们没有效果,因为你试图用pyplot显示你的图像,而不是opencv。如果你想用opencv显示它,你应该使用'cv2.imshow(“无论”,blur_gray)'。 – Headcrab

+0

它工作。使用cv2.imshow和pyplot -plt.show获得不同的图像。在使用任何绘图方法时,是否需要获得相同的图像? –

+0

使用'cv2.imshow'时,您立即显示一个图像,即您传递给它的图像作为参数。当您使用'plt.imshow'时,您将图像添加到情节,然后您可以使用'plt.show'显示整个情节 - 它显示您迄今为止添加的所有图像。此外pyplot可能会添加一些坐标轴,图例等,您可以打开/关闭或调整。 – Headcrab

回答

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()将始终显示灰度。您最上面的一组代码使用正确的颜色映射。

-1

试试行后加入

waitKey(1) 

plt.imshow(image) 

它应该做的伎俩

http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_image_display/py_image_display.html

+0

仍然同样的问题..没有数字创建。 –

+0

这根本不回答问题。 'plt'是'matplotlib'的'pyplot',而不是'OpenCV'。它不需要'waitKey()'保持图像窗口打开。 –

+0

得到了亚...我记得用普通的OpenCV运行到这个问题 - 所以我认为这可能是一个简单的修复... – JxAxMxIxN