-1

How can to split image in parts, apply histogram equalization, compose an image form the parts and display it I am trying to figure a way to split an image into defined number of parts, meaning the image is split into a number of rectangeles that when combined form the whole image. Then to each of the parts I want to apply histogram equalization. After that I want to be able to form a new image from the parts of the first image that have the histogram equalization already applied

## so far I know how to apply the histogram equalization to the entire image    

import cv2
import numpy as np
from matplotlib import pyplot as plt
 
## load image
img = cv2.imread('/home/pi/Downloads/bear.bmp',0)
 
## equalize
equ = cv2.equalizeHist(img)

plt.subplot(111),plt.imshow(equ, cmap = "gray"),plt.title('Equalized')
plt.xticks([]), plt.yticks([])
plt.show()
kristian
  • 27
  • 9

2 Answers2

2

Since you have figured out how to do histogram equalization on an image, there is left the question of splitting the original image and then merging back the pieces.

OpenCV is rather nice and provides you the concept of a region of interest (ROI), which is a part of an image (defined as a rectangle). For all practical purposes, a ROI acts as an image, but if you modify it, the original image is modified too.

Therefore, you have to extract all ROIs that interest you and apply histogram equalization on them. The merging is handled implicitly, since the ROI is a part of the image itself.

Look here and here for more information about ROIs.

Paul92
  • 8,827
  • 1
  • 23
  • 37
  • I have to divide the image in a defined number of steps in x and y direction (e.g. 3 in x and 4 in y ), not just focus in a region of interest. – kristian Mar 31 '19 at 17:04
  • Yes. My point was that instead of physically dividing the image, you can recompute appropriate positions of the region of interest. – Paul92 Mar 31 '19 at 18:35
1

Try this out:

import cv2
import numpy as np

img = cv2.imread("1.jpg")
(h,w,c) = img.shape
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

parts = []
step_x = 3
step_y = 3
eqs = []
eq_img = np.zeros_like(gray_img)

for x in range(step_x):
    for y in range(step_y):
        xratio1 = x/step_x
        xratio2 = (x+1)/step_x
        yratio1 = y/step_y
        yratio2 = (y+1)/step_y
        part = gray_img[int(yratio1*h):int(yratio2*h),int(xratio1*w):int(xratio2*w)].copy()
        parts.append(part)

        cv2.imshow("x = {0}, y = {1}".format(x,y),part)

        eq = cv2.equalizeHist(part)
        eqs.append(eq)
        eq_img[int(yratio1*h):int(yratio2*h),int(xratio1*w):int(xratio2*w)] = eq

cv2.imshow("eq_img",eq_img)
cv2.waitKey(0)

enter image description here enter image description here

Ha Bom
  • 2,787
  • 3
  • 15
  • 29