% % % RGB to Gray for Imaris % % Copyright Bitplane AG 2006 % % % Installation: % % - Copy this file into the XTensions folder in the Imaris installation directory % - You will find this function in the Image Processing menu % % % % % Matlab::BPRGB2Gray(%i,'%c') % % % % % % Description: % % Add a gray channel to the dataset. % Its intensity is given by a weighted sum of the intensity of the other % channels. % The coefficients of the sum are given by the user. % % function BPRGB2Gray(aImarisApplicationID,aChannelSelection) % convert channel selection string into array vChannelSelection = str2num(aChannelSelection); % connect to Imaris Com interface if ~isa(aImarisApplicationID, 'COM.Imaris_Application') vImarisServer = actxserver('ImarisServer.Server'); vImarisApplication = vImarisServer.GetObject(aImarisApplicationID); else vImarisApplication = aImarisApplicationID; end %Get the dataset vImarisDataSet = vImarisApplication.mDataSet.Clone; % Draw the initial dialog with values such as their sum is equal to 1 vNumberOfChannelsSelected = sum(vChannelSelection); if vNumberOfChannelsSelected >= 1 % Compute the initial weight values vDefaultWeigth = (1/vNumberOfChannelsSelected); vPrompt = cell(vNumberOfChannelsSelected-1, 1); vDefaultPromptAnswer = cell(vNumberOfChannelsSelected-1, 1); for i = 1:(vNumberOfChannelsSelected-1) vPrompt{i} = ['Channel ' int2str(i)]; vDefaultPromptAnswer{i} = num2str(vDefaultWeigth); end vPrompt(vNumberOfChannelsSelected) = {['Channel ' int2str(vNumberOfChannelsSelected)]}; vDefaultPromptAnswer(vNumberOfChannelsSelected) = {num2str(1 - (vNumberOfChannelsSelected-1)*vDefaultWeigth)}; % Draw the dialog vAnswer = inputdlg(vPrompt,'RGB2Gray',1,vDefaultPromptAnswer); % Do something only if values exist if length(vAnswer) > 0 vDefineWeight = str2num(char(vAnswer)); % the sum of the defined weigths must be equal to 1 if sum(vDefineWeight) >= 0.9 && sum(vDefineWeight) <= 1.1 % Dataset properties vNumberOfTimePoints = vImarisDataSet.mSizeT; vNumberOfChannels = vImarisDataSet.mSizeC; vNumberOfSlices = vImarisDataSet.mSizeZ; vTotalCount = vNumberOfSlices*vNumberOfChannelsSelected*vNumberOfTimePoints; vProgressDisplay = waitbar(0,'BPRGB2Gray:Processing Volume...'); vCount = 0; % Create a new channel vImarisDataSet.mSizeC = vNumberOfChannels + 1; vImarisDataSet.SetChannelName(vNumberOfChannels, 'Gray Channel'); % Loop through the dataset... for vTime = 1:vNumberOfTimePoints for vSlice = 1:vNumberOfSlices vImageSliceSum = zeros(vImarisDataSet.mSizeX,vImarisDataSet.mSizeY); for vChannel = 1:vNumberOfChannels if vChannelSelection(vChannel) == 1 % Add each channel of one slice and one % timepoint with the defined weights. The % resulting image is the gray image. vImageSlice = double(vImarisDataSet.GetDataSlice(vSlice-1, vChannel-1, vTime-1))*vDefineWeight(vChannel); vImageSliceSum = vImageSliceSum + vImageSlice; vCount = vCount + 1; end end % The image is given back to Imaris if strcmp(vImarisDataSet.mType,'eTypeUInt8') vImarisDataSet.SetDataSlice(uint8(vImageSliceSum), vSlice-1, vNumberOfChannels,vTime-1); elseif strcmp(vImarisDataSet.mType,'eTypeUInt16') vImarisDataSet.SetDataSlice(uint16(vImageSliceSum), vSlice-1, vNumberOfChannels,vTime-1); elseif strcmp(vImarisDataSet.mType,'eTypeFloat') vImarisDataSet.SetDataSlice(single(vImageSliceSum), vSlice-1, vNumberOfChannels,vTime-1); end waitbar(vCount/vTotalCount) end end close(vProgressDisplay) vImarisApplication.mDataSet = vImarisDataSet; end end end