0

I am implementing a conference paper and it was originally in matlab and I translated it into python to process it with opencv using fuzzy contextual contrast enhancement method attached paper here in gdrive for reference. Research Paper Drive link open to all

import cv2
import math
import numpy as np

image = cv2.imread('img1.bmp')
CIm = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

a=CIm[:,:,2]
Im = a/255
def f1(num):
    return math.floor(num*10000)
Im = np.vectorize(f1)(Im)

rows = Im.shape[0]
cols = Im.shape[1]

Ffactor = np.zeros(Im.shape,dtype = int)
H = np.zeros((10001,1),dtype=float)

w=1

width = round(np.std(Im.astype(float)))
for i in range(rows):
    for j in range(cols):
        
        totf = 0.0
        count = 0 
        f = 0.0
        
        for ii in range(-w,w+1):
            for jj in range(-w,w+1):
                if (i + ii >= 0 and i + ii < rows and j + jj >= 0 and j + jj < cols):
                            count = count + 1 ;
                            temp = float(Im[i + ii,j + jj]) - float(Im[i,j])
                            f = max(min(1-abs(temp/width),1),0)
                            totf = totf + (f)
                            
        t =  totf/9
        print(count)
        Ffactor[i,j] = t
                
        H[Im[i,j]] = H[Im[i,j]] + math.log2(t)**2
Hpdf =  H/np.sum(H)
#total = np.sum(H)
#for k in range(H.shape[0]):
 #   Hpdf[k] = H[k]/total
    
    
def FHE(Im,Ffactor ,Hpdf, lval, hval):
    Hcdf = np.cumsum(Hpdf)
    m=np.mean(Im,dtype=float)
    s=np.std(Im,dtype=float)
    
    delta = s/m
    
    for i in range(Im.shape[0]):
        for j in range(Im.shape[1]):
            temp = lval + round(( hval- lval) * Hcdf[Im[i,j]] )
            Im[i,j] = ((temp - float(Im[i,j])) * math.exp((-(Ffactor[i,j])**2)*1*delta) + Im[i,j])
            
    return Im   

Imf = FHE(Im, Ffactor,Hpdf, 0, 10000)
Imf = Imf/10000
Imf = Imf*255
Imf = np.vectorize(round)(Imf)
CIm[:,:,2] = Imf;
Im_out = cv2.cvtColor(CIm, cv2.COLOR_HSV2BGR)
cv2.imshow('Original image',image)
cv2.imshow('equalised image', Im_out)
   
cv2.waitKey(0)
cv2.destroyAllWindows()

Source Image processed Image

I don't get that why i am getting jigsaw puzzles in my processed image ??

I ran it with other pictures and it worked fine but with this particular picture it gives me this boxes everywhere and i can't figure out why ?? i was running original code of paper which was in matlab and it was better and worked fine without boxes. I want to remove these boxes in my processed image pls check where fault lies in code.

Ankit Raj
  • 1
  • 1
  • One thought -- before dividing a by 255, be sure that you have converted a to float. Keep the processing as float until the end and then convert back to uint8. – fmw42 Feb 04 '23 at 05:22
  • Can you show the result image from matlab? If contrast in a quite constant region is maximized it could look similar. – Micka Feb 04 '23 at 10:00
  • Im_out may be out of the range 0 to 255. So check its min an max values and scale to the range 0 to 255 and convert to uint8. – fmw42 Feb 04 '23 at 22:11
  • @Micka [Matlab Result](https://drive.google.com/file/d/1GSzAyZMGYyzfjjc93hmsDKuDw4GrHA7o/view?usp=sharing) This is the processed image through original code of the paper – Ankit Raj Feb 05 '23 at 15:25
  • @fmw42 I checked it maximum is 255 and minimum is 0 and i converted Im into float then converted it to uint8 at the end of processing – Ankit Raj Feb 05 '23 at 15:28
  • Did you convert 'a' to float before dividing by 255? – fmw42 Feb 05 '23 at 17:18

0 Answers0