2017-06-09 87 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