Skip to content

Commit

Permalink
Merge pull request #559 from stefan-k/master
Browse files Browse the repository at this point in the history
several color conversion functions, imedge, imstretch, imlaplacian, laplacian of gaussian
  • Loading branch information
JeffBezanson committed Mar 11, 2012
2 parents f5bbd1e + fea839e commit 69afb70
Showing 1 changed file with 123 additions and 4 deletions.
127 changes: 123 additions & 4 deletions examples/image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ end

imshow(img) = imshow(img, [])

function imadjustintensity(img, range)
function imadjustintensity{T}(img::Array{T,2}, range)
if length(range) == 0
range = [min(img) max(img)]
elseif length(range) == 1
Expand Down Expand Up @@ -199,8 +199,16 @@ end

imlaplacian() = imlaplacian("nodiagonals")

# more general version
function imlaplacian(alpha::Number)
lc = alpha/(1 + alpha)
lb = (1 - alpha)/(1 + alpha)
lm = -4/(1 + alpha)
return [lc lb lc; lb lm lb; lc lb lc]
end

# 2D gaussian filter kernel
function gaussian2d(sigma, filter_size)
function gaussian2d(sigma::Number, filter_size)
if length(filter_size) == 0
# choose 'good' size
m = 4*ceil(sigma)+1
Expand All @@ -217,17 +225,25 @@ function gaussian2d(sigma, filter_size)
return g/sum(g)
end

gaussian2d(sigma) = gaussian2d(sigma, [])
gaussian2d(sigma::Number) = gaussian2d(sigma, [])
gaussian2d() = gaussian2d(0.5, [])

# difference of gaussian
function imdog(sigma)
function imdog(sigma::Number)
m = 4*ceil(sqrt(2)*sigma)+1
return gaussian2d(sqrt(2)*sigma, [m m]) - gaussian2d(sigma, [m m])
end

imdog() = imdog(0.5)

# laplacian of gaussian
function imlog(sigma::Number)
m = 4*ceil(sigma)+1
return [((x^2+y^2-sigma^2)/sigma^4)*exp(-(x^2+y^2)/(2*sigma^2)) | x=-floor(m/2):floor(m/2), y=-floor(m/2):floor(m/2)]
end

imlog() = imlog(0.5)

# Sum of squared differences
function ssd{T}(A::Array{T}, B::Array{T})
return sum((A-B).^2)
Expand Down Expand Up @@ -376,3 +392,106 @@ imgaussiannoise{T}(img::Array{T}) = imgaussiannoise(img, 0.01, 0)
# 'illustrates' fourier transform
ftshow{T}(A::Array{T,2}) = imshow(log(1+abs(fftshift(A))),[])

function rgb2ntsc{T}(img::Array{T})
trans = [0.299 0.587 0.114; 0.596 -0.274 -0.322; 0.211 -0.523 0.312]
out = zeros(T, size(img))
for i = 1:size(img,1), j = 1:size(img,2)
out[i,j,:] = trans * squeeze(img[i,j,:])
end
return out
end

function ntsc2rgb{T}(img::Array{T})
trans = [1 0.956 0.621; 1 -0.272 -0.647; 1 -1.106 1.703]
out = zeros(T, size(img))
for i = 1:size(img,1), j = 1:size(img,2)
out[i,j,:] = trans * squeeze(img[i,j,:])
end
return out
end

function rgb2ycbcr{T}(img::Array{T})
trans = [65.481 128.533 24.966; -37.797 -74.203 112; 112 -93.786 -18.214]
offset = [16.0; 128.0; 128.0]
out = zeros(T, size(img))
for i = 1:size(img,1), j = 1:size(img,2)
out[i,j,:] = offset + trans * squeeze(img[i,j,:])
end
return out
end

function ycbcr2rgb{T}(img::Array{T})
trans = inv([65.481 128.533 24.966; -37.797 -74.203 112; 112 -93.786 -18.214])
offset = [16.0; 128.0; 128.0]
out = zeros(T, size(img))
for i = 1:size(img,1), j = 1:size(img,2)
out[i,j,:] = trans * (squeeze(img[i,j,:]) - offset)
end
return out
end

function imcomplement{T}(img::Array{T})
return 1 - img
end

function rgb2hsi{T}(img::Array{T})
R = img[:,:,1]
G = img[:,:,2]
B = img[:,:,3]
H = acos((1/2*(2*R - G - B)) ./ (((R - G).^2 + (R - B).*(G - B)).^(1/2)+eps(T)))
H[B > G] = 2*pi - H[B > G]
H /= 2*pi
rgb_sum = R + G + B
rgb_sum[rgb_sum == 0] = eps(T)
S = 1 - 3./(rgb_sum).*min(R, G, B)
H[S == 0] = 0
I = 1/3*(R + G + B)
return cat(3, H, S, I)
end

function hsi2rgb{T}(img::Array{T})
H = img[:,:,1]*2*pi
S = img[:,:,2]
I = img[:,:,3]
R = zeros(T, size(img,1), size(img,2))
G = zeros(T, size(img,1), size(img,2))
B = zeros(T, size(img,1), size(img,2))
RG = 0 <= H < 2*pi/3
GB = 2*pi/3 <= H < 4*pi/3
BR = 4*pi/3 <= H < 2*pi
# RG sector
B[RG] = I[RG].*(1 - S[RG])
R[RG] = I[RG].*(1 + (S[RG].*cos(H[RG]))./cos(pi/3 - H[RG]))
G[RG] = 3*I[RG] - R[RG] - B[RG]
# GB sector
R[GB] = I[GB].*(1 - S[GB])
G[GB] = I[GB].*(1 + (S[GB].*cos(H[GB] - pi/3))./cos(H[GB]))
B[GB] = 3*I[GB] - R[GB] - G[GB]
# BR sector
G[BR] = I[BR].*(1 - S[BR])
B[BR] = I[BR].*(1 + (S[BR].*cos(H[BR] - 2*pi/3))./cos(-pi/3 - H[BR]))
R[BR] = 3*I[BR] - G[BR] - B[BR]
return cat(3, R, G, B)
end

function imstretch{T}(img::Array{T,2}, m::Number, slope::Number)
return 1./(1 + (m./(img + eps(T))).^slope)
end

function imedge{T}(img::Array{T}, method::String, border::String)
# needs more methods
if method == "sobel"
s1, s2 = sobel()
img1 = imfilter(img, s1, border)
img2 = imfilter(img, s2, border)
return img1, img2, sqrt(img1.^2 + img2.^2), atan2(img2, img1)
elseif method == "prewitt"
s1, s2 = prewitt()
img1 = imfilter(img, s1, border)
img2 = imfilter(img, s2, border)
return img1, img2, sqrt(img1.^2 + img2.^2), atan2(img2, img1)
end
end

imedge{T}(img::Array{T}, method::String) = imedge(img, method, "replicate")
imedge{T}(img::Array{T}) = imedge(img, "sobel", "replicate")

0 comments on commit 69afb70

Please sign in to comment.