0% found this document useful (0 votes)
2 views118 pages

Lecture 3 Part1 Intensity Transformations Spatial Filtering

This document is a lecture on image enhancement techniques, focusing on intensity transformations and spatial filtering. It covers various methods such as point and mask processing, histogram processing, and specific transformations like image negatives, thresholding, and power-law transformations. The lecture also includes practical implementation tips for MATLAB and Python, along with examples and visual representations of the techniques discussed.

Uploaded by

Trân Trân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views118 pages

Lecture 3 Part1 Intensity Transformations Spatial Filtering

This document is a lecture on image enhancement techniques, focusing on intensity transformations and spatial filtering. It covers various methods such as point and mask processing, histogram processing, and specific transformations like image negatives, thresholding, and power-law transformations. The lecture also includes practical implementation tips for MATLAB and Python, along with examples and visual representations of the techniques discussed.

Uploaded by

Trân Trân
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 118

Hanoi University of Science and Technology

Department of Automation Engineering

Computer Vision
Lecture 3
Intensity Transformations and Spatial Filtering

Van-Truong Pham, Ph.D., Associate Professor


School of Electrical and Electronic Engineering
Hanoi University of Science and Technology
Site: https://seee.hust.edu.vn/pvtruong
Outlines

 About image enhancement


 Some basic intensity transformation functions
 Histogram processing
 Fundamentals of spatial filtering
 Smoothing spatial filters
 Sharpening spatial filters
About Image Enhancement

 Image enhancement is the process of making images


more useful
 The reasons for doing this include:
 Highlighting interesting detail in images
 Removing noise from images
 Making images more visually appealing
About Image Enhancement

Schematic diagram of image enhancement technique


About Image Enhancement

Spatial Domain Image Enhancement Techniques: Basically, two


categories: mask processing and point processing .
1. Point processing: each pixel in original image at
coordinates (x, y) is processed to create the corresponding
pixel at coordinates (x, y) in the enhanced image
2. Mask processing: Not only the pixel in original image at
coordinates (x, y) is processed, but also some neighboring
pixels of this pixel at coordinates (x, y) in the enhanced
image are involved in creating the new pixel.
About Image Enhancement—Several Examples
Image Enhancement
Image Enhancement

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Image Enhancement Example

Original Image Enhancement Image


by Gamma correction

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
About Image Enhancement—Several Examples
About Image Enhancement—Several Examples
About Image Enhancement—Several Examples
About Image Enhancement—Several Examples
Spatial domain VS frequency domain
Spatial domain VS frequency domain

 Spatial domain techniques directly manipulate image


pixels
 Frequency domain techniques manipulate Fourier
transform or wavelet transform of an image
About Image Enhancement

Image is often described in the space domain or the frequency


domain

(a) Schematic diagram of


mask processing with
many input pixels to
produce one output pixel

(b) Schematic diagram of


point processing technique
using one to one.
Spatial domain
Spatial domain
Mask/Filter
Image Pixels Manipulation
Spatial domain
Some basic intensity transformation functions
Some basic intensity transformation functions
Phép biến đổi âm bản- Image Negatives
Phép biến đổi âm bản- Image Negatives

Used for enhancing white or gray details embedded in


dark regions, especially when the black areas are dominant in size
Phép biến đổi âm bản- Image Negatives

(a) Negative image of (a)

Implementation Tips
1. You can write your own code since it is straightforward
2. Or you can use the routine imcomplement
3. Or you can use the routine imadjust
Phép biến đổi âm bản- Image Negatives
Implementation in Matlab and Python import cv2
import numpy as np
%This script shows how to generate import matplotlib.pyplot as plt

a negative image for a given image # Đọc ảnh và chuyển sang ảnh xám
im = cv2.imread('hand-xray.jpg',
im = imread('hand-xray.jpg'); cv2.IMREAD_GRAYSCALE)

im = rgb2gray(im); # Tạo ảnh âm bản (negative) thủ công


