How could I detect color?

I used this code to detect the color of circles but the code gives me a wrong color. Could you help me how to fix the mistake here. I think I have something wrong with adjusting the background of the images, but I am not sure.
if true
k = 1 : numberOfBlobs % for each microbeads
tempcolor=round(centroidsAll(k,:));
%cycle 1 color
BackgroundR=mean(mean(CropCycl1Ajust(:,:,1))); BackgroundG=mean(mean(CropCycl1Ajust(:,:,2)));
Cycl1ColorR=mean(mean(CropCycl1Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),1))); %%%Red colors
Cycl1ColorG=mean(mean(CropCycl1Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),2))); %%%Green colors
%set a threshould of 20%. Higher: the color of the microbead
if Cycl1ColorR > BackgroundR*1.2
color(k,1)='Red';
else if Cycl1ColorG > BackgroundG*1.2
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,1)=color(k,1);
end

2 Comments

We don't have enough to run your code, like the image or how you segmented the blobs out!
My code is a long code with multiple steps I just wrote here the cp;pr detection part. But I could share it with you.
if true
%find the centriods by bright image first, and find the radii.
%calculate the pixel locations for each centroids. Then average the value
%in protein detection signal.
%Further, find the color changes of 3 cycle images (RGB).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Prepare and locate the centroids of bright
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%image
fixed_bright = imread('brightcut.tif'); % take the bright field images as the reference
J = imcomplement(fixed_bright);
contrastNum=[0.2 1]; % set this number for bright pic alignment
fixed_trans=imadjust(J, contrastNum,[]);imshow(fixed_trans); % manually set threshold
%originalImage = imread('crop1.tif');%%%%%%%%%input the file name
cropProtein = imread('CropProtein.tif');
thresholdValue = 90;%%%%%%%%change threshold every time
radius=4; %%%%%set 4 as the radius in pixcels
PixelCut=10; % remove small areas less than 10 pixels
Cycl1Moved=imread('Cycle 1 moved.tif');
Cycl2Moved=imread('cycle 2 moved.tif');
Cycl3Moved=imread('cycle 3 moved.tif');
ProteinSignal=imread('cropProtein.tif');
%Try the first array of 36 arrays
[m,n]=size(fixed_trans);
% take only one array
ArrayResult={'Array row', 'Array col','Median Intensity', 'Cycle 1','Cycle 2','Cycle 3'};
Rows=6; Cols=6; %change the arrays
for Xblock=6:Rows
for Yblock=2:Cols
Xlow=round((Xblock-1)*m/Rows)+1; Xhigh=round(Xblock*m/Rows)-1;
Ylow=round((Yblock-1)*n/Cols)+1; Yhigh=round(Yblock*n/Cols)-1;
crop1=fixed_trans(Xlow:Xhigh,Ylow:Yhigh,:); % bright field
cropProtein=ProteinSignal(Xlow:Xhigh,Ylow:Yhigh,:); % proteins signal %imshow(CropCycl1Moved);
CropCycl1Moved= Cycl1Moved(Xlow:Xhigh,Ylow:Yhigh,:); %imshow(CropCycl1Moved);
CropCycl2Moved= Cycl2Moved(Xlow:Xhigh,Ylow:Yhigh,:); %imshow(CropCycl2Moved);
CropCycl3Moved= Cycl3Moved(Xlow:Xhigh,Ylow:Yhigh,:); %imshow(CropCycl3Moved);
%%%%%%%%%%Use bright field pic with adjusted phase contrast
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%main program: FindCentroids
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display the grayscale image.
originalImage=crop1;
subplot(1, 2, 1);
imshow(originalImage);
% Maximize the figure window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Force it to display RIGHT NOW (otherwise it might not display until it's all done, unless you've stopped at a breakpoint.)
drawnow;
captionFontSize=14;
caption = sprintf('Original and marked');
title(caption, 'FontSize', captionFontSize);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
%remove background by setting up and low limit
KK = imcomplement(originalImage);
binaryimageTrans=KK>thresholdValue;
binaryImage = bwareaopen (binaryimageTrans, PixelCut);
%imshow(binaryImage);
%binaryImage = binaryimageTrans > thresholdValue; % Bright objects will be chosen if you use >.
%binaryImage = imfill(binaryImage, 'holes');
labeledImage = bwlabel(binaryImage, 8); % Label each blob so we can make measurements of it
coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle'); % pseudo random color labels
% coloredLabels is an RGB image. We could have applied a colormap instead (but only with R2014b and later)
subplot(1,2,2);
imshow(coloredLabels);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
caption = sprintf('Numbered from top to bottom, then from left to right.');
title(caption, 'FontSize', captionFontSize);
% Get all the blob properties. Can only pass in originalImage in version R2008a and later.
blobMeasurements = regionprops(labeledImage, originalImage, 'all');
numberOfBlobs = size(blobMeasurements, 1);
axis image; % Make sure image is not artificially stretched because of screen's aspect ratio.
textFontSize = 5; % Used to control size of "blob number" labels put atop the image.
labelShiftX = 0; % Used to align the labels in the centers of the coins.
allBlobCentroids = [blobMeasurements.Centroid];
centroidsX = allBlobCentroids(1:2:end-1);
centroidsY = allBlobCentroids(2:2:end);
% Put the labels on the rgb labeled image also.
%%%%%%%%%%%%%%(JW) draw circles around centers
subplot(1, 2,2);
radii=ones(numberOfBlobs,1)*radius;
centroidsAll=[centroidsX' centroidsY'];
viscircles(centroidsAll,radii)
%remove outlier--> reside in empty area
for i=1:numberOfBlobs
if (centroidsAll(i,1)>(Xhigh-Xlow)) || (centroidsAll(i,2)>(Yhigh-Ylow))
centroidsAll(i,:)=10;
end
if (centroidsAll(i,1)<4) || (centroidsAll(i,2)<4)
centroidsAll(i,:)=10;
end
end
for k = 1 : numberOfBlobs % Loop through all blobs.
text(centroidsX(k) + labelShiftX, centroidsY(k), num2str(k), 'FontSize', textFontSize, 'FontWeight', 'Bold');
end
%%%%%%%%%%%%%%%%%%%%%%%%%%(JW) find the pixels and intensity from protein signal within each circle
% from cropProtein data.
MedianIntensity=zeros(numberOfBlobs,1);
for k = 1 : numberOfBlobs % for each microbeads
temp=round(centroidsAll(k,:));
Intensity=cropProtein(temp(1,1),temp(1,2)); %%%one data
for i=1:(2*radius)
for j=1:(2*radius)
if ((i-radius)^2+(j-radius)^2)<radius^2
intentemp=cropProtein(round(temp(1,1)-radius+i),round(temp(1,2)-radius+j));
Intensity=[Intensity; intentemp];
end
end
end
MedianIntensity(k,1)=median(Intensity);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%(JW) Find color of each of the spot
%%%calculate R,G and B mean data for every circle above, and then set
%%%threshold. Use "cycle 1 moved.tif" "cycle 2 moved.tif" "cycle 3 moved.tif"
% crop first, and align them with crop1.tif
figure
tempt1=crop1;
tformEstimate = imregcorr(CropCycl1Moved,tempt1); Rfixed = imref2d(size(tempt1)); CropCycl1Ajust = imwarp(CropCycl1Moved,tformEstimate,'OutputView',Rfixed);
subplot(1, 3, 1); overlap=imfuse(tempt1, CropCycl1Ajust,'blend','Scaling','joint'); imshow(overlap);
tformEstimate = imregcorr(CropCycl2Moved,tempt1); Rfixed = imref2d(size(tempt1)); CropCycl2Ajust = imwarp(CropCycl2Moved,tformEstimate,'OutputView',Rfixed);
subplot(1, 3, 2);overlap=imfuse(tempt1, CropCycl2Moved,'blend','Scaling','joint'); imshow(overlap);
tformEstimate = imregcorr(CropCycl3Moved,tempt1); Rfixed = imref2d(size(tempt1)); CropCycl3Ajust = imwarp(CropCycl3Moved,tformEstimate,'OutputView',Rfixed);
subplot(1, 3, 3);overlap=imfuse(tempt1, CropCycl2Moved,'blend','Scaling','joint'); imshow(overlap);
ColorMatrix=string([numberOfBlobs,3]); % for 3 cycles
color=string([numberOfBlobs,1]);
for k = 1 : numberOfBlobs % for each microbeads
tempcolor=round(centroidsAll(k,:));
%cycle 1 color
BackgroundR=mean(mean(CropCycl1Ajust(:,:,1))); BackgroundG=mean(mean(CropCycl1Ajust(:,:,2)));
Cycl1ColorR=mean(mean(CropCycl1Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),1))); %%%Red colors
Cycl1ColorG=mean(mean(CropCycl1Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),2))); %%%Green colors
%set a threshould of 20%. Higher: the color of the microbead
if Cycl1ColorR > BackgroundR*1.2
color(k,1)='Red';
else if Cycl1ColorG > BackgroundG*1.2
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,1)=color(k,1);
%cycle 2 color
BackgroundR=mean(mean(CropCycl2Ajust(:,:,1))); BackgroundG=mean(mean(CropCycl2Ajust(:,:,2)));
Cycl2ColorR=mean(mean(CropCycl2Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),1))); %%%Red colors
Cycl2ColorG=mean(mean(CropCycl2Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),2))); %%%Green colors
%set a threshould of 20%. Higher: the color of the microbead
if Cycl2ColorR > BackgroundR*(1.2)
color(k,1)='Red';
else if Cycl2ColorG > BackgroundG*1.2
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,2)=color(k,1);
%cycle 3 color
BackgroundR=mean(mean(CropCycl3Ajust(:,:,1))); BackgroundG=mean(mean(CropCycl3Ajust(:,:,2)));
Cycl3ColorR=mean(mean(CropCycl3Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),1))); %%%Red colors
Cycl3ColorG=mean(mean(CropCycl3Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),2))); %%%Green colors
%set a threshould of 20%. Higher: the color of the microbead
if Cycl3ColorR > BackgroundR*(1.2)
color(k,1)='Red';
else if Cycl3ColorG > BackgroundG*1.2
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,3)=color(k,1);
end
ArrayNum=zeros(numberOfBlobs,2);
for i=1:numberOfBlobs
ArrayNum(i,:)=[Xblock,Yblock];
end
ArrayResult1=[ArrayNum, MedianIntensity, ColorMatrix];
ArrayResult=[ArrayResult;ArrayResult1];
end
end
end

Sign in to comment.

Answers (0)

Categories

Asked:

on 22 Sep 2018

Commented:

on 22 Sep 2018

Community Treasure Hunt

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

Start Hunting!