Image Processing Functions and Formulas

Image Processing Functions

Morphological Operations

Opening and Closing:

function [openedImage, closedImage] = opening_closing(image, se)
    openedImage = imopen(image, se);
    closedImage = imclose(image, se);
end

Erosion and Dilation:

function [erodedImage, dilatedImage] = erosion_dilation(image, se)
    erodedImage = imerode(image, se);
    dilatedImage = imdilate(image, se);
end

Color Space Conversions

RGB to HSI:

function [HSI] = rgb_2_hsi(RGB)
    R = double(RGB(:,:,1)) / 255;
    G = double(RGB(:,:,2)) / 255;
    B = double(RGB(:,:,3)) / 255;
    I = (R + G + B) / 3;
    minVal = min(cat(3, R, G, B), [], 3);
    S = 1 - (3 ./ (R + G + B + eps)) .* minVal;
    num = 0.5 * ((R - G) + (R - B));
    den = sqrt((R - G).^2 + (R - B) .* (G - B));
    H = acos(num ./ (den + eps));
    H(B > G) = 2 * pi - H(B > G);
    H = H / (2 * pi);
    HSI = cat(3, H, S, I);
end

HSI to RGB:

function [RGB] = hsi_2_rgb(HSI)
    H = HSI(:,:,1) * 2 * pi;
    S = HSI(:,:,2);
    I = HSI(:,:,3);
    R = zeros(size(H));
    G = zeros(size(H));
    B = zeros(size(H));
    idx = H < 2 * pi / 3;
    B(idx) = I(idx) .* (1 - S(idx));
    R(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ cos(pi/3 - H(idx)));
    G(idx) = 3 * I(idx) - (R(idx) + B(idx));
    idx = (H >= 2 * pi / 3) & (H < 4 * pi / 3);
    H(idx) = H(idx) - 2 * pi / 3;
    R(idx) = I(idx) .* (1 - S(idx));
    G(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ cos(pi/3 - H(idx)));
    B(idx) = 3 * I(idx) - (R(idx) + G(idx));
    idx = H >= 4 * pi / 3;
    H(idx) = H(idx) - 4 * pi / 3;
    G(idx) = I(idx) .* (1 - S(idx));
    B(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ cos(pi/3 - H(idx)));
    R(idx) = 3 * I(idx) - (G(idx) + B(idx));
    R = uint8(R * 255);
    G = uint8(G * 255);
    B = uint8(B * 255);
    RGB = cat(3, R, G, B);
end

Top-Hat and Bottom-Hat Transforms

function top_bottom_hat(image, se)
    opened = imopen(image, se);
    topHat = image - opened;
    closed = imclose(image, se);
    bottomHat = closed - image;
    subplot(2, 2, 1); imshow(image); title('Original');
    subplot(2, 2, 2); imshow(topHat); title('Top-Hat');
    subplot(2, 2, 3); imshow(bottomHat); title('Bottom-Hat');
end

Pseudocolor

function pseudocolor(image)
    grayImg = mat2gray(image);
    colormap jet;
    imagesc(grayImg);
    colorbar;
    title('Pseudocolor');
end

Color Slicing

function color_slicing(image, targetColor, tolerance)
    diff = abs(double(image) - targetColor);
    mask = all(diff <= tolerance, 3);
    result = uint8(mask) .* image;
    subplot(1, 2, 1); imshow(image); title('Original');
    subplot(1, 2, 2); imshow(result); title('Color Slicing');
end

Histogram Equalization (RGB)

function equalize_rgb(image)
    R = image(:, :, 1);
    G = image(:, :, 2);
    B = image(:, :, 3);
    R_eq = histeq(R);
    G_eq = histeq(G);
    B_eq = histeq(B);
    equalizedImage = cat(3, R_eq, G_eq, B_eq);
    subplot(1, 2, 1); imshow(image); title('Original');
    subplot(1, 2, 2); imshow(equalizedImage); title('Equalized');
end

Image Processing Formulas

Fourier Transform

\(F(u, v) = \sum_{x=0}^{M-1} \sum_{y=0}^{N-1} f(x, y) e^{-j2\pi(\frac{ux}{M} + \frac{vy}{N})}\)

Inverse Fourier Transform

\(f(x, y) = \frac{1}{MN} \sum_{u=0}^{M-1} \sum_{v=0}^{N-1} F(u, v) e^{j2\pi(\frac{ux}{M} + \frac{vy}{N})}\)

Power Spectrum

\(P(u, v) = |F(u, v)|^2\)

Gaussian Low-Pass Filter

\(H(u, v) = e^{-\frac{D(u, v)^2}{2D_0^2}}\)

Gaussian High-Pass Filter

\(H(u, v) = 1 – e^{-\frac{D(u, v)^2}{2D_0^2}}\)

Ideal Low-Pass Filter

\(H(u, v) = \begin{cases} 1, & D(u, v) \leq D_0 \\ 0, & D(u, v) > D_0 \end{cases}\)

Convolution Theorem

\((f * h)(x, y) \iff F(u, v) \cdot H(u, v)\)

Phase Spectrum

\(\phi(u, v) = \arctan\left(\frac{\text{Imaginary}(F(u, v))}{\text{Real}(F(u, v))}\right)\)

RGB to HSI Conversion

\(H = \cos^{-1}\left(\frac{(R-G) + (R-B)}{2\sqrt{(R-G)^2 + (R-B)(G-B)}}\right), \text{if } B > G: H = 360^\circ – H\)

\(S = 1 – \frac{3}{R + G + B} \cdot \min(R, G, B), \quad I = \frac{R + G + B}{3}\)

HSI to RGB Conversion

\(R = I \cdot (1 + \frac{S \cdot \cos(H)}{\cos(60^\circ – H)}), \quad G = 3I – (R + B), \quad B = I \cdot (1 – S)\)

RGB to CMY Conversion

\(C = 1 – R, \quad M = 1 – G, \quad Y = 1 – B\)

RGB to CMYK Conversion

\(K = 1 – \max(R, G, B), \quad C = \frac{1 – R – K}{1 – K}, \quad M = \frac{1 – G – K}{1 – K}, \quad Y = \frac{1 – B – K}{1 – K}\)

Top-Hat Transform

\(T_{\text{hat}}(f) = f – (f \circ B)\)

Bottom-Hat Transform

\(B_{\text{hat}}(f) = (f \bullet B) – f\)

Chromaticity Coordinates

\(x = \frac{X}{X + Y + Z}, \quad y = \frac{Y}{X + Y + Z}, \quad z = 1 – (x + y)\)

Erosion

\(A \ominus B = \{z \mid B_z \subseteq A\}\)

Dilation

\(A \oplus B = \{z \mid B_z \cap A \neq \emptyset\}\)

Opening

\(A \circ B = (A \ominus B) \oplus B\)

Closing

\(A \bullet B = (A \oplus B) \ominus B\)