使用OpenCV将一系列图像转换为阵列,然后将RGB数组转换为灰度

发布于 2025-01-27 01:33:02 字数 383 浏览 3 评论 0原文

嗨,我正在尝试将一系列图像转换为阵列,然后将RGB转换为灰度。

在我的工作文件夹中,我有x帧数。png,我需要在数组中读取所有这些帧,然后将每个帧(RGB)转换为灰度。

对于一帧,我的代码是:

import numpy as np
import cv2 as cv
from PIL import Image

# Read image

Image = cv.imread('frame0.png') 


# RGB to Gray Scale

GS = cv.cvtColor(Image, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)

有什么想法吗?

Hi I'm trying to convert a series of images into an array and then convert the RGB to gray scale.

In my work folder I have x number of frames.png, I need to read all this frames in an array and then convert each frame (RGB) to Gray scale.

For one frame my code is:

import numpy as np
import cv2 as cv
from PIL import Image

# Read image

Image = cv.imread('frame0.png') 


# RGB to Gray Scale

GS = cv.cvtColor(Image, cv.COLOR_BGR2GRAY)
th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒浓于脸红 2025-02-03 01:33:02

您可以使用OS从文件夹读取文件。使用“ Endswith”功能,您可以提取文件格式并将其全部拉动。

这是一个工作代码

import numpy as np
import cv2 as cv
import os

for file in os.listdir("/mydir"): # images folder path
    if file.endswith((".png",".jpg")): # you can add formats
        img = cv.imread(file)
        GS = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
        cv.imwrite("converted-"+file,Gray)

You can use os to read files from a folder. With the "endswith" function, you can extract file formats and pull them all.

Here is a working code

import numpy as np
import cv2 as cv
import os

for file in os.listdir("/mydir"): # images folder path
    if file.endswith((".png",".jpg")): # you can add formats
        img = cv.imread(file)
        GS = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        th, Gray = cv.threshold(GS, 128, 192, cv.THRESH_OTSU)
        cv.imwrite("converted-"+file,Gray)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文