turn green pixels to black and everything else white

I currently have a program that takes an image through a connected camera and then processes that image. The processed image will turn black and white, turning any darker colors or basically anything other than white into black and having the rest of the image stay white. The purpose of the program is to recognize a target, take a picture of the target, and determine the distance between the target and the camera. Right now the program is taking the processed black and white image, turning some fiducials on the target into black and keeping everything else white, counting the black pixels in the image (which are the fiducials), and using an equation to turn the amount of pixels into a distance. The fiducials on my target are going to be green, so I need the program to recognize only green colors and turn the green pixels into black and have everything else to be white. Everything else on the program works, except for it wanting to pick up multiple colors. This is my code below, anything that I have commented I have taken out to get the program to work so far. Any help that you could provide would be great.
clc
clear
clear all
images= [];
webcamlist();
cam= webcam(1);
preview(cam)
pause(2);
images= snapshot(cam);
imshow(images)
r_channel= images(:,:,1);
g_channel= images(:,:,2);
b_channel= images(:,:,3);
images(:,:,1) =1;
images(:,:,3) = 1;
images(:,:,2) = 1;
image(images);
mask= g_channel > 100, b_channel > 800; r_channel < 800; b_channel < 1; r_channel < 0;
imshow(mask);
gr_ratio = double(g_channel)./double(r_channel);
gb_ratio = double(g_channel)./double(b_channel);
rb_ratio = double(r_channel)./double(b_channel);
% gr_ratio(isnan(g_ratio))=0;
% grayImage = rgb2gray(image);
% binaryImage = grayImage > 20;
% binaryImage = bwareaopen(binaryImage, 50);
% binaryImage = imfill(binaryImage, 'holes');
% grayImage(~binaryImage) = 0;
falseMatrix= zeros(7,7);
targetMatrix= ones(7,7);
checkMatrix= ones(7,7);
%if targetMatrix == checkMatrix
% cam = camStop;
%return
%end
%this counts how many black pixels are in the image
numBlackPixels = numel(mask) - nnz(mask);
%This displays how many black pixels were counted
disp(numBlackPixels);
%This converts the amoun of black pixels into a distance
distanceInches = (157.21- (0.019214 * numBlackPixels) + ((7.59064 * (10^(-7))) * (numBlackPixels^2)));
%This displays the distance in inches between the target and the camera
disp(distanceInches);

3 Comments

How would you define a green pixel? At any rate, I suspect this line:
mask= g_channel > 100, b_channel > 800; r_channel < 800; b_channel < 1; r_channel < 0;
does not do what you are hoping it does. The way it is written, it sets mask to g_channel > 100 and then runs the remaining four comparisons without doing anything with their results. What are you hoping the line does?
To answer your first question, I just need anything that is green in the picture to be processed to black. And to be completely honest, I dont completely remember what the goal was with this line. I came up with the basis of the code a couple of weeks ago at the beginning of a project. When I started, I did not know what color the fiducials for my target was going to be, so I just had the program take a variety of colors and process them to turn them to black.
mask = g_channel > 100 & b_channel < 800 & r_channel < 800 & b_channel > 1 & r_channel > 0;
maybe??
However, webcams almost always return uint8 (but not necessarily rgb) so the maximum channel value would be 255.
"Green" does not have a particular definition so you have to make a decision that works for your purposes.
You might, for example, convert to HSV and look at the Hue, making a decision based upon your needs. See the hue map at https://stackoverflow.com/questions/47483951/how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image

Sign in to comment.

Answers (0)

Asked:

on 8 Apr 2020

Commented:

on 8 Apr 2020

Community Treasure Hunt

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

Start Hunting!