How to make a mean image out of several images converted into a density map
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a script that works to count cells in a image of a rat brain section and divide them into bins that can be plotted as a density map i.e. the image is divided into squares each one having a certain number of cells and then the cell numbers are colormapped. I want to be able to use this script for making a mean image out of brain sections of several different animals to get a mean image of a group of animals. I managed to make it possible to select several image files and it runs all the files up until the colormapping bit. How do I make a mean out of the image files after each image has been divided into bins of cells?
This is the script I am working on:
function Cellcounting_pseudocolormapping(thresh,binsize,minp,maxp)
global cc
if(~isdeployed) cd(fileparts(which(mfilename))); end clc; % Clear command window. close all; % Close all figure windows except those created by imtool. imtool close all; % Close all figure windows created by imtool. workspace; % Make sure the workspace panel is showing.
gFontsize = 16; gFileNameDefault = 'T37-F7-MOVO-L.tif'; gFileDir = '.'; gThreshold = 0.4; gMinPix = 2; gMaxPix = 75; gBinSizePix = 20; gPixelsPerMM = 800; % need to figure this out...
if exist('thresh'), gThreshold = thresh; end if exist('minp'), gMinPix = maxp; end if exist('maxp'), gMaxPix = maxp; end if exist('binsize'), gBinSizePix = binsize; end
[gFileNames,gPathName,gFilterindex] = uigetfile(sprintf(sprintf('%s/*.tif',gFileDir)),'MultiSelect','on','Select Image File'); %if ~gFileName, %fprintf('WARNING: Loading default file %s\n',gFileNameDefault); %gFileName = gFileNameDefault; %end
% for each picture for K = 1 : length(gFileNames) gFileName = gFileNames{K};
% Read in standard MATLAB color image Image = imread(gFileName);
%increase contrast Image = rgb2gray(Image); Image = imadjust(Image);
% convert to binary according to threshold BW = im2bw(Image,gThreshold);
%change what is black to white and viece versa binImage = ~BW; binImage = 1-binImage; binImage = (binImage == 0);
%exclude blobs smaller than 9 pixels cc = bwconncomp(binImage); stats = regionprops(cc, 'Area'); idx = find([stats.Area] > gMinPix); BW2 = ismember(labelmatrix(cc), idx);
%exclude blobs larger than 75 pixels cc = bwconncomp(BW2); stats = regionprops(cc, 'Area'); idx = find([stats.Area] < gMaxPix); BW3 = ismember(labelmatrix(cc), idx);
%count cells in image cc = bwconncomp (BW3);
totpix = length(cc.PixelIdxList) pixcoords = zeros(totpix,2);
for i=1:totpix linpix = cc.PixelIdxList{i}(1); % convert the linear pixels (output from bwconncomp) to r,c coords [r,c] = ind2sub(cc.ImageSize,linpix); pixcoords(i,1) = c; pixcoords(i,2) = r; end
% now compute the density map
nr = cc.ImageSize(1); nc = cc.ImageSize(2);
nrb = floor(nr/gBinSizePix)+1; ncb = floor(nc/gBinSizePix)+1;
cpixcoords = squeeze(pixcoords(:,1)); rpixcoords = squeeze(pixcoords(:,2)); binCells = []; for rb = 1:nrb for cb = 1:ncb minr = (rb-1)*gBinSizePix+1; maxr = minr+gBinSizePix; minc = (cb-1)*gBinSizePix+1; maxc = minc+gBinSizePix; valindx = find(rpixcoords>=minr & rpixcoords<=maxr & cpixcoords>=minc & cpixcoords<=maxc); binCells(rb,cb) = numel(valindx); end end end
%make a mean image. How do I do this?
mmArea = (gBinSizePix/gPixelsPerMM)^2;
binCellDensity = binCells/mmArea;
size(binCells)
%subplot(2,2,3); imagesc(binCellDensity); %imagesc(imrotate(binCells,-90)); colormap(jet); colorbar;
0 commentaires
Réponses (1)
Doug Hull
le 29 Mai 2013
The image you are dealing with is really just a matrix of numbers where the numbers represent the density. If every image is the same size, then you could stack them in a three-dimensional matrix. When they are stored like this, math functions like mean, min, max can all take an optional input to determine what dimension you are working on.
0 commentaires
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!