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

标签 python image opencv matplotlib

这个问题在这里已经有了答案:





OpenCV giving wrong color to colored images on loading

(6 个回答)


4年前关闭。




我正在尝试拍摄图像并将其转换为灰度,为该图像添加一些高斯模糊,并检测边缘。我无法使用 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
但它不会绘制图像。
编辑
我使用 plt.show 让它显示.但是,现在我遇到了不同的问题。我获得了this figure来自 pyplot , 但使用 cv2.imshow ,我得到了这些:top two images , bottom 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()
使用 pyplot 获得不同的图像和 cv2 .不应该获得相同的图像吗?

最佳答案

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

Matplotlib 采用 RGB 顺序,而 OpenCV 使用 BGR 顺序。要使 Matplotlib 图像具有正确的颜色,您需要交换第一个和最后一个 channel 。您可以使用内置的 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()只有一个 channel 时总是显示灰度。您的顶级代码集使用正确的颜色图。

关于python - 为什么在 Opencv-Python 中绘制的图像不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44447957/

相关文章:

python - 我想替换字典的值

jquery - 淡入正文背景图像

opencv - 两个 3D 点之间的 3D 旋转矩阵

python - 在标绘线上打印字符串(模拟等高线图标签)

python - 列出字典值的理解

python - 如何解码字符串以与 Google 语言检测 API 一起使用?

image - 将内容从主机操作系统复制到 Docker 镜像中,而无需重建镜像

c# - UWP 应用程序更快地加载图像?

python - 相机标定,像素方向反投影

python - 开放式 CV 轮廓面积计算错误