0% found this document useful (0 votes)
29 views4 pages

Histogram Matching in MATLAB

This document provides an overview of color and contrast techniques for object detection in images. It discusses thresholding grayscale and color images to create binary detection masks. Thresholding involves assigning pixels above or below a threshold value as detected or not detected. The document also introduces color spaces like HSV that may be easier to threshold than RGB. It describes how the HSV channels represent hue, saturation, and value of colors. Thresholding a single HSV channel like hue can help detect objects of a specific color. Binary operations on thresholded images can clean up detections.

Uploaded by

Jgg
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)
29 views4 pages

Histogram Matching in MATLAB

This document provides an overview of color and contrast techniques for object detection in images. It discusses thresholding grayscale and color images to create binary detection masks. Thresholding involves assigning pixels above or below a threshold value as detected or not detected. The document also introduces color spaces like HSV that may be easier to threshold than RGB. It describes how the HSV channels represent hue, saturation, and value of colors. Thresholding a single HSV channel like hue can help detect objects of a specific color. Binary operations on thresholded images can clean up detections.

Uploaded by

Jgg
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

Lecture 2: Color and Contrast

Object Detection
 The goal of object detection is to identify which
pixels belong to a certain object or class (and
Lecture 2: which pixels do not).
Color and Contrast  A detection mask is a binary image which
assigns a 0 or 1 to each pixel:
 1=object detected (positive / WHITE)
Math 490  0=object not detected (negative / BLACK)
Prof. Todd Wittman
The Citadel

Grayscale Thresholding Logicals


 The simplest way to perform detection on a grayscale
image is to threshold the pixels at some level T.  We create a 0-1 logical matrix in Matlab by putting a
1 if ,  <
 ,  = 
boolean statement in square brackets.
0 if ,  ≥  This creates a matrix with value 1 where the statement is
 For example, the coins are dark objects on a white true, 0 where it is false.
background. So we would choose a threshold less than
pure white, say T=200. Ex D = [A > 100];
201 0 6 255

255 199 202 15


A= D=
253 0 200 100

If the coins were bright objects on a dark background,


we may threshold the other way: D i, j = 1 if ,  > .


Logicals Selecting the Threshold


 Picking the right threshold value is a bit of an
 Basic Matlab boolean operators art form. It may require some trial and error.
== Equality & And  Thresholding is subjective. Different people

~ Not | Or may prefer different thresholds.


 It may help to add a colorbar to your image.

 Detect objects with a specific gray value:  The Matlab command graythresh uses Otsu's
algorithm to pick a good dividing line,
D = [A == 153]; normalized to the range [0,1]. You may use
 Detect dark objects: this as a starting point.
D = [A<115]; A=imread('[Link]');
level = 255*graythresh(A)
 Detect light objects:
126
D = [A>180]; subplot(121); imshow(A); colorbar;
 Detect gray objects in a specific range: subplot(122); imshow([A<126]);
D = [ A>83 & A<192];

1
Lecture 2: Color and Contrast

Binary Image Operations Color Image Thresholding


 After you get your thresholded binary image, you can
clean it up using Matlab's binary image operations.  Thresholding color images is more difficult
 imdilate -- Make the blobs a little bigger. because we have 3 channels to consider.
 imerode -- Make the blobs a little smaller.
 Essentially we have to set 6 threshold values.
 bwareaopen -- Remove small blobs.
 imfill -- Fill in the holes in the blobs.
 watershed -- Separate overlapping blobs. D = [ A(:,:,1) > Rmin & A(:,:,1) < Rmax
D = [A<200]; & A(:,:,2) > Gmin & A(:,:,2) < Gmax
subplot(121); imshow(D);
& A(:,:,3) > Bmin & A(:,:,3) < Bmax ];
D2 = imfill(D, 'holes');
subplot(122); imshow(D2);

Color Image Thresholding Alternative Color Spaces


 The standard RGB format is sometimes hard to
 Ex Find the yellow M&Ms. work with.
 Several other schemes for storing color
R information have been proposed. These are
called color spaces.
 HSV
G
 IHS
 YIQ

B  YCbCr
 CIE Lab
 It takes 3 numbers to make a specific color.

HSV Color Space RGB vs. HSV


A = imread('[Link]');
 To convert from RGB to the HSV space, we calculate
