PythonMagick JPEG2000 数据提取
我正在开发一个应用程序,该应用程序(除其他外)需要读取卫星图像(每个图像只有一个波段)并处理像素数据。
格式是 JPEG-2000,因此我无法使用 PIL 库(它简化了一切)。我找到了PythonMagick库,我可以完美地读取图像并提取像素的值。但只针对一个像素!
im=PythonMagick.Image(dirimage) # (This is very slow....)
a=im.pixelColor(j-1,i-1).intensity() # the value intensity is extracted for one pixel
a=a/int(XML_var[37][2]) # the reflectance values are normalised to the range [0,1]
因此,我需要一个 for 循环来获取所有像素值(图像非常大)。 我尝试使用 Blob 函数来获取数据,但它崩溃了。
还有更好的选择吗?如何快速获取JPEG2000图像的像素数据并将其保存到数组中?
I am working in an application which (among other things) need to read a satellite image (with only one band per image) and process the pixel data.
The format is JPEG-2000 and therefore I cannot use the PIL library (which simplifies everything). I have found the PythonMagick library and I can perfectly read the image and extract the value of the pixel. But only for one pixel!
im=PythonMagick.Image(dirimage) # (This is very slow....)
a=im.pixelColor(j-1,i-1).intensity() # the value intensity is extracted for one pixel
a=a/int(XML_var[37][2]) # the reflectance values are normalised to the range [0,1]
Therefore, I need a for-loop to get all the pixel values (the images are very large).
I tried with Blob function to get the data but it crashes.
Are there any better options? How could I quickly get the pixel data of a JPEG2000 image and save it into an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Blob 应该可以:
Using a Blob should work:
你给我的答案很好,完美地提取了像素信息(将 uint8 更改为 uint16)。然而,我得到的值比真实值要高。存在偏移,并且由于 JPEG2000 中的 LOSSY 压缩,该值存在 1 或 2 的小误差。
我不喜欢使用外部调用,但在这种情况下,我发现这是一个更好、更快的解决方案:
我下载了 Kakadu(用于非商业目的的免费软件)并使用模块 kdu_expand。
os.system('kdu_expand -i image.jp2 -o temp_image.tif')
im=PIL.Image.open('temp_image.tif')
像素=数组(im.getdata()).reshape((im.size[ 0], im.size[1]))
我将图像从 JPEG2000 转换为 TIF,但速度很快,而且静态内存通常不是计算机中的限制(现在)。然后,PIL 库完美地提取了数据。
注意:我直接使用 PythonMagick 尝试了转换,但它给了我与之前相同的偏移量
注 2:我在 OpenCV 中发现了另一个有趣的库,但结果不正确
Pixels_cv2=cv2.imread('image.jp2',0)
注 3:我使用的图像是用 12 位编码的卫星图像。可能在其他类型的数据中,PythonMagick 表现得更好。
The answer you gave me is great and extract perfectly the pixel information (changing uint8 to uint16). However, the values I obtain are higher than the real ones. There is an offset and because of the LOSSY compression in JPEG2000 there is a little error of 1 or 2 in the value.
I don't like to use external calls but in this case I found this as a better and faster solution:
I downloaded Kakadu (free software for non commercial purposes) and I use the module kdu_expand.
os.system('kdu_expand -i image.jp2 -o temp_image.tif')
im=PIL.Image.open('temp_image.tif')
pixels=array(im.getdata()).reshape((im.size[0], im.size[1]))
I convert the image from JPEG2000 to TIF but it is quick and the static memory is not usually a limitation (nowadays) in a computer. Then, the PIL library perfectly manages to extarct the data.
Note: I tried the conversion straight with PythonMagick but it gives me the same offset as before
Note 2: I found another interesting library in OpenCV but the result is incorrect
pixels_cv2=cv2.imread('image.jp2',0)
Note3: The images I used are satellite images codified with 12 bites. Possibly in other type of data the PythonMagick behaves better.
对于运行 Kakadu (KDU) 演示的人来说,有一点偏离主题的评论。想补充一点,如果运行 kdu_expand,如上所述: os.system('kdu_expand -i image.jp2 -o temp_image.tif') 这会给你一个错误:
检查 libkdu_v78R.dylib 的权限并将其更改为 644 或666,或具有读/写访问权限的东西。
Slight off-topic comment for those running the Kakadu (KDU) demo. Would like to add that if running kdu_expand, like above: os.system('kdu_expand -i image.jp2 -o temp_image.tif') and this gives you an error:
Check the permissions of libkdu_v78R.dylib and change them to 644 or 666, or something with read/write access.