Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom metadata to OpenCV/numpy image?

I have a question.

I am performing a series of operations (resize, copy, etc.) to some captured images in OpenCV, but I want to set some specific metadata (the attribute names should also be defined by me) to these images, which would survive those operations and can be extracted later on. I am not setting resolution values or any other value that changes after these operations. How can I do this? I heard of pyexiv2, but I have never seen any example involving OpenCV.

Thanks in advance!

like image 723
Subhamoy S. Avatar asked Mar 21 '12 16:03

Subhamoy S.


1 Answers

So, ultimately, you could just facade the 2 file contents (data and metadata) behind your own python class:

import cv 
import pyexiv2

class MyImage():

  _filename = None
  _data = None
  _metadata = None

  def __init__(self,fname):

    _filename = fname
    _data = cv.LoadImage(_filename)
    _metadata = pyexiv2.ImageMetadata(_filename)
    _metadata.read()

  def addMeta(self,key,value):
    _metadata[key] = pyexiv2.ExifTag(key, value)

  def delMeta(self,delkey):
    newdict = {key: value for key, value in some_dict.items()
                if value is not delkey}

  def resize(self,newx,newy):
    tmpimage = cv.CreateMat(newy, newx, cv.CV_8UC3)
    cv.Resize(_data,tmpimage)
    _data = tmpimage
    try: # Set metadata tags to new size if exist!
      _metadata['Exif.Image.XResolution'] = newx
      _metadata['Exif.Image.YResolution'] = newy
    except:
      pass

  def save(self):
    cv.SaveImage(_filename,_data) 
    _metadata.write()
like image 193
timlukins Avatar answered Nov 14 '22 08:11

timlukins