Let C=max(R,G,B) - min(R,G,B). $−%
./0 6 if max (#, $, %) = #
" = max #, $, % (
( 1 %−#
*= ∗ + 2 if max (#, $, %) = $
& = '" if " ≠ 0 6 (
0 if " = 0 #−$
+ 4 if max (#, $, %) = %
(
B = rgb2hsv(A);
 The Matlab commands rgb2hsv and hsv2rgb will do the
conversion for us.
 The HSV values are normalized to be in the range [0,1].
 Note the imshow and imagesc commands are built for
RGB images. Do not try to display the HSV image.
What is the basic color? How vibrant is the color? How bright is the color?

2
Lecture 2: Color and Contrast

Hue (H) Hue Thresholding


 The first channel H describes the "basic" color of that  If we want to threshold a color image, we saw that picking out
pixel. specific RGB colors is a pain.
 It is sometimes easier to pick out the Hue. Then we only need to
 Note that the H colors are periodic. threshold one channel.
 Ex Find the yellow M&Ms.
H=0 H=1

 To shift the colors along the Hue scale by 0.2: H=0 H=1
B=rgb2hsv(A); D = [B(:,:,1)>0.1 & B(:,:,1)<0.2];
B=rgb2hsv(A); B(:,:,1)=B(:,:,1)+0.2; imshow(hsv2rgb(B));

Saturation (S) Value (V)


 The second channel S measures how vibrant the color is.  The 3rd channel V measures the intensity or brightness
 Low saturation means the color is close to white. of the color.
 Low value means the color is close to black.
S=0 S=1

To increase the saturation by 50%


V=0 V=1
B=rgb2hsv(A); B(:,:,2) = 1.5 * B(:,:,2); imshow(hsv2rgb(B));  To increase an image's value by 50%
B=rgb2hsv(A); B(:,:,3)=1.5 * B(:,:,3); imshow(hsv2rgb(B));

Advantages of HSV Contrast


 We can visualize the HSV color  Contrast is the difference in the image's brightness (or
space as a cylinder. color) values.
 Objects are more visible in images with high contrast.
 Improving the contrast can make some tasks easier,
such as thresholding to detect objects.
 Compared to RGB, it is easier to do some things
in the HSV color space.
 Threshold a color image just on the Hue channel.
 Make an image more vivid by increasing the
Saturation channel.
 Make an image brighter or darker by manipulating the
Value channel.

3
Lecture 2: Color and Contrast

Image Histograms Analyzing Histograms


 An image histogram displays the frequency of each gray  The histogram reveals why the contrast on some images is
value in the range [0,255]. poor.
 An image with good contrast should use all the gray values
 The Matlab command imhist displays the image
to a roughly equal extent (uniform distribution).
histogram of a grayscale uint8 image.
imhist(A) Dark image Light image Low contrast High contrast

4
200 0 0 255 3

A = 255 200 200 0 2


255 0 200 100 1

0 50 100 200 255

Histogram Equalization Color Histogram Equalization


 The goal of histogram equalization is to redistribute the  The histeq method only works on a
gray values of the image so that the histogram is flatter grayscale image.
(more uniform).  We could apply histogram equalization
 The Matlab command histeq produces the histogram to each RGB channel.
equalized image of a uint8 grayscale image. for i=1:3
B(:,:,i) = histeq(A(:,:,i));
A=imread('[Link]');
end
B=histeq(A);
 Or we could just apply histogram
subplot(221); imshow(A); equalization to the Value channel.
subplot(222); imhist(A); B = rgb2hsv(A);
subplot(223); imshow(B); B(:,:,3) = histeq(B(:,:,3));
subplot(224); imhist(B);
B = hsv2rgb(B);

Histogram Matching Locally Adaptive Algorithms


 Instead of flattening the histogram, we could transform the  We say a method is locally adaptive if it does different
histogram to a desired shape. things in different parts of the image.
 Histogram matching is the process of making one image's  The command adapthisteq performs adaptive histogram
histogram more closely resemble another image's histogram. equalization by looking at localized histograms, instead
 The Matlab command imhistmatch takes two uint8 images of making one global histogram.
as input and makes the first image's histogram match the
second's. Local thresholding varies the threshold parameter T
spatially: (, ) is threshold at pixel (, ).

A=imread('[Link]'); B=imread('[Link]'); C=imhistmatch(A,B);
 Niblack's method is a popular algorithm for detecting
letters in text documents. It examines the mean and
standard deviation in a 4 × 4 window around each pixel.

You might also like