简体   繁体   English

灰度图像python实现

[英]Greyscale Image python Implementation

I try to convert a RGB image to grayscale using python as a function but the problem is I give it a RGB image that have height, width and channel but after the code I should have an image with just height and width but it gives me an image with height, width and channel why?我尝试使用 python 作为函数将 RGB 图像转换为灰度,但问题是我给它一个具有高度、宽度和通道的 RGB 图像,但在代码之后我应该有一个只有高度和宽度的图像,但它给了我一个具有高度,宽度和通道的图像为什么?

def RGBtoGRAY(img):
    height, width, channels = img.shape
    grayimg = img
    for i in range(height):
        for j in range(width):
            grayimg[i,j] = 0.3 * image[i,j][0] + 0.59 * image[i,j][1] +  0.11 * image[i,j][2]
    return grayimg

the size of the input image is输入图像的大小是

image.shape 
(533, 541, 3)

the size of the output image is输出图像的大小是

grayimage.shape 
(533, 541, 3)

normally I want to find in the size of the output image通常我想在输出图像的大小中找到

(533, 541)

You should avoid using for loops when performing image processing since it is very slow.执行图像处理时应避免使用for循环,因为它非常慢。 Instead you can use Numpy which is highly optimized for vector operations.相反,您可以使用 Numpy,它针对向量操作进行了高度优化。 Using this grayscale conversion formula :使用这个灰度转换公式

gray = R * .299 + G * .587 + B * .114

Method #1: apply_along_axis :方法#1: apply_along_axis

import cv2
import numpy as np

def grayscale(colors):
    r, g, b = colors
    return 0.299 * r + 0.587 * g + 0.114 * b

# Create image of size 100x100 of random pixels
# Convert to grayscale
image = np.random.randint(255, size=(100,100,3),dtype=np.uint8)
gray = np.apply_along_axis(grayscale, 2, image)

# Display
cv2.imshow('image', image)
cv2.imshow('gray', gray)
cv2.waitKey()

Before -> After之前->之后

在此处输入图片说明 在此处输入图片说明

Method #2: cv2.cvtColor方法#2: cv2.cvtColor

You could use OpenCV directly and read in the image as grayscale with cv2.imread by passing in the cv2.IMREAD_GRAYSCALE or 0 flag to load the image as grayscale.您可以直接使用 OpenCV 并通过传入cv2.IMREAD_GRAYSCALE0标志以将图像加载为灰度图像,并使用cv2.imread将图像作为灰度读取。

image = cv2.imread('img.png', cv2.IMREAD_GRAYSCALE) # OR
# image = cv2.imread('img.png', 0)

If you already have the image loaded, you can convert the RGB or BGR image to grayscale using cv2.cvtColor如果您已经加载了图像,则可以使用cv2.cvtColor将 RGB 或 BGR 图像转换为灰度

image = cv2.imread('img.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Assuming you are using a for loop, because you intent to solve it "manually" (like C code), there are number of issues with your implementation:假设您正在使用 for 循环,因为您打算“手动”解决它(如 C 代码),您的实现存在许多问题:

  • The assignment grayimg = img in Python does not create a copy of img (the result is that grayimg referencing img ). Python 中的赋值grayimg = img不会创建img的副本(结果是grayimg引用img )。
    You meant to use: grayimg = img.copy() .您打算使用: grayimg = img.copy()
  • img has 3 dimensions, so when using grayimg = img , grayimg also has 3 dimensions. img3 个维度,所以当使用grayimg = imggrayimg也有 3 个维度。
    You need to create grayimg with two dimensions.您需要创建具有二维的grayimg
    Example for creating grayimg and initialize to zeros:创建grayimg并初始化为零的示例:

     grayimg = np.zeros((height, width), img.dtype)
  • Inside the for loop, you are using image instead of img .在 for 循环中,您使用的是image而不是img

Here is a corrected version of RGBtoGRAY :这是RGBtoGRAY的修正版本:

def RGBtoGRAY(img):
    height, width, channels = img.shape
    #grayimg = img
    # Create height x width array with same type of img, and initialize with zeros.
    grayimg = np.zeros((height, width), img.dtype)
    for i in range(height):
        for j in range(width):
            grayimg[i,j] = 0.3 * img[i,j][0] + 0.59 * img[i,j][1] +  0.11 * img[i,j][2]
    return grayimg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM