Ankit Raj
Ankit Raj

Reputation: 1

What is the problem with my code that it makes my resultant image of this particular image as jigsaw puzzles?

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.

Upvotes: 0

Views: 51

Answers (0)

Related Questions