braunlee
braunlee

Reputation: 71

Use OpenCV to stitch 2d histograms - Merge cv2.detail.MatchesInfo

I am attempting to stitch multiple 2d histograms (2d data arrays) together for which the horizontal and vertical axes of the histograms are spatial coordinates, but the origin for the different histograms may vary. This task therefore requires affine transformations after identifying matching features, for which a slightly modified OpenCV stitching pipeline (https://github.com/opencv/opencv/blob/4.x/samples/python/stitching_detailed.py) is used.

I first transform this grayscale data to RGB format that CV is expecting (see below), then apply the stitching pipeline. The stitched RGB image is finally converted back to a 2d array using code from a different SO thread: https://stackoverflow.com/a/71649250/12533405

from matplotlib.colors import LinearSegmentedColormap
from matplotlib._cm import _jet_data
from matplotlib import cm
import numpy as np
import cv2 as cv

def load_data(*files):
    
    data = dict()
    maxvals = list()
    arrays = list()
    
    for file in list(files):
        arr = np.loadtxt(file)
        arrays.append(arr)
        maxvals.append(np.max(arr))
    
    maxval = max(maxvals)

    mymap = cm.get_cmap(LinearSegmentedColormap('myjet',_jet_data,N=10000))

    for idx,arr in enumerate(arrays):
        img = mymap(np.flipud(arr)/maxval,bytes=True)[:,:,[2,1,0]]

        data[list(files)[idx]] = cv.resize(img, (0,0), fx=3, fy=3)
        
    return data, maxval

Two histograms with features matched in the overlap region

This routine works quite well if the processed data exhibits enough overlap with matchable features.

On to the problem

The spatial histogram data is actually 3D, meaning that the 2d array produced for processing is simply a summation along the third axis for a specific region of interest. Summing over a different region of interest would produce a very different false-color image with the same spatial coordinates.

Question: While it is possible to extract the camera settings (estimator) for one region of interest and apply them to the warper for a different region of interest, I wonder how it would be possible to construct the camera settings taking multiple regions of interest into account simultaneously?

Currently, orb finds features for each image in one region of interest, then the detail_AffineBestOf2NearestMatcher finds matching features and feeds to the estimator. I was hoping that multiple instances of cv2.detail.MatchesInfo corresponding to different regions of interest could be merged. Is that possible?

It seems OpenCV is quite useful to approach this problem but I am also open to different implementations.

Upvotes: 0

Views: 75

Answers (0)

Related Questions