how to masked RGB Image

8 views (last 30 days)
Hi there...
is there anyone can help? I've tried to do a masked to a certain image based on the histogram of RGB color intensity. for example, I try to do the masking of the Red color (150 <R <190), Green (140 <G <185) and Blue (140 <R <193).
I got this intensity number from the highway image histogram where I want to perform filtering based on the intensity of the masking for the RGB histogram.
This is the script that I've tried, but it did not work for me..
thank you,
-asep-
% script-----------------------------------------------------------
I = imread('highway.tif');
J = rgb2gray(I);
figure, imshow(J);
figure, imhist(J);
R=imhist(I(:,:,1));
G=imhist(I(:,:,2));
B=imhist(I(:,:,3));
figure, imshow(I);
figure, plot(R,'r')
hold on, plot(G,'g') plot(B,'b'), legend(' Red channel','Green channel','Blue channel'); hold off,
%------------------------------------------
binaryRed = R > 150; % i dont know how to make a function for this intensity
binaryGreen = G > 140;
binaryBlue = B > 140;
% Find where all exceed threshold.
mask = binaryRed & binaryGreen & binaryBlue;
% Mask image to be black there.
maskedRgbImage = bsxfun(@times, I, cast(mask,class(I)));
imshow(maskedRgbImage);

Accepted Answer

Image Analyst
Image Analyst on 31 Mar 2015
See my SimpleColorDetection application in my File Exchange.
  1 Comment
Asep Muhammad Taufik
Asep Muhammad Taufik on 1 Apr 2015
Edited: Asep Muhammad Taufik on 1 Apr 2015
Thanks Image Analyst,.. i will learn your code,..

Sign in to comment.

More Answers (2)

Asep Muhammad Taufik
Asep Muhammad Taufik on 2 Apr 2015
wow.. amazing,.. :) thank you so much Image Analyst..

Asep Muhammad Taufik
Asep Muhammad Taufik on 20 Apr 2015
Edited: Asep Muhammad Taufik on 20 Apr 2015
Image Analyst please help me,.. now I have a red and green histogram data as follows, but I need to determine the two lowest point between the peaks ..
Then i used the data and make some experimenting using microsoft excel, but the position of the two lowest point between the peak is difficult to find automatically.
Is there any solution to my problem using matlab? Thank you for your help.
regards,
-enjat-
  2 Comments
Image Analyst
Image Analyst on 20 Apr 2015
Your image didn't come through. Anyway, start at the mode, which you can find from mode() or max() functions. Then start marching down the left side of the histogram with a for or while loop until it turns around.
Asep Muhammad Taufik
Asep Muhammad Taufik on 23 Apr 2015
image Analyst, I have tried the function max () and find the peak position (X, Y) .. thanks anyway...
but it's difficult for me to find a boundary point of the left and right peaks. I have tried a manual experiment, but i don't know how to use for (); while() function to solved my problems; could you help me please?
thanks for your help.. regards,
-enjat-
%myscript
clc;
clear;
close all;
workspace;
redband = imread('rgb2.tif');
R= redband(:,:,1);
G= redband(:,:,2);
B= redband(:,:,3);
data = imhist(R);
[Y, X] = imhist(R);
%plot(X, Y);
indexmax = find(max(Y) == Y)
xmax = X(indexmax)
ymax = Y(indexmax)
% iteration to the left side of the peak
%first iteration
indexleft1 = indexmax-1
xleft1 = X(indexleft1)
yleft1 = Y(indexleft1)
diffleft1 = ymax - yleft1
%second iteration
indexleft2 = indexmax-2
xleft2 = X(indexleft2)
yleft2 = Y(indexleft2)
diffleft2 = yleft1 - yleft2
%iteration must stop upon finding negative difference more than once, so the value (x,y) before the negative difference is taken.
% iteration to the right side of the peak
%first iteration
indexrigt1 = indexmax+1
xright1 = X(indexrigt1)
yright1 = Y(indexrigt1)
diffright1 = ymax - yright1
%second iteration
indexright2 = indexmax+2
xright2 = X(indexright2)
yright2 = Y(indexright2)
diffright2 = yright1 - yright2
%iteration must stop upon finding negative difference more than once, so the value (x,y) before the negative difference is taken.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!