2017-06-09 3 views
0

Ich versuche ein Bild aufzunehmen und es in Graustufen zu konvertieren, indem ich dem Bild eine Gaußsche Unschärfe hinzufüge und die Kanten erkenne. Ich habe Probleme beim Anzeigen des Bildes mit matplotlib 's pyplot.Warum wird mein Bild in Opencv-Python anders geplottet?

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

Während obige Code in Pycharm läuft erzeugt er folgende Meldung:

('This image is ', <type 'numpy.ndarray'>, ' with dinmesions', (320L, 400L, 3L)) 
Inside gray sale 
Inside Gaussian 

Aber es das Bild nicht zeichnen.

EDIT

habe ich es plt.show mit angezeigt werden soll. Jetzt habe ich ein anderes Problem. Ich erhielt this figure von pyplot, aber unter Verwendung von cv2.imshow, ich habe diese: top two images, bottom two images

Dies ist mein Code für 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() 

Und dies ist mein Code cv2.imshow für die Anwendung:

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

Verschiedene Bilder werden unter Verwendung von pyplot und cv2 erhalten. Sollte nicht das gleiche Bild erhalten werden?

+0

Fügen Sie einfach 'plt.show()'. Und ich glaube nicht, dass du die letzten zwei Zeilen brauchst, sie haben keine Wirkung, weil du versuchst, deine Bilder mit pyplot anzuzeigen, nicht mit opencv. Wenn Sie es mit opencv anzeigen möchten, sollten Sie 'cv2.imshow (" Whatever ", blur_gray)' verwenden. – Headcrab

+0

Es hat funktioniert. Unterschiedliches Bild wird erhalten, während cv2.imshow und pyplot-plt.show verwendet werden. Sollte das gleiche Bild nicht erhalten werden, während irgendein Ansatz für die Handlung verwendet wird? –

+0

Wenn Sie 'cv2.imshow' verwenden, zeigen Sie sofort ein Bild an, das Sie als Parameter übergeben haben. Wenn Sie 'plt.imshow' verwenden, fügen Sie dem Plot ein Bild hinzu, und später können Sie das gesamte Plot mit' plt.show' anzeigen - es zeigt alle Bilder an, die Sie bisher hinzugefügt haben.Außerdem kann pyplot einige Koordinatenachsen, Legenden usw. hinzufügen, Sie können diese ein-/ausschalten oder optimieren. – Headcrab

Antwort

1

Sie sollten plt.show() verwenden, um die Handlung zu bekommen angezeigt, nachdem Sie erstellen tun sollten die subplots.

Matplotlib geht davon aus RGB Ordnung während OpenCV verwendet BGR Bestellung. Um die Matplotlib-Bilder in die richtige Farbe zu bringen, müssen Sie den ersten und letzten Kanal wechseln. Sie können die integrierte OpenCV-Methode rgb_img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) verwenden, um sie vor der Anzeige zu ändern.

Auch die Bilder auf der rechten Seite in plt.imshow() nicht verwenden einen grauen colormap obwohl sie graue Bilder sind. Sie müssen plt.imshow(blur_gray, cmap='gray') und plt.imshow(edges_image, cmap='gray') verwenden, um die Graustufenfarbkarte zu verwenden. cv2.imshow() zeigt immer Graustufen an, wenn nur ein Kanal vorhanden ist. Ihr oberster Satz Code verwendet die richtigen Farbzuordnungen.

-1

Try

waitKey(1) 

nach der Linie

plt.imshow(image) 

Hinzufügen Es den Trick

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

+0

Immer noch das gleiche Problem ..Keine Figur erstellt. –

+0

Dies beantwortet die Frage überhaupt nicht. 'plt' ist' matplotlib's 'pyplot' und nicht' OpenCV'. Es ist kein 'waitKey()' erforderlich, um das Bildfenster offen zu halten. –

+0

Verzeihung ... Ich erinnere mich, dass ich mit OpenCV in dieses Problem gerannt bin - also dachte ich, es könnte eine einfache Lösung sein ... – JxAxMxIxN

Verwandte Themen