Image processing code error
3 views (last 30 days)
Show older comments
I used this code and I got this message. I am trying to do image registeration and compare pixels color in 4 different images but usually it said the array is not consistant.
I make all the images to have the same pixels before I run the program but I am not sure why they are not consistant on matlab.
The error message is the following:
"Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in DetectOneBig_4_cycles (line 208)
ArrayResult1=[ArrayNum, MedianIntensity, ColorMatrix];"
And the cod is that
%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.5 1]; % set this number for bright pic alignment
fixed_trans=imadjust(J, contrastNum,[]);imshow(fixed_trans); % manually set threshold
originalImage=imread('brightcut.tif');%%%%%%%%% input the file name
ProteinSignal=imread('cropprotein.tif');
thresholdValue = 300;%%%%%%%% 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 moved2.tif');
Cycl2Moved=imread('cycle 2 moved.tif');
Cycl3Moved=imread('cycle 3 moved.tif');
Cycl4Moved=imread('cycle 4 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', 'Cycle 4'};
Rows=1; Cols=1; %change the arrays
Xblock=1;Yblock=1;
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;
brightcut=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);
CropCycl4Moved= Cycl4Moved(Xlow:Xhigh,Ylow:Yhigh,:); %imshow(CropCycl3Moved);
%%%%%%%%%% Use bright field pic with adjusted phase contrast
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%main program: FindCentroids
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display the grayscale image.
originalImage=fixed_trans;
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=brightcut;
tformEstimate = imregcorr(CropCycl1Moved,tempt1); Rfixed = imref2d(size(tempt1)); CropCycl1Ajust = imwarp(CropCycl1Moved,tformEstimate,'OutputView',Rfixed);
subplot(1, 4, 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, 4, 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, 4, 3);overlap=imfuse(tempt1, CropCycl3Moved,'blend','Scaling','joint'); imshow(overlap);
tformEstimate = imregcorr(CropCycl4Moved,tempt1); Rfixed = imref2d(size(tempt1)); CropCycl4Ajust = imwarp(CropCycl4Moved,tformEstimate,'OutputView',Rfixed);
subplot(1, 4, 4);overlap=imfuse(tempt1, CropCycl4Moved,'blend','Scaling','joint'); imshow(overlap);
ColorMatrix=string([numberOfBlobs,4]); % 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)> (Cycl1ColorG/BackgroundG)
color(k,1)='Red';
else if (Cycl1ColorR/BackgroundR)< (Cycl1ColorG/BackgroundG)
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) > (Cycl2ColorG/BackgroundG)
color(k,1)='Red';
else if (Cycl2ColorR/BackgroundR) < (Cycl2ColorG/BackgroundG)
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) > (Cycl3ColorG/BackgroundG)
color(k,1)='Red';
else if (Cycl3ColorR/BackgroundR) < (Cycl3ColorG/BackgroundG)
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,3)=color(k,1);
%cycle 4 color
BackgroundR=mean(mean(CropCycl4Ajust(:,:,1))); BackgroundG=mean(mean(CropCycl4Ajust(:,:,2)));
Cycl4ColorR=mean(mean(CropCycl4Ajust(round(tempcolor(1,1)-radius):round(tempcolor(1,1)+radius),round(tempcolor(1,2)-radius):round(tempcolor(1,2)+radius),1))); %%%Red colors
Cycl4ColorG=mean(mean(CropCycl4Ajust(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 (Cycl4ColorR/BackgroundR) > (Cycl4ColorG/BackgroundG)
color(k,1)='Red';
else if (Cycl4ColorR/BackgroundR) < (Cycl4ColorG/BackgroundG)
color(k,1)='Green';
else
color(k,1)='missing';
end
end
ColorMatrix(k,4)=color(k,1);
end
ArrayNum=zeros(numberOfBlobs,2);
for i=1:numberOfBlobs
ArrayNum(i,:)=[Xblock,Yblock];
end
ArrayResult1=[ArrayNum, MedianIntensity, ColorMatrix];
ArrayResult=[ArrayResult;ArrayResult1];
0 Comments
Answers (1)
Shubh Sahu
on 16 Sep 2019
ArrayNum, MedianIntesity are array of doubles where as ColorMatrix is array of string and it seems that all these different data are store in one array that’s why not consistent error came. One thing I can suggest, make it cell array so that it can store different type of data.
ArrayResult1={ArrayNum, MedianIntensity, ColorMatrix};
ArrayResult={ArrayResult;ArrayResult1};
One more thing is there, the threshold value is too high (above 255) due to which black image is generated while taking complement of original image and thresholding it. If threshold is below 255 then will get result like this
But after changing the threshold one has to work out in line no. 107
intentemp=cropProtein(round(temp(1,1)-radius+i),round(temp(1,2)-radius+j));
here matrix dimension of cropProtien is going out of bound so fix it according to your need.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!