为什么在Opencv Python中绘制的图像不同?

为什么在Opencv Python中绘制的图像不同?,python,image,opencv,matplotlib,Python,Image,Opencv,Matplotlib,我正在尝试拍摄一张图像并将其转换为灰度,在图像中添加一些高斯模糊,并检测边缘。使用matplotlib的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 re

我正在尝试拍摄一张图像并将其转换为灰度,在图像中添加一些高斯模糊,并检测边缘。使用
matplotlib
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
这是我使用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,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()
#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
cv2
获得不同的图像。不应该获得相同的图像吗?

您应该使用
plt.show()
在创建
子绘图后获得要显示的绘图

Matplotlib采用RGB顺序,而OpenCV使用BGR顺序。要使Matplotlib图像具有正确的颜色,需要交换第一个通道和最后一个通道。您可以使用内置的OpenCV方法
rgb\u img=cv2.cvtColor(bgr\u img,cv2.COLOR\u BGR2RGB)
在显示它们之前对它们进行更改


另外,
plt.imshow()
中右侧的图像不使用灰色颜色贴图,即使它们是灰色图像。您需要使用
plt.imshow(blur\u gray,cmap='gray')
plt.imshow(edges\u image,cmap='gray')
来使用灰度彩色贴图
cv2.imshow()
只有一个频道时,始终显示灰度。顶部的一组代码使用了正确的颜色映射。

您应该使用
plt.show()
在创建
子绘图后获得要显示的绘图

Matplotlib采用RGB顺序,而OpenCV使用BGR顺序。要使Matplotlib图像具有正确的颜色,需要交换第一个通道和最后一个通道。您可以使用内置的OpenCV方法
rgb\u img=cv2.cvtColor(bgr\u img,cv2.COLOR\u BGR2RGB)
在显示它们之前对它们进行更改


另外,
plt.imshow()
中右侧的图像不使用灰色颜色贴图,即使它们是灰色图像。您需要使用
plt.imshow(blur\u gray,cmap='gray')
plt.imshow(edges\u image,cmap='gray')
来使用灰度彩色贴图
cv2.imshow()
只有一个频道时,始终显示灰度。您的顶级代码集使用了正确的颜色映射。

只需添加
plt.show()
。我认为你不需要最后两行,它们没有效果,因为你试图用pyplot而不是opencv来显示你的图像。如果你想用opencv显示它,你应该使用
cv2.imshow(“随便什么”,模糊_gray)
。它起作用了。使用cv2.imshow和pyplot-plt.show时会获得不同的图像。在使用任何绘图方法时,不需要获得相同的图像吗?当您使用
cv2.imshow
时,您会立即显示一个图像,即作为参数传递给它的图像。使用
plt.imshow
时,将图像添加到绘图中,然后可以使用
plt.show
显示整个绘图-它显示迄今为止添加的所有图像。此外,pyplot可能会添加一些坐标轴、图例等,您可以打开/关闭或调整。我的问题是,为什么不同的图像会使用pyplot和cv2进行相同的操作。我已经用图编辑了最后的问题。@HimalAcharya这可能是因为OpenCV默认使用BGR,而PyPlot使用RGB。只需添加
plt.show()
。我认为你不需要最后两行,它们没有效果,因为你试图用pyplot而不是opencv来显示你的图像。如果你想用opencv显示它,你应该使用
cv2.imshow(“随便什么”,模糊_gray)
。它起作用了。使用cv2.imshow和pyplot-plt.show时会获得不同的图像。在使用任何绘图方法时,不需要获得相同的图像吗?当您使用
cv2.imshow
时,您会立即显示一个图像,即作为参数传递给它的图像。使用
plt.imshow
时,将图像添加到绘图中,然后可以使用
plt.show
显示整个绘图-它显示迄今为止添加的所有图像。此外,pyplot可能会添加一些坐标轴、图例等,您可以打开/关闭或调整。我的问题是,为什么不同的图像会使用pyplot和cv2进行相同的操作。我已经用图编辑了问题的结尾。@HimalAcharya这可能是因为OpenCV默认使用BGR,而PyPlot使用RGB。