Lecture 3 Part1 Intensity Transformations Spatial Filtering
Lecture 3 Part1 Intensity Transformations Spatial Filtering
Computer Vision
Lecture 3
Intensity Transformations and Spatial Filtering
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)
Results
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,[]);
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)
# Ảnh gốc
plt.subplot(1, 2, 1)
plt.imshow(im, cmap='gray')
plt.title('Original Input')
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');
result = 1./(1+(m./im).^E);
import cv2
Python code import numpy as np
import matplotlib.pyplot as plt
plt.show()
Another Contrast Stretching Function- An Example
Piece‐wise Linear Transformations
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;
plt.show()
Intensity level slicing - Làm mỏng mức xám
Gray Level Slicing
# 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
bp0 bp1
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
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])
h( rk ) nk
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 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()
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)
(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