imNeg_manual = 255 - im
%this is myself implementation plt.figure()
plt.imshow(imNeg_manual, cmap='gray')
imNeg = 255 - im; plt.title('Negative Image (Manual)')
figure; plt.show()
imshow(imNeg,[]);
# Tạo ảnh âm bản bằng hàm cv2.bitwise_not (tương tự
imcomplement trong Matlab)
%or, you can use the matlab routine imNeg_complement = cv2.bitwise_not(im)
imcomplement plt.figure()
plt.imshow(imNeg_complement, cmap='gray')
imNeg = imcomplement(im); plt.title('Negative Image (cv2.bitwise_not)')
figure; plt.show()
imshow(imNeg,[]);
# Tạo ảnh âm bản bằng cách điều chỉnh (tương tự
imadjust trong Matlab)
%or, you can use the matlab routine imNeg_adjust = cv2.normalize(im, None, 1, 0,
imadjust cv2.NORM_MINMAX) # Điều chỉnh mức xám
imNeg_adjust = 1 - imNeg_adjust # Đảo ngược giá trị
imNeg = imadjust(im,[0,1],[1,0]); plt.figure()
figure; plt.imshow(imNeg_adjust, cmap='gray')
imshow(imNeg,[]); plt.title('Negative Image (cv2.normalize)')
plt.show()
Phép biến đổi âm bản- Image Negatives

Results

Input Images (a) Negative Images of (a)


Image Negative Original
digital
mammogram
Negative
digital
mammogram

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Phép biến đổi âm bản- Image Negatives
Phép biến đổi âm bản- Image Negatives
Thresholding

Thresholding transformations are particularly useful f


or segmentation in which we want to isolate an object
of interest from a background

Implementation Tips
1. You can write your own code since it is straightforward
2. Or you can use the routine im2bw
Thresholding
Implementation close all
clear all
im = imread('hand-xray.jpg');
im = rgb2gray(im);
figure;
imshow(im,[]);
%you can use the matlab routine im2bw
imNeg = im2bw(im, 0.1);
figure;
imshow(imNeg,[]);

Input Images (a) Thresholding result of (a)


Implementation in Matlab and Python
import cv2
close all import matplotlib.pyplot as plt
clear all from skimage import filters
im = imread('hand-xray.jpg');
im = rgb2gray(im); # Đọc ảnh và chuyển sang ảnh xám
figure; im = cv2.imread('hand-xray.jpg',
cv2.IMREAD_GRAYSCALE)
imshow(im,[]);
%you can use the matlab routine # Hiển thị ảnh gốc
im2bw plt.figure()
imNeg = im2bw(im, 0.1); plt.imshow(im, cmap='gray')
figure; plt.title('Original Grayscale Image')
imshow(imNeg,[]); plt.show()

# Tạo ảnh nhị phân (binary image) với ngưỡng 0.1


(tương đương im2bw)
thresh = filters.threshold_otsu(im) # Tìm ngưỡng nhị
phân tự động (Otsu)
imNeg = im > thresh * 0.2 # Áp dụng ngưỡng 0.2

# Hiển thị ảnh nhị phân


