【问题标题】:How do I convert multiple pictures, in a folder with ITK color map?如何在带有 ITK 颜色图的文件夹中转换多张图片?
【发布时间】:2018-12-22 07:55:05
【问题描述】:

每次我在终端中运行我的程序时,它都会打印出: thumb0496.jpg 未转换 {} 未转换

无论我做什么,它都不会起作用...我是 Python 新手,并且通过 Anaconda 以及 OpenCV、Pip 和 ITK 安装了它。我只做了 4 天就被困住了。 Python 也是我的第一语言。为什么我的代码不起作用?

如果这段代码看起来很相似。我不得不尝试结合一些元素。不幸的是,我再也找不到那个帖子了。代码之前更糟糕,但我(不知何故)修复了它。这只是我自己无法修复的(新)部件!

import cv2
import sys
import itk
import os,glob

from os import listdir,makedirs
from os.path import isfile,join
path = '/Users/admin/Desktop/ff'
dstpath = '/Users/admin/Desktop/test'

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

RGBPixelType = itk.RGBPixel[PixelType]
RGBImageType = itk.Image[RGBPixelType, Dimension]

ColormapType = itk.CustomColormapFunction[PixelType, RGBPixelType]
colormap = ColormapType.New()

ColormapFilterType = itk.ScalarToRGBColormapImageFilter[ImageType,RGBImageType]
colormapFilter1 = ColormapFilterType.New()

colormapFilter1.SetInput(reader.GetOutput())
colormapFilter1.SetColormap(colormap)

WriterType = itk.ImageFileWriter[RGBImageType]
writer = WriterType.New()
writer.SetFileName(dstpath)
writer.SetInput(colormapFilter1.GetOutput())

try:
    makedirs(dstpath)
except:
    print ("Directory already exist, images will be written in same folder")
files = [f for f in listdir(path) if isfile(join(path,f))]
for image in files:
    try:
        reader = ReaderType(os.path.join(path,image))
        map = ColormapFilterType(reader, PixelType, RGBImageType, ImageType)
        dstPath = join(dstpath,image)
        cv2.imwrite(dstPath,map)
    except:
        print ("{} is not converted".format(image))
for fil in glob.glob("*.jpg"):
    try:
        img = ReaderType(os.path.join(path,fil))
        map_imag = ColormapType(img, PixelType, RGBImageType,ImageType)
        cv2.imwrite(os.path.join(dstpath,fil),map_image)
    except:
        print('{} is not converted')

【问题讨论】:

  • 你的代码有很多问题。您没有定义ReaderType,但您正在尝试使用它。您创建了一个 ITK 编写器,但您正在尝试使用 OpenCV 编写图像。那是行不通的——只有 ITK 编写者可以编写 ITK 图像。对于您想要做的事情,您不需要 OpenCV。而且由于我不知道您要完成什么,您可能需要查看 LabelToRGBImageFilter 而不是 ScalarToRGBColormapImageFilter + CustomColormapFunction。
  • 谢谢!所以我不能将 cv2 与 itk 结合起来,很高兴知道!我已将 ReaderType 重新定义为 itk.ImageFileReader 并且仍然没有变化...当我编写 writer.Update() 时,它给了我这个错误:“您可能无法设置文件后缀,或者将后缀设置为不受支持的类型。” cv2.imwrite和cv2.imread的ITK版本是什么??

标签: python opencv directory itk color-mapping


【解决方案1】:

您为什么不从一个工作示例开始,然后逐步更改它以满足您的需求?示例可以在quick-start guide 和此博客post 中找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-27
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-26
    相关资源
    最近更新 更多