Filter Image such that only certain colours show

82 views (last 30 days)
I'm looking to filter a radar image in png, and I only want the parts of the image that are orange, yellow and red to show up on the picture. I'm then looking to calculate the distance from these orange/yellow/red parts to other points on the map. Any help greatly appreciated, thanks!

Answers (1)

Rohit Kudva
Rohit Kudva on 20 Jul 2015
Hi Carissa,
For this task, you may convert the RGB image to HSV (Hue-Saturation-Value) image. HSV separates the image intensity from the color information. Then you can use the color information of the HSV image to filter out a specific range of colors (In your case from Red to Yellow).
If you have Image Processing Toolbox, you can use the Color Thresholder App to filter out parts of the image with specific colors. You can refer to the following link on how to use the Color Thresholder App.
In case you do not have the toolbox, you can use some basic MATLAB functions to obtain the desired image. However, you will need to know the threshold values for 'hue', 'saturation' and 'value' to filter the image for specific color range. Refer to the following script:
% read the original image
I = imread('radar_img.jpg');
% call createMask function to get the mask and the filtered image
[BW,maskedRGBImage] = createMask(I);
% plot the original image, mask and filtered image all in one figure
subplot(1,3,1);imshow(I);title('Original Image');
subplot(1,3,2);imshow(BW);title('Mask');
subplot(1,3,3);imshow(maskedRGBImage);title('Filtered Image');
Following is a sample code for 'createMask' function:
function [BW,maskedRGBImage] = createMask(RGB)
% Convert RGB image to HSV image
I = rgb2hsv(RGB);
% Define thresholds for 'Hue'. Modify these values to filter out different range of colors.
channel1Min = 0.965;
channel1Max = 0.188;
% Define thresholds for 'Saturation'
channel2Min = 0.000;
channel2Max = 1.000;
% Define thresholds for 'Value'
channel3Min = 0.000;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
BW = ( (I(:,:,1) >= channel1Min) | (I(:,:,1) <= channel1Max) ) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
% Initialize output masked image based on input image.
maskedRGBImage = RGB;
% Set background pixels where BW is false to zero.
maskedRGBImage(repmat(~BW,[1 1 3])) = 0;
You can then use the binary mask 'BW' to calculate the distance between the two set of points on the map.
I hope you find my answer helpful.
- Rohit
  3 Comments
eltaf kazemi
eltaf kazemi on 27 Jun 2021
Edited: eltaf kazemi on 27 Jun 2021
very good Naveen
what is your suggestion for using other simple commands instead of creat mask function ? like 'edge' , ' 'rgb2gray' , color filters and so on....
for example i do this for my project and i want to remove noises at the end (just show me the disease on leaf)... I'm so grateful for your help with this, thanks!
close all;
clear all;
pic = imread('LEAF_0334.jpg');
I = imadjust(pic,stretchlim(pic));
Agray = rgb2gray(I);
eg = edge(Agray,'canny',0.3);
%eg_1 = bwareaopen(eg,300);
bw = im2bw(eg);
se = strel('disk',3);
bw = imdilate(bw,se);
bw2 = imfill(bw,'holes');
r = bwlabel(bw2);
subplot(3,3,1),imshow(pic),title('original');
subplot(3,3,2),imshow(I),title('adjust');
subplot(3,3,3),imshow(Agray),title('grayscale');
subplot(3,3,4),imshow(eg),title('canny');
subplot(3,3,5),imshow(bw),title('dilate');
subplot(3,3,6),imshow(bw2),title('fill');
subplot(3,3,7),imshow(r),title('label');

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!