直方图均衡的OpenCV python代码中的语法错误

时间:2016-06-15 04:45:16

标签: python matlab

我想在Ubuntu中编码直方图均衡而不使用内置函数。我已经用matlab代码翻译了两个函数的代码,即一个用于直方图绘制,第二个是直方图均衡,以下是两个代码

function histogram = histog(image)
    dim = size(image)
    L=256;
    histogram=zeros(1,L);
    for i=1:dim(1)
     for j=1:dim(2)
        a=image(i,j);
        histogram(a+1) = histogram(a+1) + 1;
    end
end
subplot(1,2,2)
stem(histogram)
subplot(1,2,1)
orignal = imhist(image);
stem(orignal)
end

直方图发现的代码是

function histEQ = hist_eq(image)
h1=histog(image); %histogram of the image
%freq_original = histogram_image(image);
dim = size(image);
L=256;

pro = zeros(1,L)
for n=1:L
    for k=1:n
        pro(n) = pro(n) + h1(k);
    end
    pro(n)=(pro(n)/(dim(1)*dim(2)));
end
recon=zeros(1,L);
for i=1:dim(1)
    for j=1:dim(2)
        recon(i,j) =uint8( pro(image(i,j)+1)*255);
    end
end

b=histeq(image);
h2=histog(recon);
figure(1)
subplot(1,2,1)
stem(h1)
subplot(1,2,2)
stem(h2)
figure(2)
imshow(recon)
title('orignal image')
figure(3)
imshow(image)
title('after equalization using my function')
figure(4)
imshow(b)
title('using histeq builtin')
end

我已经在ubuntu中将此代码翻译成了OpenCV的python,但我在循环内部行中有一些错误

import numpy as np
import cv2
from matplotlib import pyplot as p

img = cv2.imread('89.jpg',1)
x,y,z = img.shape

hist = np.zeros(1,256)
histeq = np.zeros(1,256)
h=s=n=k=1
for h in range(x):
        for s in range(y):
            a = img(h,s)
            hist(a+1) = hist(a+1) +1
p.plot(hist)#simple histogram
# now histogram equalization
for n in range(256):
        for k in range(n):
             histeq(n) = histeq (n) + hist(k)
        histeq = (histeq(n))/(x *y)
recon = np.zeros(1,256)       
for i in range(x):
        for j in range(y):
            recon(i,j) = np.int8( histeq(img(i,j)+1)*255)

p.plot(recon)

0 个答案:

没有答案