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?
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
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? –
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