Help getting xy coordiantes using for loop and impixel...!

1 view (last 30 days)
I am trying to get all the z values (x,y coordinates of B image) of certain RGB values. My ultimate goal is to create a big matrix z of all the x and y values that meet the criteria of the if statement. When I run the code, all the z values are shown in the command window, but only the last recorded z value is stored in the work space. I need all the z(x,y coordinates) into one matrix.
A=imread('ulcer.jpg'); % Or any other image is fine
B=imresize(A, [100 100]);
for i=1:100;
for j=1:100;
x=i;
y=j;
RGB=impixel(B,x,y);
if RGB(1)>= 120 & RGB(1)<=180 & RGB(2)>=20 & RGB(2)<=90 & RGB(3)>=40 & RGB(2)<=100;
z=[x y]
else
disp('No');
end
end
end

Accepted Answer

Image Analyst
Image Analyst on 29 Nov 2014
Try it this way instead
% Extract individual color channels.
redChannel = B(:,:,1);
greenChannel = B(:,:,2);
blueChannel = B(:,:,3);
% Create masks to see where criteria is met for each color channel.
redMask = redChannel > 120 & redChannel < 180;
greenMask = greenChannel > 20 & greenChannel < 90;
blueMask = blueChannel >= 40 & blueChannel <= 100;
% Combine to see where criteria is met for all color channels.
overallMask = redMask & greenMask & blueMask;
% Find rows and columns where criteria is met:
[rows, columns] = find(overallMask);

More Answers (0)

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!