plt.figure()
plt.imshow(imNeg, cmap='gray')
plt.title('Binary Image (Threshold 0.1)')
Input Images (a) Thresholding result of (a) plt.show()
Log Transformations – Phép biến đổi Log
Log Transformations – Phép biến đổi Log
Phép biến đổi Log ngược
Log Transformations – Phép biến đổi Log
Log Transformations – Phép biến đổi Log
Log Transformations – Phép biến đổi Log
Log Transformations – Phép biến đổi Log
# Đọc và hiển thị ảnh màu
im = cv2.imread('truong.jpg')
Implementation in Matlab and Python plt.figure()
plt.imshow(cv2.cvtColor(im, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.show()
close all
clear all # Chuyển ảnh sang ảnh xám
im =imread('truong.jpg'); im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
Figure; imshow(im)
# Tính FFT của ảnh
im = rgb2gray(im);
imfft = np.abs(np.fft.fft2(im_gray))
imfft = abs(fft2(im));
imfft = fftshift(imfft); # Dịch tâm FFT về giữa ảnh
figure; imfft_shifted = np.fft.fftshift(imfft)
imshow(imfft,[]);
imfftlog = log10(1+imfft); # Hiển thị ảnh FFT
figure; plt.figure()
plt.imshow(imfft_shifted, cmap='gray')
imshow(imfftlog,[]);
plt.title('FFT of the Image (shifted)')
plt.show()

# Áp dụng phép log để co dữ liệu


imfft_log = np.log10(1 + imfft_shifted)

# Hiển thị ảnh FFT với log


plt.figure()
plt.imshow(imfft_log, cmap='gray')
plt.title('Logarithmic FFT of the Image')
plt.show()
Power‐Law (Gamma) Transformations
Power‐Law (Gamma) Transformations
Power‐Law (Gamma) Transformations
Power‐Law (Gamma) Transformations

Implementation Tips
1. You can write your own code since it is straightforward
2. Or you can use the routine imadjust

im =imread('citylandscape.tif');
imEnhanced = imadjust(im,[0,1],[0,1],4.0);
figure;
subplot(1,2,1);
imshow(im,[]);
title('original input');
subplot(1,2,2);
imshow(imEnhanced,[]);
title('power-law enhanced output');
import cv2
import numpy as np
import matplotlib.pyplot as plt
Implementation in Matlab and Python
# Đọc ảnh
im = cv2.imread('citylandscape.tif', cv2.IMREAD_GRAYSCALE)

# Chuẩn hóa ảnh từ 0-255 về 0-1 để thực hiện phép toán


gamma
im_normalized = im / 255.0
im =imread('citylandscape.tif');
imEnhanced = # Áp dụng phép tăng cường theo power-law (gamma) với
imadjust(im,[0,1],[0,1],4.0); gamma = 4.0
figure; gamma = 4.0
subplot(1,2,1); imEnhanced = np.power(im_normalized, gamma)
imshow(im,[]);
# Chuyển ảnh về dải giá trị 0-255 để hiển thị
title('original input'); imEnhanced = np.uint8(imEnhanced * 255)
subplot(1,2,2);
imshow(imEnhanced,[]); # Hiển thị ảnh gốc và ảnh đã tăng cường
title('power-law enhanced output'); plt.figure(figsize=(10, 5))

# Ảnh gốc
plt.subplot(1, 2, 1)
plt.imshow(im, cmap='gray')
plt.title('Original Input')
plt.axis('off')

# Ảnh đã tăng cường


plt.subplot(1, 2, 2)
plt.imshow(imEnhanced, cmap='gray')
plt.title('Power-Law Enhanced Output')
plt.axis('off')

plt.show()
Power‐Law (Gamma) Transformations

Power law
transformations are
useful for general‐
purpose contrast
manipulation
Power‐Law (Gamma) Transformations
Another Contrast Stretching Function
Another Contrast Stretching Function
Implementation in Matlab
%This script shows how to perform contrast
stretching for a a given image
im = imread('boneXRay.tif');
im = im2single(im);
m = 0.2;
E = 0.9;
csResult = constrastStrechForAnImage(im, m, E);

figure;
subplot(1,2,1); imshow(im,[]);
title('original input');
subplot(1,2,2); imshow(csResult,[]);
title('constrast stretched result');

function result = constrastStrechForAnImage(im, m, E)

result = 1./(1+(m./im).^E);
import cv2
Python code import numpy as np
import matplotlib.pyplot as plt

# Hàm thực hiện việc kéo căng độ tương phản


def contrast_stretch(im, m, E):
return 1 / (1 + (m / (im + 1e-6)) ** E)

# Đọc ảnh từ file


image = cv2.imread('boneXRay.tif', cv2.IMREAD_GRAYSCALE)
image = image.astype(np.float32) / 255 # Chuyển đổi ảnh về
dạng float32 và chuẩn hóa

# Tham số cho kéo căng độ tương phản


m = 0.2
E = 0.9

# Áp dụng kéo căng độ tương phản


cs_result = contrast_stretch(image, m, E)

# Hiển thị ảnh gốc và kết quả


plt.figure(figsize=(10, 5))

# Hiển thị ảnh gốc


plt.subplot(1, 2, 1)
plt.imshow(image, cmap='gray')
plt.title('original input')

# Hiển thị kết quả kéo căng độ tương phản


plt.subplot(1, 2, 2)
plt.imshow(cs_result, cmap='gray')
plt.title('contrast stretched result')

plt.show()
Another Contrast Stretching Function- An Example
Piece‐wise Linear Transformations

 Giãn độ tương phản


Giãn độ tương phản
 Phép biến đổi tuyến tính từng khúc
Giãn độ tương phản
Giãn độ tương phản
Giãn độ tương phản
Giãn độ tương phản
Giãn độ tương phản
Intensity level slicing - Làm mỏng mức xám

 Trong thực tế, chiếu sáng cao trong một giải đặc trưng các
cấp xám đối với một ảnh là việc thường xảy ra
Intensity level slicing - Làm mỏng mức xám
Intensity level slicing - Làm mỏng mức xám
Intensity level slicing - Làm mỏng mức xám

Implementation in Matlab

close all
clear all
im = double(imread('kidney.bmp'));

highLevel = 200;

intervalMask = im > 60 & im < 80;


resultIm = im .* (1-intervalMask) + intervalMask * highLevel;
figure;
subplot(1,2,1); imshow(uint8(im));
title('original input');
subplot(1,2,2); imshow(uint8(resultIm));
title('intensity slicing output');
import cv2
Implementation in Python import numpy as np
import matplotlib.pyplot as plt

# Đọc ảnh từ file


image = cv2.imread('kidney.bmp', cv2.IMREAD_GRAYSCALE)
image = image.astype(np.float64) # Chuyển ảnh sang kiểu
float64

# Tham số mức cao


high_level = 200

# Tạo mặt nạ cho khoảng cường độ


interval_mask = (image > 60) & (image < 80)

# Áp dụng cắt mức cường độ


result_image = image * (1 - interval_mask) + interval_mask *
high_level

# Hiển thị ảnh gốc và kết quả


plt.figure(figsize=(10, 5))

# Hiển thị ảnh gốc


plt.subplot(1, 2, 1)
plt.imshow(image.astype(np.uint8), cmap='gray')
plt.title('original input')

# Hiển thị kết quả cắt mức cường độ


plt.subplot(1, 2, 2)
plt.imshow(result_image.astype(np.uint8), cmap='gray')
plt.title('intensity slicing output')

plt.show()
Intensity level slicing - Làm mỏng mức xám
Gray Level Slicing

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Intensity level slicing - Làm mỏng mức xám
Bit‐plane slicing- Làm mỏng mứcmặt phẳng Bit
Bit‐plane slicing- Example
Implementation in Matlab
im = imread('HUST.jpg');
im= rgb2gray(im);
imshow(im)
[rows, cols] = size(im);
bitPlanes = zeros(rows, cols, 8); %since the image is a 8-bit gray
image, there are 8 bit-planes
for shiftIndex = 0:7
bitShiftedIm = bitshift(im, -shiftIndex);
bitPlane = mod(bitShiftedIm, 2);
figure;
imshow(bitPlane,[]);
bitPlanes(:,:,shiftIndex+1) = bitPlane;
end
%reconstruction based on bitplane 8,7,and 6
reconstructedImage1 = 128 * bitPlanes(:,:,8) + 64 * bitPlanes(:,:,7) +
32 * bitPlanes(:,:,6);
figure;
imshow(reconstructedImage1,[]);
imwrite(uint8(reconstructedImage1),'reconstructed1.bmp');
%reconstruction based on bitplane 8,7,6,and 5
reconstructedImage2 = 128 * bitPlanes(:,:,8) + 64 * bitPlanes(:,:,7) +
32 * bitPlanes(:,:,6) + 16 * bitPlanes(:,:,5);
figure;
imshow(reconstructedImage2,[]);
imwrite(uint8(reconstructedImage2),'reconstructed2.bmp');
import cv2
import numpy as np
Implementation in Python
import matplotlib.pyplot as plt

# Đọc ảnh và chuyển đổi thành ảnh xám


im = cv2.imread('HUST.jpg', cv2.IMREAD_GRAYSCALE)

# Hiển thị ảnh gốc


plt.figure() plt.figure()
plt.imshow(im, cmap='gray') plt.imshow(reconstructedImage1, cmap='gray')
plt.title('Original Image') plt.title('Reconstructed Image from Bit Planes 8, 7, 6')
plt.axis('off') plt.axis('off')
plt.show() plt.show()

# Lấy kích thước của ảnh # Lưu ảnh tái tạo


rows, cols = im.shape cv2.imwrite('reconstructed1.bmp', reconstructedImage1)

# Khởi tạo mảng lưu các bit-plane # Tái tạo ảnh dựa trên các bit-plane 8, 7, 6 và 5
bitPlanes = np.zeros((rows, cols, 8), dtype=np.uint8) reconstructedImage2 = (128 * bitPlanes[:, :, 7] +
64 * bitPlanes[:, :, 6] +
# Tách ảnh thành các bit-plane 32 * bitPlanes[:, :, 5] +
for shiftIndex in range(8): 16 * bitPlanes[:, :, 4])
bitShiftedIm = np.right_shift(im, shiftIndex)
bitPlane = np.mod(bitShiftedIm, 2) plt.figure()
plt.figure() plt.imshow(reconstructedImage2, cmap='gray')
plt.imshow(bitPlane, cmap='gray') plt.title('Reconstructed Image from Bit Planes 8, 7, 6, 5')
plt.title(f'Bit Plane {shiftIndex+1}') plt.axis('off')
plt.axis('off') plt.show()
plt.show()
bitPlanes[:, :, shiftIndex] = bitPlane # Lưu ảnh tái tạo
cv2.imwrite('reconstructed2.bmp', reconstructedImage2)
# Tái tạo ảnh dựa trên các bit-plane 8, 7 và 6
reconstructedImage1 = (128 * bitPlanes[:, :, 7] +
64 * bitPlanes[:, :, 6] +
32 * bitPlanes[:, :, 5])
Bit‐plane slicing- Example

Image of our university


Bit‐plane slicing- Example

bp0 bp1

bp2 bp3 bp4

bp5 bp6 bp7


Bit‐plane slicing- Example

Original image reconstruction by highest 3 bitplanes

reconstruction by highest 4 bitplanes reconstruction by highest 5 bitplanes


Bit-plane Slicing

Bit 7 Bit 6

Bit 5

Bit 3
Bit 2 Bit 1
(Images from Rafael C. Gonzalez and Richard E.
Wood, Digital Image Processing, 2nd Edition.
Introduction to Histogram (Lược đồ xám)
Introduction to Histogram (Lược đồ xám)
Ví dụ: Tính lược đồ xám của ảnh I

Biểu diễn lược đồ xám của ảnh I

Lập bảng
Introduction to Histogram
Introduction to Histogram
Introduction to Histogram
Implementation in Matlab and Python import cv2
import numpy as np
close all import matplotlib.pyplot as plt
clear all; # Đọc ảnh và chuyển sang ảnh xám
im = imread('Truong1.jpg'); image = cv2.imread('Truong1.jpg')
im= rgb2gray(im); gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
imwrite(uint8(im),'truong.bmp'); # Lưu ảnh dưới dạng tệp BMP
imshow(im) cv2.imwrite('truong.bmp', gray_image)
f=imread('truong.bmp');
# Hiển thị ảnh xám
h=imhist(f); cv2.imshow('Gray Image', gray_image)
imhist(f); cv2.waitKey(0)
% axis([0 255 min(h) max(h)]); cv2.destroyAllWindows()
axis([0 255 min(h) 5e4]); # Đọc lại ảnh BMP và hiển thị histogram
bmp_image = cv2.imread('truong.bmp',
cv2.IMREAD_GRAYSCALE)

# Tính histogram
hist = cv2.calcHist([bmp_image], [0], None, [256], [0, 256])

# Vẽ histogram với giới hạn trục y


plt.figure()
plt.plot(hist)
plt.xlim([0, 255])
plt.ylim([0, 5e4]) # Giới hạn trục y
plt.title('Histogram')
plt.xlabel('Pixel Intensity')
I and my histogram plt.ylabel('Frequency')
plt.show
Histogram of an Image

h( rk )  nk

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Histogram of an Image (cont.)

(Images from Rafael C. Gonzalez and Richard E.


Wood, Digital Image Processing, 2nd Edition.
Introduction to Histogram

Chuẩn hóa lược đồ xám


Introduction to Histogram
Xử lý lược đồ xám
Introduction to Histogram
Introduction to Histogram
Introduction to Histogram
Introduction to Histogram
import cv2
import numpy as np
# Hiển thị histogram sử dụng stem plot
import matplotlib.pyplot as plt
plt.figure()
plt.stem(np.arange(256), histogram[:, 0],
# Đọc ảnh từ file
basefmt=" ", markerfmt=" ",
image = cv2.imread('pout.tif',
use_line_collection=True)
cv2.IMREAD_GRAYSCALE)
plt.title('Stem Plot of Histogram')
plt.xlim([0, 255])
# Hiển thị ảnh
plt.ylim([min(histogram),
plt.figure()
max(histogram)])
plt.imshow(image, cmap='gray')
plt.show()
plt.title('Original Image')
# Hiển thị histogram sử dụng plot
# Tính histogram của ảnh
plt.figure()
histogram = cv2.calcHist([image], [0], None,
plt.plot(np.arange(256), histogram[:, 0])
[256], [0, 256])
plt.title('Plot of Histogram')
plt.xlim([0, 255])
# Hiển thị histogram sử dụng imhist
plt.ylim([min(histogram),
plt.figure()
max(histogram)])
plt.hist(image.ravel(), bins=256, range=[0, 256])
plt.show()
plt.title('Histogram using imhist')
plt.xlim([0, 255])
plt.ylim([min(histogram), max(histogram)])
plt.show()

# Hiển thị histogram dưới dạng thanh (bar plot)


plt.figure()
plt.bar(np.arange(256), histogram[:, 0],
width=1.0)
plt.title('Bar Plot of Histogram')
plt.xlim([0, 255])
plt.ylim([min(histogram), max(histogram)])
plt.show()
Introduction to Histogram

h(k) or p(k)

k
Introduction to Histogram

Ảnh tối
close all
clear all;
f=imread(‘I1.jpg');
figure(1)
imshow(f)
h=imhist(f);
figure(2)
imhist(f);
axis([0 255 min(h) max(h)]);
Introduction to Histogram

Ảnh sáng
Introduction to Histogram
Introduction to Histogram
Properties of the Histogram

Histogram h(x) does not depend on the locations of


image pixels. Thus, it is impossible to reproduce image
f from its histogram.
Properties of the Histogram
Histogram Equalization (cân bằng lược đồ xám)
Phương pháp cân bằng lược đồ xám

How to find such a T?


Histogram Equalization (cân bằng lược đồ xám)
Ví dụ cân bằng lược đồ xám
Histogram Equalization (cân bằng lược đồ xám)
Ví dụ cân bằng lược đồ xám (tiếp)
Histogram Equalization (cân bằng lược đồ xám)
Ví dụ cân bằng lược đồ xám (tiếp)
Histogram Equalization (cân bằng lược đồ xám)

Histogram Equalization
 Histogram equalization to enhance image
 Due to the different gray levels of images, one
needs to process to equalize the gray level
distribution of the image before other
processing steps.
Histogram Equalization (cân bằng lược đồ xám)
Histogram Equalization (cân bằng lược đồ xám)
Histogram Equalization (cân bằng lược đồ xám)

 Matlab
J = histeq(I); %Enhance contrast %using histogram equalization

 Python
J = cv2.equalizeHist(I) # Cải thiện độ tương phản sử dụng Histogram qualization
Histogram Equalization (cân bằng lược đồ xám)
import cv2
Implementation in Matlab and Python import numpy as np
import matplotlib.pyplot as plt

# Đọc ảnh
close all I = cv2.imread('I2.jpg', cv2.IMREAD_GRAYSCALE)
clear all
%I = imread('pout.tif'); # Hiển thị ảnh và histogram gốc
plt.figure(figsize=(10,5))
I=imread(‘I2.jpg');
figure plt.subplot(1,2,1)
subplot(1,2,1) plt.imshow(I, cmap='gray')
imshow(I) plt.title('Original Image')
subplot(1,2,2)
plt.subplot(1,2,2)
imhist(I,64) plt.hist(I.ravel(), bins=64, range=[0, 256])
plt.title('Original Histogram')
J = histeq(I); %Enhance contrast %using
histogram equalization plt.show()

# Cải thiện độ tương phản sử dụng Histogram qualization


figure J = cv2.equalizeHist(I)
subplot(1,2,1)
imshow(J) # Hiển thị ảnh và histogram sau khi cân bằng histogram
subplot(1,2,2) plt.figure(figsize=(10,5))
imhist(J,64) plt.subplot(1,2,1)
plt.imshow(J, cmap='gray')
plt.title('Enhanced Image')

plt.subplot(1,2,2)
plt.hist(J.ravel(), bins=64, range=[0, 256])
plt.title('Enhanced Histogram')

plt.show()
Histogram Equalization (cân bằng lược đồ xám)

(a) Ảnh và lược đồ xám


trước khi cân bằng

(b) Ảnh và lược đồ xám


sau khi cân bằng
Histogram Equalization (cân bằng lược đồ xám)

(a) Ảnh và lược đồ


xám trước khi cân
bằng

(a)
(b) Ảnh và lược đồ
xám sau khi cân
bằng

(b)
Equalization Transformation Function
Equalisation Transformation Functions
The functions used to equalise the images in the previous example
Equalization Examples
1
Equalization Examples
2
Equalization Examples
3

4
Summary about Histogram

 Histogram represents the intensity occurrences of a


given image
 Histogram is a global descriptor; reflect no structural
information; two totally different images can have
identical histograms
 Histogram has many potential applications in DIP
 Histogram equalization can enhance the image’s
contrast

You might also like