# How to divide an image by another image in opencv-python?

**URL:** <https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199>\
**Category:** Uncategorized\
**Tags:** core\
**Created:** [September 14, 2022, 6:58pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199 "2022-09-14T18:58:50Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 14, 2022, 6:58pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/1 "2022-09-14T18:58:50Z")

</div>

Hello dear OpenCV community.  
I’m wondering if there is a command in OpenCV-Python that can divide an image by another image.  
[Matlab has one](https://www.mathworks.com/help/images/ref/imdivide.html) which is called _imdivide_. I’ve been failing to find the analog in Python in general and in OpenCV in particular.  
Thank you in advance.  
Ivan  
P.S. If there is none, I guess, I will go with _numpy_ to divide the images …  
P.P.S. If anybody is interested, I need it to implement a min-max filter. Which is also not implemented in OpenCV. According to the forums, the general consensus on the min-max filter is to subtract _erode_ from _dilate_. But, actually, min-max filter is more than that. Thus, one of the other parts of the filter is normalization, that’s where I need image division.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 15, 2022, 5:43am UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/2 "2022-09-15T05:43:12Z")

</div>

since it is all about numpy arrays, you can simply use the builtin `/` operator :

```auto
>>> a = np.array([[1,2],[3,4]])
>>> b = np.array([[2,2],[2,2]])
>>> a / b
array([[0.5, 1.],
       [1.5, 2.]])

```

and, before you start writing a `min-max filter`, have a look at [cv.normalize()](https://docs.opencv.org/4.x/d2/de8/group __core__ array.html#ga87eef7ee3970f86906d69a92cbf064bd)

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 15, 2022, 6:06pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/3 "2022-09-15T18:06:27Z")

</div>

Thank you @berak .

I looked into _cv2.normalize()_ and here’s a note from me.

Evidently, I was loose with the language.

First, I should’ve said that I wanted to implement _ **local** normalization_. By that I would mean division of each pixel’s intensity of the original image by an intensity value unique for this particular pixel. I.e., I would have an original image which may be considered as the matrix of pixel intensities and I have a corresponding matrix of the same size where every entry is the normalizing intensity. That second matrix can be considered as an image. That is why I wanted to divide an image by another image.

Second, personally, if somebody said to me “normalization” (not local, as I mentioned above, but just normalization), I would, probably, assume that I need to divide each pixel’s intensity of the original image by some value which is the same for all the pixels. Usually, that value is the norm of the image (i.e. the norm of the matrix representing the image). But, evidently, that is not the case with _cv2.normalize()_.

Third, as stated in the _cv2.normalize()_ manual, the _cv2.normalize_ function converts the original image _img_ to another image _dst_ such that the norm (e.g. _L\_2 norm_) of the _dst_ image is equal to the supplied parameter _alpha_. Personally, this is an absolutely new definition of the term _“normalization”_ to me. I have never heard of it before. I don’t see how it can help me with my task. I’m going to adhere to the _numpy_ solution.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 15, 2022, 6:54pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/4 "2022-09-15T18:54:21Z")

</div>

indeed, normalize(NORM\_MINMAX) uses a single, global min/max value.  
(simply, for each pixel, subtract the global min, then divide by global (max-min))

> [@nepomnyi](#):
>
> division of each pixel’s intensity of the original image by an intensity value unique for this particular pixel.

what would _that_ be ? some local neighbourhood ?

> [@nepomnyi](#):
>
> how it can help me with my task

which is, exactly ? please explain (on a higher level) !  
what’s the concept behind the min-max filter, and what do you need it for ?

> [@nepomnyi](#):
>
> the general consensus on the min-max filter is to subtract _erode_ from _dilate_.

i wonder, where you found that idea ? it’s for sure not the ‘general’ case.  
(and would only work on binary images)

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 15, 2022, 7:20pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/5 "2022-09-15T19:20:28Z")

</div>

Here is what I want to do.

Take into account that _cv2.dilate_ and _cv2.erode_ can work with gray scale images and can be considered as _max_ and _min_ filters (I found that out in numerous posts on StackOverflow).  
Let _originalImage_ be original image.  
Let _low_ be eroded original image.  
Let _upp_ be dilated original image.  
Then I want to compute (_originalImage_ - _low_) / (_upp_ - _low_).

All those operations are pixel by pixel operations. They can be done with _numpy_ as you mentioned. I just wanted to see if there were cv2 native division function (like, e.g., there is _cv2.subtract_ for subtraction).

If you compare those operations with what _cv2.normalize_ do, you’ll readily see that _cv2.normalize_ is not of use here.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 15, 2022, 11:00pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/6 "2022-09-15T23:00:46Z")

</div>

perhaps I don’t understand what you’re asking because clearly there exists this:

[https://docs.opencv.org/4.x/d2/de8/group\_\_core\_\_array.html#ga6db555d30115642fedae0cda05604874](https://docs.opencv.org/4.x/d2/de8/group __core__ array.html#ga6db555d30115642fedae0cda05604874)

and you surely must have found that already, right?

and beyond that, OpenCV supports “matrix expressions” in C++, same as numpy does in Python.

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 15, 2022, 11:14pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/7 "2022-09-15T23:14:21Z")

</div>

Thank you @crackwitz , that’s exactly what I was looking for!

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 16, 2022, 6:37am UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/8 "2022-09-16T06:37:42Z")

</div>

still curious, what is your use-case for this ?  
what kind of data are you processing ?

(as i havent encountered this in real life, ever !)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 16, 2022, 10:03am UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/9 "2022-09-16T10:03:42Z")

</div>

I can give an example.

