【问题标题】:Why my image is different being plotted in Opencv-Python? [duplicate]为什么在 Opencv-Python 中绘制的图像不同? [复制]
【发布时间】:2017-11-10 21:23:09
【问题描述】:

我正在尝试拍摄图像并将其转换为灰度,为该图像添加一些高斯模糊,并检测边缘。我无法使用matplotlibpyplot 显示图像。

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 让它显示。但是,现在我遇到了不同的问题。我从pyplot 获得了this figure,但是使用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 获得不同的图像。不应该得到相同的图像吗?

【问题讨论】:

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

标签: python image opencv matplotlib


【解决方案1】:

您应该在创建subplots 后使用plt.show() 来显示绘图。

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() 只有一个通道时总是显示灰度。您的顶级代码集使用正确的颜色图。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-23
    • 2019-02-12
    • 2014-06-01
    相关资源
    最近更新 更多