Clear Filters
Clear Filters

How can I set up an RGB threshold

1 view (last 30 days)
I am given an image and using an input value by the user called thrper I am to calculate a threshold value for each of the color layers I have made
[image_R, image_G, image_B] = seperate_RGB_components(image_RGB)
I am to only use explicit loops such as for,if and else. In his example my professor sets thrper to .75 meaning the bottom 75% values will be converted to black and the top 25% will be converted to white (0 and 255 respectively). Will I need to use a combination of for and if loops to do so? Or is there a simpler method using only one of the explicit loops?

Accepted Answer

David Sanchez
David Sanchez on 5 Nov 2013
There are easy ways to do this, but if you MUST use for loops and if conditionals:
[image_R, image_G, image_B] = seperate_RGB_components(image_RGB)
thrper = .75 % threshold
% separate the layers
image_R = image(:,:,1);
R_thres = sum(sum(image_R))/numel(image_R);
image_G = image(:,:,2);
G_thres = sum(sum(image_G))/numel(image_G);
image_B = image(:,:,3);
B_thres = sum(sum(image_B))/numel(image_B);
% apply filter
[rows cols] = size( image_R ); % number of rows and columns
% RED LAYER
for k_r = 1:rows
for k_c = 1:cols
if (image_R(k_c,k_r) < R_thres)
image_R(k_c,k_r) = 0;
else
image_R(k_c,k_r) = 255;
end
end
end
% GREEN LAYER
for k_r = 1:rows
for k_c = 1:cols
if (image_G(k_c,k_r) < G_thres)
image_G(k_c,k_r) = 0;
else
image_G(k_c,k_r) = 255;
end
end
end
% BLUE LAYER
for k_r = 1:rows
for k_c = 1:cols
if (image_B(k_c,k_r) < B_thres)
image_B(k_c,k_r) = 0;
else
image_B(k_c,k_r) = 255;
end
end
end
  2 Comments
Sabanam
Sabanam on 27 Mar 2014
After getting individual Segmented image R,G,B,how can i show single segmented image or combine three results?
Image Analyst
Image Analyst on 27 Mar 2014
What would the single image represent? Do you want to AND or OR the three individual images together? We have no idea how you want to, or should, combine your 3 images. Why not start your own thread with your own image attached?

Sign in to comment.

More Answers (1)

Feynman
Feynman on 5 Nov 2013
image_R = image(:,:,1);
R_thres = sum(sum(image_R))/numel(image_R);
image_G = image(:,:,2);
G_thres = sum(sum(image_G))/numel(image_G);
image_B = image(:,:,3);
B_thres = sum(sum(image_B))/numel(image_B);
This code won't calculate the range of the image RGB will it? The function should prompt the user to enter a percentage to be saved as thrper and by finding the range of values in the image layer find the points in which the pixel value should be reset to 0 or 255 so if the red layer had values ranging from -100 to 300 the threshold value should be 200 so any pixel below 200 will be set to 0 and any value above 200 will be set to 255

Community Treasure Hunt

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

Start Hunting!