say someone has a sheet of paper with text on it, it’s unevenly lit, and they take a photo of it. the intensity of a “sheet” pixel depends on its illumination, as a scale factor. well lit area: pixels are 200 (blank) or 20 (toner), badly lit area: pixels are 50 or 5.

now you could apply a sufficient median to estimate illumination, assuming it’s mostly text (ink/toner covers less than 50%)

then you _divide_, to get “normalized” intensity, which leaves you with pixels being 100% and 10% in _both_ cases, resulting in a good picture, except for the inevitable noise.

_subtracting_ instead (and adding white, but let’s ignore that) would distort the picture, leaving dark parts with less contrast. you’d get `[200, 20]-200 = [0, -180]` vs `[50, 5]-50 = [0, -45]`

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 16, 2022, 5:27pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/10 "2022-09-16T17:27:01Z")

</div>

I’m processing PIV data.

PIV stands for particle image velocimetry. That’s the main tool in experimental fluid mechanics created in 1984s. This method gives you velocity field in a flow. Velocity field is when you know velocity at each point in a flow (turns out velocities at different points in a flow are different in general). All our equations describing fluid motion are written in terms of velocity fields. Once you’ve measured velocity fields, plug them into the equations and you are good to go uncovering new physics of flows. The way to measure velocity field is to seed the flow with tiny particles, illuminated them with a thin laser sheet, take two photos with a high-speed camera. Knowing the time between the photos and, provided you can identify the distance each particle has moved between the photos, you divide the distance by the time and get the velocity of the particle which you assume equal to the velocity of the flow at the point where the particle is located.

Identifying individual particles on the photos reliably enough to get the acceptable measurement accuracy can be difficult (due to uneven laser illumination, reflections and so on). Therefore, before you go ahead and measure velocity fields, you have to preprocess (precondition) the images. The preprocessing scheme can be different. For instance, for some images it is enough to just subtract the background. For others, you have to low-pass-filter the image, subtract it from the original image, then high-pass-filter the image, then binarize it and only then you can estimate the velocity field.

One of the preprocessing steps suggested by the guru (and creator) of PIV is image enhancement. Which is pretty much enhancing the contrast of the image. The contrast of the image (as suggested by the guru) can be enhanced either by the means of histogram equalization of by the means of min-max filter. Both of these methods have the purpose to increase the contrast of the image to make the seeding particles more distinct relative to the background.

The min-max filtering algorithm is suggested in the guru’s book (Adrian & Westerweel, “Particle image velocimetry”, 2011, Cambridge university press, see p.250 for the algorithm). The algorithm is presented in a MATLAB version in the book. I’m trying to convert it to Python for my needs.

As an example, I’m attaching one of my PIV images (not the best one)

 ![Test 1.73sikznz.000000b](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/f/fcd5e490ee34fabf1c501c5cbf8d724e753e6e19.jpeg)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 17, 2022, 1:26pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/11 "2022-09-17T13:26:39Z")

</div>

thanks a ton for that detailled & helpful explanation !

so, we’re talking about (re)implementing this ?

 ![minmax](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/c/cd84cd11cede39d65e53c81ee141bf8ed7d95d59.png)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 17, 2022, 2:09pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/12 "2022-09-17T14:09:11Z")

</div>

```python
def methlab_minmax(image, ksize, mincontrast):
    N = ksize ** 2
    kernel = np.ones((ksize, ksize), np.uint8)
    upp = cv.dilate(image, kernel)
    low = cv.erode(image, kernel)
    #upp = cv.filter2D(upp, ddepth=cv.CV_32F, kernel=kernel) / N
    #low = cv.filter2D(low, ddepth=cv.CV_32F, kernel=kernel) / N
    upp = cv.blur(upp, ksize=(ksize, ksize)) # faster
    low = cv.blur(low, ksize=(ksize, ksize))
    contrast = np.maximum(upp - low, mincontrast)
    return (image - low) / contrast

```

_(edit: forgot the lowpass step, it’s amended)_

why they would go with 64 instead of anything else is a mystery to me. or why they would not calculate `N = FiltSize^2`

the erosion/dilation can be done quicker but OpenCV only supports arbitrary kernels, not specifically square uniform ones. maybe there’s a fast path in the code… I hope.

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 17, 2022, 3:17pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/13 "2022-09-17T15:17:07Z")

</div>

@crackwitz , multiplication by 64 is MATLAB specific, you don’t need to do it in Python. I looked for why to multiply by 64 and found a couple of answer on StackOverFlow if I’m not mistaken. I don’t remember the explanation, but you have to do it in MATLAB.

---

<div class="post-metadata">

**Author:** ![nepomnyi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/nepomnyi/32/5734_2.png) [@nepomnyi](https://forum.opencv.org/u/nepomnyi)\
**Post date:** [September 17, 2022, 3:19pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/14 "2022-09-17T15:19:35Z")

</div>

@berak yep. Here’s my [ResearchGate question](https://www.researchgate.net/post/Can_anybody_clarify_the_min-max_filter_implementation_as_shown_in_Adrians_and_Westerweels_2011_PIV_book) and here’s [my gist](https://gist.github.com/nepomnyi/b8546038a8e70f234f41969491d272c8).

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 17, 2022, 3:56pm UTC](https://forum.opencv.org/t/how-to-divide-an-image-by-another-image-in-opencv-python/10199/15 "2022-09-17T15:56:13Z")

</div>

![image](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/e/eb64fc107a6672b9718144b6ba76ff4cb68b9b58.png)

the `64` figure is _not_ matlab-specific. the author just felt like it. that’s all there is to it. ok the book mentions some histogram equalization but those numbers are just pulled out of someone’s you-know-what.

did you know [http://www.openpiv.net/](http://www.openpiv.net/)
