0

Okay guys, I am stuck trying to create a histogram matching for two images; a template image and a destination image(destination meaning the image where i want to match the template image). Rather than displaying the matched image, i get a blank image. I feel like i am exhausted at this hence why i came to SO. Can someone guide me in the right direction?

Any Help in the right direction would be appreciated.

def matching(template, target, numberOfBins=256):

    templateHist, bins1 = np.histogram(template.flatten(), numberOfBins, density = False)
    targetHist, bins2 = np.histogram(target.flatten(), numberOfBins, density = False)
    cdfTemplate = templateHist.cumsum() #Cumulative distributed function
    cdfTemplate = (255 * cdfTemplate / cdfTemplate[-1]) #normalize
    cdfTarget = targetHist.cumsum()
    cdfTarget = (255 * cdfTarget / cdfTarget[-1]).astype(np.float64)
    im2 = np.interp(template.flatten(), cdfTemplate, bins1[:-1])
    im3 = np.interp(im2, cdfTarget, bins2[:-1])
    result = im3.reshape((template.shape))

    return result
  • 1
    See here: http://stackoverflow.com/q/32655686/1461210 – ali_m May 14 '16 at 17:49
  • it means that cdfTemplate[-1] is returning 0. – Andrew Li May 14 '16 at 18:18
  • 1
    The error message is telling you that `cdfTemplate[-1]` is equal to 0, which results in all of the elements in `cdfTemplate` becoming NaNs. This implies that the sum of `templateHist` must also be 0. It's harder to explain why this should be the case, given that it consists of histogram bin counts, and the automatic selection of histogram bin edges ought to ensure that at least one bin contains a non-zero count. What are `template` and `target` like? I'm guessing that they are masked arrays? – ali_m May 14 '16 at 18:21
  • 1
    The only thing I can think of that would give you all-zero bin counts is if you were passing an empty array to `np.histogram` – ali_m May 14 '16 at 18:24
  • It is passing in an empty array into np.histogram. Every article i read up on histogram matching gives me similiar functionality and design. –  May 14 '16 at 18:52

1 Answers1

0
  • The error message is telling you that cdfTemplate[-1] is equal to zero, which results in all of the elements in cdfTemplate becoming NaNs.
  • Working backwards, this implies that the sum of templateHist must also be zero.
  • templateHist contains bin counts computed from template.flatten(). Since you haven't specified a set of weights for np.histogram, there's no way that any of the elements in templateHist could be negative. Therefore templateHist must be all zeros.
  • You are also passing a positive integer as the bins parameter to np.histogram, and you haven't specified the range parameter. Therefore if template contains any values then np.histogram should automatically select a set of bin edges such that at least one of the counts would be positive.
  • If template contained NaNs or infinite values you should get a ValueError rather than a vector of all-zero bin counts.
  • Therefore the logical conclusion is that template must be an empty array.
ali_m
  • 71,714
  • 23
  • 223
  • 298
  • I'm a little lost when you said i havent specified the range parameter. Can you be a little more clear. –  May 14 '16 at 21:44
  • 1
    [`np.histogram`](http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.histogram.html) accepts a `range` parameter that can be used to specify the `(lower, upper)` limits for the range of bin edges. In principle you could have got all-zero bin counts because you passed two numbers such that all of the values in `template` fell outside this range, but you didn't. – ali_m May 14 '16 at 21:50
  • You're absolutely right. I'm debugging my code and found out as soon as the first second statement in my *hist function* gets executed, the result == 0. –  May 14 '16 at 22:07
  • Okay, @ali_m, Can you guide me in selecting my range. I tried following [http://docs.scipy.org/doc/numpy-dev/reference/generated/numpy.histogram.html] but it still is a bit unclear to me. –  May 14 '16 at 22:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111951/discussion-between-brandon-j-and-ali-m). –  May 14 '16 at 23:53
  • Added what the contents were. –  May 14 '16 at 23:58
  • That can't be correct - if you pass an array containing NaNs to `np.histogram` you should get a `ValueError`. What is `type(template)`? – ali_m May 15 '16 at 00:01
  • It's a 2000 x 2000 array. –  May 15 '16 at 00:06
  • What kind of array? A `numpy.ndarray`? A masked array? – ali_m May 15 '16 at 00:07
  • it's a numpy.ndarray. –  May 15 '16 at 00:10
  • I can't reproduce what you're describing using numpy 1.10.4. The array you've shown in your question contains at least one NaN. If I pass an array containing any NaNs to `np.histogram` I get a `ValueError: range parameter must be finite`, not a vector of all-zero bin counts. That said, I strongly suspect that the fact that `template` contains NaNs is the cause of your problems. – ali_m May 15 '16 at 00:21
  • Okay thanks anyways. I'll accept that answer. I'll figure it out. –  May 15 '16 at 00:26